Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active March 11, 2021 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/8596e05cb01d7f1d5acc0bdf2f6e99a5 to your computer and use it in GitHub Desktop.
Save damiencarbery/8596e05cb01d7f1d5acc0bdf2f6e99a5 to your computer and use it in GitHub Desktop.
Attach WooCommerce product image to Contact Form 7 email - Based on a comment on another post, use a CF7 filter to attach a WooCommerce product image to the email. https://www.damiencarbery.com/2018/10/attach-woocommerce-product-image-to-contact-form-7-email/
<?php
/*
Plugin Name: Attach WooCommerce product image to Contact Form 7 email
Plugin URI: https://www.damiencarbery.com/2018/10/attach-woocommerce-product-image-to-contact-form-7-email/
Description: Based on a comment on another post, use a CF7 filter to attach a WooCommerce product image to the email.
Author: Damien Carbery
Version: 0.2
*/
// Add product image to email as an attachment
add_filter( 'wpcf7_mail_components', 'dcwd_cf7_mail_components', 10, 3 );
function dcwd_cf7_mail_components( $components, $current_cf7_form, $form_object ) {
$cf7_form_id = 6890;
// Verify that the we are processing the correct form.
if ( $cf7_form_id == $current_cf7_form->id() ) {
// Ensure that a match was found.
if ( 1 === preg_match( '/Product ID: (\d+)/m', $components[ 'body' ], $matches ) ) {
$product_id = $matches[1];
// Check that the matched product ID is a number.
if ( ( $product_id > 0 ) && is_numeric( $product_id ) ) {
// Create a product object...
$product = wc_get_product( $product_id );
// checking that the ID is a valid product...
if ( is_object( $product ) ) {
// then get the product image url.
$product_image_url = wp_get_attachment_image_src( $product->get_image_id(), 'large' )[ 0 ];
if ( $product_image_url ) {
// Change the url to an absolute path to include it in the email.
$product_image_path = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $product_image_url );
// Verify that the file exists before appending it to the attachments array.
if ( file_exists( $product_image_path ) ) {
$components[ 'attachments' ][] = $product_image_path;
}
}
}
}
}
}
return $components;
}
[dynamichidden product-id "CF7_get_post_var key='ID'"]
Product ID: [product-id]
@Jashandeep-1229
Copy link

Hi sir
I want to know how to add product description in the email

@damiencarbery
Copy link
Author

I want to know how to add product description in the email
You can use:
$product->get_description();
or
$product->get_short_description();

You then need to add that information to $components[ 'body' ]
Where did you need it added? At the end of the email? In the middle?

@Jashandeep-1229
Copy link

Jashandeep-1229 commented Mar 9, 2021 via email

@Jashandeep-1229
Copy link

Jashandeep-1229 commented Mar 9, 2021 via email

@Jashandeep-1229
Copy link

one more thing is it possible to add a quotation form on the woo-commerce product details page without a button.

@damiencarbery
Copy link
Author

damiencarbery commented Mar 10, 2021

I want to add in the middle of the email like email body don't want to show on the form while submitting after submitted, I want to send a pdf with product images, name, description, and some text. to admin's email

You will need to add a placeholder in the CF7 mail message body e.g. PRODUCT_NAME or PRODUCT_DESCRIPTION.
Then do a str_replace() to replace those placeholders with the proper values. For example:
str_replace('PRODUCT_DESCRIPTION', $product->get_description(). $components[ 'body' ] );

@damiencarbery
Copy link
Author

one more thing is it possible to add a quotation form on the woo-commerce product details page without a button.

I don't understand what you mean. Do you want to remove the "Add to cart" button?
I did write code for a Product Enquiry form: https://www.damiencarbery.com/2017/02/product-enquiry-form-using-woocommerce-and-contact-form-7/

@Jashandeep-1229
Copy link

Ok Thank You so much
Can you tell me how to get value from one contact form to another contact form with another link
Like:-
first, I filled form (Name, Email, and Phone), after submitting the form
I got an email with a button then I click this button and opened a new form
but I need three field values which I already filled the first form on the new form with hidden field

@damiencarbery
Copy link
Author

Ok Thank You so much
Can you tell me how to get value from one contact form to another contact form with another link
Like:-
first, I filled form (Name, Email, and Phone), after submitting the form
I got an email with a button then I click this button and opened a new form
but I need three field values which I already filled the first form on the new form with hidden field

I don't know how to do that but https://wordpress.org/plugins/contact-form-7-multi-step-module/ is good for multi step forms where the person would fill in the forms in one go. It would not do one form, then an email link to the second form.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment