• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Damien Carbery - Website Development

Getting You Online & Keeping You There

Getting You Online & Keeping You There Get Started
  • Home
  • Services
  • Maintenance Plans
  • Portfolio
  • Blog
  • About Me
  • Contact
You are here: Home / Website Development / Product Enquiry form using WooCommerce and Contact Form 7

Product Enquiry form using WooCommerce and Contact Form 7

Published: February 6, 2017 (Updated: August 27, 2023)

Make it easy for customers to ask questions about your products - add a product enquiry form that will include the product name in the email that it sends.

I have added a WooCommerce version number to the code so that you can see what version I have tested the code on.

25 August 2023: I have changed all dynamictext to dynamic_text and dynamichidden to dynamic_hidden as version 4.0.0 of CF7 DTX as deprecated the former.

I also updated the WC tested up to: 8.0.2 line.

Sometimes a customer has questions that the product description and photos don’t answer. Instead of requiring them to go to your contact page you can make it much easier to ask their questions by added a simple contact form to the product page. I will show you how to include the product name in the query email without having to have a different form for each product, and how to automatically add the form to each product page.

Install the Plugins

I recommend the very popular Contact Form 7 (CF7) plugin. You will also need the Contact Form 7 Dynamic Text Extension plugin, a companion plugin to Contact Form 7.

After you install and activate the Contact Form 7 and Contact Form 7 Dynamic Text Extension plugins you can go to the Contact/Contact Forms page. For the enquiry form you can edit an existing form or create a new one.

CF7 forms do not have any dynamic features so the email sent by the form won’t tell you the product page that the form was submitted from. We need the features of the Contact Form 7 Dynamic Text Extension plugin.

Add Product Name to Enquiry Form

We need to add a field to the CF7 form with the product name as its value so that it can be included in the email that CF7 sends. The product name can be included as a visible field in the form or as a hidden field.

Edit the contact form and insert a new form field that will hold the product name.
It will look better to include the new field as a hidden one, but we will initially use a visible field to make it easy to check that it is being correctly populated with the product name.

This uses the Dynamic Text Extension plugin to retrieve the title of the current post (or page or product). I can now use the declared mail tag (‘product-name’) in the email:

You can see that I have added it to the mail subject and body.

Test the Form

It is worth doing a quick test that the form displays correctly and also includes the product name in the email. Edit one of your products and add the form shortcode to the editor area. For example:

Change Product Name to Hidden Field

If the form is working as expected I suggest changing the product name field to be hidden so that users don’t change its value when submitting the form. Just change dynamic_text to dynamic_hidden.

Automatically Add Form to All Products

You could manually add the form shortcode to the product description of every product but that would be tedious and error prone. It would also be easy to forget to add it to products that you add in the future.

The easiest method is to write a small plugin to do this automatically. Either put this code in a file in the wp-content/plugins directory and activate it, or add the code to the active theme’s functions.php.

The code checks that Woocommerce is active because the is_product() check is only available when it is. If you omit the first test it will cause a fatal error:

That code places the form in the ‘Description’ tab after the full product description.
You can also place it under the short description using add_action or add_filter.

Enhancements

You may decide to only display the enquiry form when the product is in stock, or to display a slightly different form when it is out of stock. When you have checked that Woocommerce is active you can easily access all the information for the product. The options are endless.

Display Form for In Stock Products

For example, here is the code to display the form for in-stock products:

Form to Report Error on Page

You could create a form to allow people report errors on your page e.g. spelling mistakes. Adding the page or post url is easy but you can go further by including a link to be able to edit the page.

We need the page url (so that you can review the report) and then the site’s url and the post ID. These two will be combined to create a link to directly edit the page.

We combine the site url and post ID to make the edit link. When you click on that link you can quickly correct the issue.

Include Product SKU in Email

The SKU is stored as a custom field so ‘CF7_get_custom_field’ can be used to retrieve it. The key is ‘_sku’.

Limit the form to specified categories

You can use the has_term() function to test whether a product is in specified categories. It can take an array of category slugs.

Using CF7 in a popup

If CF7 is used in a popup it may not be possible to retrieve the product information using the built in CF7 DTE features. You may need a custom shortcode. I have written a separate post about using a Shortcode for Contact Form 7 Dynamic Text Extension.

Related Posts

Shortcode for Contact Form 7 Dynamic Text Extension

CF7DTE is great but sometimes you need a custom shortcode to get product info into the form.

Product Enquiry form with Ninja Forms

I completed the code for a product enquiry form for a variable product using Ninja Forms.

Contact Form 7 Product Enquiry form for variable products

Watch the variation dropdowns and copy the values to a CF7DTE hidden field.

Reader Interactions

Comments

  1. Alex says

    October 27, 2017 at 4:37 pm

    Hi, thanks for this. I created a new plugin copying the content of what you wrote above. The only problem I have is that the function runs only on the home page and not on the products pages… any idea why? I even tried to add that code to the theme functions.php

    Reply
    • damien says

      October 27, 2017 at 9:25 pm

      Can you post your code somewhere e.g. https://pastebin.com/ The code can be in functions.php or its own file as a plugin.

      Reply
    • damien says

      November 1, 2017 at 9:16 am

      Resolved: Sort of… Alex and I did some troubleshooting over an number of emails. For some reason the code was not running on the product page, even when is_product() was true – this was verified by Query Monitor and Query Monitor Extend.

      The workaround was to use a WooCommerce action. Alex wrote the following:

      add_action('woocommerce_after_single_product_summary', 'add_quote_form');

      function add_quote_form(){ echo do_shortcode('[contact-form-7 id="2173" title="Request a Quote"]'); }

      Reply
      • morissa says

        March 30, 2019 at 8:41 pm

        Thank you damien ! I’m using your solution and very satisfied. Regards!

        Reply
        • damien says

          March 30, 2019 at 8:57 pm

          Brilliant, thanks for letting me know.

          Reply
  2. Nick says

    November 13, 2017 at 2:42 pm

    I was searching for a while to get that product name into Contact form 7. Been through lots of sites but nothing was simple like this one.. Finally I can add product name directly into the Contact form 7 enquiry form. Saved my day. Halfway through the article. Thank you so much <3

    Reply
  3. Nio says

    November 20, 2017 at 12:59 am

    how to add woo product’s SKU instead of product title ?

    Reply
    • damien says

      November 20, 2017 at 8:50 pm

      Nio – The SKU is stored in a custom field so you can use the ‘CF7_get_custom_field’ feature of the plugin. https://gist.github.com/damiencarbery/f7db36de7a875b6b9b4d10ff61597f60#file-cf7-retrieve-product-sku-txt

      Reply
      • Marco says

        October 17, 2019 at 8:44 pm

        Hi Damien,

        this doesn`t work for me. The field SKU remains without the sku.

        Thanks!

        Reply
        • damien says

          October 18, 2019 at 10:45 am

          @Marco: Please email me a screenshot or copy of the text in the Contact Form 7 “Message Body” Also, is the SKU set in the Edit Product area? Can you provide a url that I could look at?

          Reply
          • damien says

            November 1, 2019 at 3:09 pm

            Marco and I exchanged a few emails and I logged into his website. Everything looked good. I did not see the error.

          • damien says

            November 8, 2019 at 4:06 pm

            After a few more emails Marco told me that he’s using the WooCommerce Quote or Enquiry Contact Form on the site. This loads the CF7 form in a popup using ajax. The product’s custom fields cannot be accessed by the CF7 DTE plugin so I needed to write a custom shortcode. I have written more about this in Shortcode for Contact Form 7 Dynamic Text Extension.

  4. sergigonza says

    December 27, 2017 at 9:18 pm

    Hello, I want show form in specific category product, but i don´t know how do it …

    I know I have to modify the following if, but I do not know how to filter by category:

    `if ( $product->is_in_stock() ) {`

    Can you help me?

    Reply
    • damien says

      December 27, 2017 at 11:15 pm

      To show the form on a category archive you will need a different hook as ‘the_content’ is not run on category archives.

      add_action( 'woocommerce_after_shop_loop', 'dc_cf7_form_to_category_archive', 40 ); function dc_cf7_form_to_category_archive() { if ( is_product_category( array( 'category_1', 'category_2' ) ) ) { echo do_shortcode( '[contact-form-7 id="65" title="Contact form 1"]' ); } }

      To apply to all categories you can remove the is_product_category() check. You can see examples of that function at: WooCommerce Conditional Tags docs page. The ‘woocommerce_after_shop_loop‘ is only on category pages so the function doesn’t need to check whether it is in a product category page.

      I suggest using [dynamichidden page-url "CF7_URL"] in the form and Page URL: [page-url] in the email so that you know what category the form came from.

      You could write a shortcode that returns the product category, but the url is a quick solution.

      Reply
  5. Francesco says

    February 22, 2018 at 3:09 pm

    Is there a way to request in the same mail a quote for multiple products specifying different characteristics for each of them? I Mean: i would ask a quote for 200 t-shirts with: SIZE: M, COLOR: WHITE, SLEEVE: LONG and 150 HATS with: COLOR: BLACK, LOGO: YELLOW etc…?

    Reply
    • damien says

      February 22, 2018 at 4:03 pm

      Francesco – The default contact form has a textarea for the message. The customer can add anything they want in that textarea so they could ask for the quote there. I don’t think that it would be worth writing code for a rare scenario like you describe. You could add text in the form to explain to customers that they can ask for a quote.

      Reply
  6. Simon says

    April 19, 2018 at 12:18 pm

    Hmm, how would we use this with a variable product to get the variable SKU?

    Reply
    • damien says

      April 20, 2018 at 10:38 am

      Simon – I think that you would need to write custom code for this. You can easily get the product ID and from that use wc_get_product($id) to the product object and then call ‘get_available_variations()’ to get an array of info about the variations and then get the SKU for each variation.

      The hard part might be how to know which variation the customer is asking about. I don’t know if you could inject markup into the form e.g. radio buttons, one for each variation.

      Can you describe in more detail what you would like to see in the form and in the email?

      I suggest posting a question in the the CF7 Dynamic Text Extension support forum: https://wordpress.org/support/plugin/contact-form-7-dynamic-text-extension

      Reply
    • damien says

      May 14, 2018 at 9:20 am

      Simon – I implemented an enquiry form for variable products using Ninja Forms: https://www.damiencarbery.com/2018/05/product-enquiry-form-with-ninja-forms/

      Reply
  7. Peter James says

    June 18, 2018 at 2:51 pm

    Hi Damien

    This is very helpful.

    Is there a way to automatically put the form into products that are in a certain category?

    Many thanks…Peter

    Reply
    • damien says

      June 18, 2018 at 4:38 pm

      Peter – You certainly can. Add a check like: `has_term( ‘MyCategorySlug’, ‘product_cat’ )` You can provide a category slug or name or ID as the first parameter.

      Reply
  8. Alessio says

    June 21, 2018 at 4:00 pm

    Hi. i’d like to view the form to a modal pop-up in when i clickto a button with the name “Ask info”

    That is possible?

    Reply
    • Alessio says

      June 21, 2018 at 5:08 pm

      where i have to insert the code if i want to show it under the buy button?

      Reply
    • damien says

      June 22, 2018 at 12:11 pm

      Alessio – Here is code to create a button under the Add to Cart button. https://gist.github.com/damiencarbery/f7db36de7a875b6b9b4d10ff61597f60#file-add-cf7-popup-to-product-page-php

      It uses ‘thickbox’, an old javascript module that is good for popups. I used it because it is part of WordPress core. It is probably not responsive and you might find a much better option.

      https://businessbloomer.com/woocommerce-visual-hook-guide-single-product-page/ is very helpful to find the appropriate action on WooCommerce pages.

      Reply
  9. Toto says

    July 7, 2018 at 5:57 pm

    Hi, great pleasure to find this useful post!! I added a form to my woocommerce and everithing is working perfectly (product description and SKU). I will be pleased to know if possible to add some other useful info into the form… the product attribute. For instance, will be useful the form to contain –> product decription / attribute (brand) / SKU: (Jeans / Armani / 4452aa4) Do You know if possible? Thank You in advance for Your help

    Reply
    • damien says

      July 7, 2018 at 8:19 pm

      Toto – I could not find a way to add attribute information to a Contact Form 7 form. I then tried Ninja Forms and I was able to add a drop down with a list of the product variations. The customer chooses the variation that they are enquiring about. See: Product Enquiry form with Ninja Forms

      Reply
  10. Stefanie says

    July 11, 2018 at 5:32 pm

    I’m looking for something where I can create a contact form that has a section where customers can select any products from the full list of products. I.E. I want a form that has a list of every product available where a customer can check each product they are inquiring about. I don’t want it separate on each product page, just one list included in one form. Is there a way to do this? This insanity is not for me… it’s for a client! Any help is appreciated :)

    Reply
    • damien says

      July 11, 2018 at 11:02 pm

      Stefanie – I think that you will have to use Ninja Forms for this. Product Enquiry Form with Ninja Forms gives code to add a drop down of product attributes. You could use this idea (i.e. populate an empty HTML element with markup) to show all products and allow users choose some of them. Maybe you could use a ‘select’ checkbox element that allows multiple items to be selected and your code will create multiple ‘option’ elements.

      You will also have to write a small bit of code to query all products to create those ‘option’ elements.

      I am on holiday at the moment so I cannot try to write some code for you.

      Reply
  11. Siti Muti'ah says

    September 12, 2018 at 7:36 am

    Hi! Good day! i have test the form but the product name not appear. kindly assist. thanks

    Reply
    • damien says

      September 17, 2018 at 9:39 am

      Siti emailed me screenshots of the form and email setup. The form had a small typo: [dynamichidden product-name "CF7_get_post_var key='product-name'"] The “key” should have been ‘title’ instead of ‘product-name.’

      Reply
  12. RwkY says

    October 3, 2018 at 11:05 am

    For anyone wanting to show the url of the product in a product loop, here’s what I’ve used.

    [site-url]?p=[post-id] because permalink is not available in $post, I used the basic query string :)

    Reply
    • damien says

      October 3, 2018 at 11:26 am

      @RwkY: Use: [dynamichidden page-url “CF7_URL”] in the form and [page-url] in the mail.

      Reply
  13. Andrés says

    October 20, 2018 at 9:35 am

    Hi Damien! Amazing post and one great extension for CF7.

    And if we need send pictures hidden of one product? I mean, I was able to send the title, but I would like can to send the pictures of product.

    I have only been able to send via url, but the images are not attached, it is just a link.

    Have a nice weekend!!

    Reply
    • damien says

      October 20, 2018 at 4:37 pm

      @Andres: What are you trying to achieve? Why is a link to the image not okay? Could you use the url inside an ‘img’ tag in the email? This should display the image in your email.

      Reply
      • Andr'es says

        October 20, 2018 at 5:24 pm

        Hi Damien, thank you for your attention. This is the code that I used on function . php add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' ); function woo_new_product_tab( $tabs ) { // Adds the new tab $tabs['desc_tab'] = array( 'title' => __( 'Pedir Información', 'woocommerce' ), 'priority' => 50, 'callback' => 'woo_new_product_tab_content' ); return $tabs; }

        function woo_new_product_tab_content() { // The new tab content echo do_shortcode('[contact-form-7 id="150" title="Producto"]'); }

        function cf7_add_image_url(){ $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); return $feat_image; } add_shortcode('CF7_ADD_IMAGE_URL', 'cf7_add_image_url');

        Then, CF7 -> Module Tab: [dynamichidden product-name "CF7_get_post_var key='title'"]

        Tu nombre (requerido) [text* your-name] Tu correo electrónico (requerido) [email* your-email] Asunto [text your-subject] Tu mensaje [textarea your-message]

        [submit "Enviar"]

        [dynamichidden featured-img "CF7_ADD_IMAGE_URL"] And Email Tab: De: [your-name] Asunto: [your-subject] Producto: [product-name]

        Cuerpo del mensaje: [your-message]

        I followed your advice with the img tag, but the result is the following, attached picture.

        https://ibb.co/mABfLf

        Pd. Do you want to share the link of the store?

        Regards!

        Reply
      • Andres says

        October 20, 2018 at 5:33 pm

        Sorry for duplicating the post, what I’m trying is that when the email is sent, the images of the product are attached. But I have only managed to get a url of a photo, but it has to be attached. So the one who receives the email can see what files there are, without entering the link.

        Thank you so much.

        Reply
        • damien says

          October 20, 2018 at 9:23 pm

          @Andres: I think that you will have to look at CF7 docs for file attachments: https://contactform7.com/file-uploading-and-attachment/ It mentions including the relative path to a file in the File Attachment section of the Mail tab. It only accepts files that are under the wp-content directory. This seems promising – I thought that you might be able to add `[featured-img]` to that box but when I read the CF7 code it looks like it doesn’t examine shortcodes in that box (see https://plugins.trac.wordpress.org/browser/contact-form-7/tags/5.0.4/includes/mail.php#L150 and how it skips over a line if it begins with a square bracket).

          If you wanted to dive into the code further, there is a filter (‘wpcf7_mail_components’ at https://plugins.trac.wordpress.org/browser/contact-form-7/tags/5.0.4/includes/mail.php#L107) where you could set the attachments but I think that it would be very difficult to know what file to attach – you might have to include the url in the body of your email and then parse it out in that filter. It seems very complicated.

          So, I don’t have a good solution here but I can think of a use case where this would be needed – imagine if you have a form to request a specific catalog. The reply would attach the requested catalog. I’ll make a note of this situation and see if it can be handled by CF7 or Ninja Forms.

          Reply
  14. Aleksey says

    November 26, 2018 at 6:20 pm

    Bro, awesome share! Helped a lot. Thanks! :)

    Reply
    • damien says

      November 26, 2018 at 9:19 pm

      Aleksey – You’re very welcome. It’s been a good challenge and a number of comments have challenged me to find solutions to a number of different ways of using the code.

      Reply
  15. Valentin says

    December 19, 2018 at 6:41 am

    Hello, Can anybody help me. I need to have enquiry form that is connect with the woocommerce order form product variables. The idea is when somebody want to make a quote before click submit the form ask him to make a choice of the size for the product. If not choose a size to see error in contact form 7 form. Can we do this?

    Reply
    • damien says

      December 19, 2018 at 1:53 pm

      Valentin – I am not 100% clear where the product form will be but I was not able to get product variations into a CF7 form. I had to use Ninja Forms. I wrote about this in May: https://www.damiencarbery.com/2018/05/product-enquiry-form-with-ninja-forms/

      Reply
    • Valentin says

      December 19, 2018 at 7:21 pm

      Thank you very much Damien. So I need to check with ninjaforms.

      Reply
  16. AnnoyingKitten says

    January 2, 2019 at 10:57 am

    YAY! IT WORKS! Thank you so much.

    Reply
    • damien says

      January 2, 2019 at 12:19 pm

      Thanks for commenting. I’m delighted that the post was useful for you.

      Reply
  17. fisnik says

    January 29, 2019 at 2:40 pm

    Hello i have tried your code and it works only in product-single page but i am trying this to get working on shop page where all the products are displayed and when i am using your code i get the shop page title and not the product name.

    Reply
    • damien says

      January 30, 2019 at 4:45 pm

      @fisnik and I exchanged a few emails. If the form is on the shop archive page then it will only capture the shop page title and not a product title because it does not know what product title to capture. It is easiest to only have the form on the single product page.

      Reply
  18. Ryan Lim says

    April 9, 2019 at 10:18 pm

    Hi there Damien, I managed to input my contact form on to a single product and fetch its [product-name], but for some reason, I am unable to automatically add the form to all products. I have tried inserting your code into functions.php but it doesnt seem to be showing up. Can you please help?

    Btw i absolutely just want to thank you for this amazing post, it helped me out alot! Now i just need to finish the last step.

    Reply
    • damien says

      April 9, 2019 at 11:46 pm

      @Ryan: What code are you putting into functions.php? Have you changed the form id?

      Another visitor put the form in a different location – between the photos and the tabs: https://pastebin.com/raw/4Qj6CagV

      Reply
  19. ray cruz says

    April 11, 2019 at 12:18 am

    How do we change the styling of the button? I just need to change the width.

    Reply
    • damien says

      April 11, 2019 at 10:18 am

      @ray: Use CSS to change the width of the submit button. You can use: .wpcf7-form input[type="submit"] { width: 300px; }

      Reply
  20. Getadwords says

    April 29, 2019 at 2:10 pm

    Hi….thanks for the above all.

    I did everything & works fine.

    But, I have put the contact form in a new tab, after Product Description Tab, so once we submit the form it’s coming back to First Tab i.e. Product Description…ideally it should stay there….to let user see the message..”Thank you for your message. It has been sent.”

    http://adaptinsights.com/product/global-graphite-sheet-market-by-type-natural-graphite-sheet-synthetic-graphite-sheet-nanocomposite-graphite-sheet-application-laptop-led-lighting-flat-panel-displays-others-region-key-manuf-3/#wpcf7-f180-p251-o1

    Waiting for your reply.

    Regards, GetAdwords

    Reply
    • damien says

      April 29, 2019 at 2:48 pm

      @GetAdwords – I submitted two tests of the contact form and in both cases it stayed on the ‘Request Sample’ tab and I saw the ‘Thank you for your message. It has been sent.’ message. I am using Chrome 73 on Windows 7.

      Reply
      • Getadwords says

        April 29, 2019 at 3:13 pm

        Thanks for the quick reply. I am using using Version 74.0.3729.108 (Official Build) (64-bit) on windows 8.1, tried again, still it’s coming back to Product Description tab.

        Also, just now I have tested on Mobile, the form is not getting submitted i.e. the icon beside the Submit Button keeps rolling. Can u please check that. https://prnt.sc/niasya

        Regards, Shridsan

        Reply
        • damien says

          April 30, 2019 at 10:58 am

          @Shridsan – I still cannot reproduce this behaviour. I have tried Chrome 74, Firefox Quantum 66,Opera 60 all on desktop and Safari on iOS 12.2. All of them leave me on the ‘Request Quote’ tab and I can see the ‘Thank you’ message. I cannot debug the issue because I do not experience it.

          For the error on mobile, can you see if there are any error messages in the server logs at that time? For the form on desktop, try putting the form on a standard WordPress page and see if it does anything strange. You can open the browser Console via Inspect Element to see if there are any error messages.

          Reply
          • Getadwords says

            April 30, 2019 at 5:53 pm

            https://drive.google.com/open?id=1Wo5_OdZYX0MtTUAgDuLS58_-DPHUE9sh

          • Getadwords says

            April 30, 2019 at 5:58 pm

            Error Message logs –

            [30-Apr-2019 16:55:00 UTC] add_to_cart_fragments is deprecated since version 3.0.0! Use woocommerce_add_to_cart_fragments instead. [30-Apr-2019 16:55:00 UTC] The WC_Cart::get_cart_url function is deprecated since version 2.5. Replace with wc_get_cart_url.

          • damien says

            April 30, 2019 at 8:32 pm

            Thanks for the video. That is so strange. I cannot explain what is going on. I do not have any idea how to fix that issue.

            Aside: The two items in the error log show that your theme should be updated. accessPress need to fix them because the theme will stop working at some point. The fixes are so simple!

  21. Jim Chet says

    May 23, 2019 at 12:14 pm

    Hi Is there a way to get the enquiry form to show quantity and product title. I know how to get the product title but not the quantity. THanks James

    Reply
    • damien says

      May 24, 2019 at 3:56 pm

      @Jim: You could add a number field to the contact form. It wouldn’t copy how many the customer had selected but it could prompt them to independently enter the quantity.

      You might be able to write some JavaScript that would watch the WooCommerce quantity field and copy the value into the CF7 number field. It might not be worth all the work, though use of IDs could make it robust.

      Reply
  22. Mar Luiz says

    June 12, 2019 at 4:47 am

    Hi there!

    Is possible get the current cart produts list and send it by e-mail?

    I try it but can’t find a solution! Already try enable the shortcode on CF7, but when I try send it, look like can load HTML.

    Thanks.

    Reply
    • damien says

      June 12, 2019 at 2:07 pm

      Mar – Thanks for your question. I have written some code that will list the cart contents in the email. https://gist.github.com/damiencarbery/fd02b001b1943651573d294dc9374a48#file-cart-contents-shortcode-php

      This will list the names of the products in the cart. It doesn’t include the price or quantity but you can write more code to do that. I got some of the code from the WooCommerce templates (woocommerce/templates/cart/mini-cart.php).

      Reply
      • Mar Luiz says

        June 12, 2019 at 8:38 pm

        I Damien!

        I tried and works perfect!!! Really, really, really thanks!

        Kind Regards.

        Reply
  23. softton says

    June 29, 2019 at 7:15 am

    i want to show the inquiry form on a particular category products, means show inquiry form on all product detail page that belongs to “category-one”. Please suggest me how i can do this.

    Reply
    • damien says

      June 30, 2019 at 12:09 pm

      @softton: You can use the has_term() function. It can take an array of category slugs. See: https://gist.github.com/damiencarbery/f7db36de7a875b6b9b4d10ff61597f60#file-cf7-to-product-page-specified-categories-php

      Reply
  24. Ajay Radadiya says

    July 11, 2019 at 8:57 am

    Contact form 7 woocommerce product dropdown field and Contact form 7 custom post type dropdown more good plugin

    Reply
    • damien says

      July 11, 2019 at 9:41 am

      @Ajay: Thanks for the links to your plugin. I watched the demo video and it looks very interesting. Best of luck with it.

      Reply
  25. Jens van den Wijngaard says

    November 1, 2019 at 1:18 pm

    Hi Damien,

    I’ve looked through this forum but still can’t figure it out on my own. So HELP!!!

    I’m trying to add the SKU to a contact form but it doesn’t seem to work. Product en aantal [dynamictext product-sku “CF7_get_custom_field key=’_sku'”] above is the code snippet i’m trying to use. I could send you some screenshots via email so the problem becomes more clear.

    I hope to hear from you soon.

    Reply
    • damien says

      November 29, 2019 at 6:40 pm

      Jens and I exchanged a *lot* of emails to fix this issue. The CF7_get_custom_field should have worked but the CF7 form was being loaded in a special tab so the custom field was not available. When I found this out I use a custom shortcode to retrieve the SKU and blogged about it.

      Reply
  26. WP tanfolyam says

    January 4, 2020 at 2:42 pm

    Damien, you have saved my life, a big THANKS to you! Thanks a lot!

    Reply
    • damien says

      January 4, 2020 at 4:05 pm

      Thanks for commenting. I’m glad that the code was helpful.

      Reply
  27. russell says

    January 8, 2020 at 6:39 pm

    Hi, Im thinking of using this as I need customers to send details of their order and then us contact to arrange artwork. we have three branches and what im also hoping to find somewhere, is the ability to have a dropdown box in the TO field of the contact form so that users can select which branch they send the email form to – can your plugin be adapted to do this – or got any other bright ideas? thanks x

    Reply
  28. damien says

    January 9, 2020 at 12:15 pm

    @russell: The ability to change the “To” field via a dropdown is already part of Contact Form 7. See: https://contactform7.com/selectable-recipient-with-pipes/ Use the second example, with pipes, so that you don’t expose the email address of the recipient (and to give a user friendly name for the branch).

    Reply
  29. Emilia says

    January 14, 2020 at 3:23 am

    Absolute life saver. Thank you

    Reply
    • damien says

      January 14, 2020 at 9:03 am

      @Emilia: I’m delighted that it helped you.

      Reply
  30. Rizve Joarder says

    February 22, 2020 at 6:56 am

    Hi,

    Thanks for this excellent tutorial. I am trying to make a product page with many variations. My shop is in catalog mode, and I have added a button for the query. When people click on that button, select variations, products name dynamically will show in the field on the popup form. Could you please tell me how I can display those options?

    Thanks

    Reply
    • damien says

      March 10, 2020 at 10:13 am

      @Rizve: What plugin or code are you using in the popup? How do the selected variations make it into the popup?

      Reply
      • damien says

        March 10, 2020 at 10:27 am

        Rizve and I exchanged a few emails. He’s using Popup Maker. It sounded like he found a solution for the popup.

        He asked an unrelated question: He disabled the ‘Add to cart’ button for all products though only wanted to disable it for some categories. I suggested using a has_term() check: https://rudrastyh.com/woocommerce/if-product-in-category.html

        Probably something like:

        $cats_to_hide_button = array( 'sneakers', 'backpacks' ); if( has_term( $cats_to_hide_button, 'product_cat' ) { // Code to hide button }

        I didn’t hear anything further.

        Reply
  31. Ptunder says

    March 9, 2020 at 7:59 pm

    Awesome tutorial. I’m trying to display a popup contact form and remove the add to cart for selected products above X€. How can I do that?

    Thanks

    Reply
    • damien says

      March 10, 2020 at 10:37 am

      @Ptunder: What popup plugin are you using?Do you have the contact form working inside the popup?

      Removing the ‘Add to cart’ button is a separate question.The button is added by the templates/loop/add-to-cart.php file. This is called by the woocommerce_template_loop_add_to_cart() function. This function is called by the 'woocommerce_after_shop_loop_item' action.

      I suggest writing code to run before the woocommerce_template_loop_add_to_cart() that would check the product price. If the price is over a certain amount then run:

      remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );

      Reply
  32. iqra says

    March 25, 2020 at 8:30 pm

    hello through your code we can directly add form in single product page , is it possible we add button “Enquiry Now” then by clicking the button . form is open in popup like this https://infinitistone.co.uk/styles/volcanic-grey/

    Reply
    • damien says

      March 26, 2020 at 11:27 am

      @iqra: I wrote about making the product enquiry form work in a CF7 popup. In that case it uses the WooCommerce Quote or Enquiry Contact Form 7 plugin.

      My code creates a custom CF7 shortcode to get the product SKU. To get the product name you can change:

      $product_sku = get_post_meta( $product_id, '_sku', true ); return $product_sku;

      to

      $product = wc_get_product($_POST[‘product_id’]); return $product->get_name();

      Reply
  33. Nancy says

    April 1, 2020 at 6:47 pm

    How do I add a form just to one product page?

    Reply
    • damien says

      April 6, 2020 at 11:10 am

      To display the form on only 1 product you can add another conditional to the function. To make it look like:

      add_filter('the_content', 'dcwd_cf7_form_to_a_product'); function dcwd_cf7_form_to_a_product($content) { if ( class_exists( 'woocommerce' ) && is_product() && is_main_query() ) { if ( is_single( 'parka' ) ) { // Use product slug for readability. return $content . '[contact-form-7 id="27" title="Product Enquiry"]'; } } return $content; }

      Reply
  34. Gerben says

    April 25, 2020 at 5:37 pm

    Hi, I want to add this “ask a question” to my online store. I installed the plugin CF7 and Contact Form 7 Dynamic Text Extension plugin. I have the button “ask a question” on my product pages. When I click on the button I get a page error 404.

    Im not sure what I must do. Must I create a new form? Must I create a new page?

    Please advise?

    Reply
    • damien says

      May 6, 2020 at 1:04 pm

      I exchanged a few emails with Gerben but could not get the answer about how the “Have a question?” button is added to the single product page.

      Reply
  35. LuisG says

    July 19, 2020 at 4:14 pm

    Hi, I am working on a project to create a car dealer, it will have different brands (manufacturers) such as Ford, Chevrolet, Dodge, etc. But I need to create a different request form for each brand, for example, if the user is interested in a Ford Mustang, the system sends the form to the person in charge of Ford, and the same for each brand. I would like to do it dynamically, could you please give me some idea of ​​how to do it. Thank you

    Reply
    • damien says

      July 20, 2020 at 1:14 pm

      @LuisG: I just cannot figure out how to do this.

      Contact Form 7 does support a dropdown where you can use the pipe symbol to hide the value and use this to add multiple email addresses and use the selected one as the email recipient. Some other thoughts:

      • If you don’t mind someone finding the email address you could use a [dynamichidden] that would be set to the appropriate person based on the car’s brand. There’s a small chance that someone could use the form to send emails to someone outside the dealership.
      • You could create a different contact form for each brand. They would be identical except for the recipient.You could dynamically add the correct form to the car page – your code could have a list of the form IDs and determine the brand and call do_shortcode() with the right one.
      • Create a dropdown as mentioned in the CF7 page. Make it hidden (e.g. give it a class that will hide it).Write some Javascript that will determine what brand the page is on (this could be done in PHP and JS code created on the fly) and the JS will select the appropriate item from the dropdown.
      • There is a CF7 filter called ‘wpcf7_mail_components’ that is called just before an email is sent.In combination with a [dynamichidden] field (which would have the car brand in it) it would read that info and change the recipient email address appropriately.
      Reply
  36. Irfan Ali says

    July 24, 2020 at 5:37 pm

    Hi, Great post, helped me a lot. I just want to know how can i call product category in contact form 7, I searched a lot but didn’t find anything. Thanks

    Reply
    • damien says

      July 25, 2020 at 1:30 pm

      Irfan: You need a custom shortcode like I wrote about in Shortcode for Contact Form 7 Dynamic Text Extension.

      For product categories here is the code:

      add_shortcode( 'product_cats', 'dcwd_product_categories_for_cf7' ); function dcwd_product_categories_for_cf7() { // Strip the html tags to get a plain comma separated list. return strip_tags( wc_get_product_category_list( get_the_ID() ) ); }

      In the Contact Form 7 Form you add:

      [dynamichidden product-categories-from-shortcode "product_cats"]

      In the Contact Form 7 Mail tab you add:

      Product categories: [product-categories-from-shortcode]

      Reply
  37. Tony Poulos says

    December 6, 2020 at 2:27 pm

    I have set up a Product Enquiry form for Woo Commerce and it is working fine but is it possible to have the Product Enquiry to appear after the Price? It currently appears *before* the Product Description and SKU.

    Reply
    • damien says

      December 8, 2020 at 9:07 am

      @Tony: Have you changed the product page layout with Divi?

      The default layout is – Product title/name – Price – Short description (and the CF7 form is added to the bottom of this) – Add to cart button – SKU – Categories & tags For example: https://themes.woocommerce.com/storefront/product/stainless-steel-professional-blender/

      It looks like my code is adding it to the short description but that is displayed *before* the price and SKU on your site.

      Reply
  38. Oleg says

    December 11, 2020 at 9:19 am

    Hello, how can i send to mail the title of variation?

    Reply
    • damien says

      December 11, 2020 at 1:35 pm

      @Oleg: The form is created when the page loads so it does not know what variation the customer will choose.

      You might be able to write JavaScript to put the variation into a hidden field in the form. The JavaScript would have to watch the variation fields and read them every time they are changed. Then it would put the information into the hidden field.

      This is not easy.

      Reply
  39. WP weboldal készítés says

    February 9, 2021 at 6:59 am

    Hi, brilliant article! It is in my bookmarks and I have used it three times! Thanks again!

    Reply
  40. aftab iqbal says

    February 26, 2021 at 11:48 am

    [dynamictext product-name “CF7_get_post_var key=’title'”]

    who can we add textarea

    Reply
    • damien says

      February 26, 2021 at 4:45 pm

      @aftab – I do not understand what you mean when you ask where to add textarea.

      Do you want to add a textarea field to the form? You can do that when editing the form.

      Reply
  41. aftab iqbal says

    March 1, 2021 at 5:21 am

    dynamictext same as a dynamictextarea we want to use but no have any option available

    Reply
    • damien says

      March 1, 2021 at 9:00 pm

      @aftab – The CF7 Dynamic Text Extension plugin does not have a ‘dynamictextarea‘ option.

      The docs say: The plugin does not currently support textareas, radios, selects, or other form field types

      You could submit a request to the developer to suggest ‘dynamictextarea‘

      Reply
  42. Naveen says

    May 26, 2021 at 2:46 pm

    Great tutorial Damien. Thank you for this. I just have one problem. When my description field is empty, the form is not displayed. It only displays when there is some other data also in the Description tab.

    Reply
    • damien says

      May 27, 2021 at 12:42 pm

      @Naveen: WooCommerce does not display the “Description” tab if the description is empty. I have updated the code to ensure that the “Description” tab is always shown: wc-cf7-form-to-all-products-php

      Please tell me if that fixes the issue.

      Reply
  43. Roberto formosa says

    July 1, 2021 at 8:06 am

    Hi I’m still having issues to display the Dynamic Text – still getting the dynamictext field blank.

    Reply
    • damien says

      July 2, 2021 at 11:34 am

      @Roberto – As you are using the ‘Get a Quote Button for WooCommerce‘ plugin you need different code. My Shortcode for Contact Form 7 Dynamic Text Extension post has the code that can get the sku or name.

      Reply
  44. Bira says

    September 28, 2021 at 7:04 pm

    Hi Damien, Excellent material! I’ve changed Divi Forms to CF7 on a website I’m developing because I couldn’t find a way to setup conversions in Google Analytics, and with your plugin I could use CF7 that’s free and working perfectly for all 140 products pages. Question: this website has about 40 categories pages and I’m using then as landing pages, so I want to insert contact forms automatically in the description of theses pages too, do you think it’s possible?

    Reply
    • damien says

      October 3, 2021 at 5:45 pm

      @Bira – Use the following code to add it to the top of all product category pages (after the category description, if it is set):

      add_action( 'woocommerce_archive_description', 'dcwd_cf7_to_product_archive', 20 ); function dcwd_cf7_to_product_archive() { // Don't display CF7 form on search page. if ( is_search() ) { return; }

      if ( is_product_category() ) { echo do_shortcode( '[contact-form-7 id="8018" title="Category Enquiry"]' ); } }

      Reply
  45. Joel George says

    March 26, 2022 at 7:42 pm

    Damien, Hey. I am new to web development. I’ve set up a motel website using WordPress. I had set up a page with 2 different room types where customers can click the “book now” button and be redirected to a contact form 7 reservation form. I was able to receive all the other user details except for the product title or in my case the “room type”. I was able to follow through with all of your instructions but I do not know how to link my rooms as WooCommerce products. Unfortunately for this reason I cannot proceed with this method. Could you kindly spare me a second to explain to me how to set up a WooCommerce products page using my already existing rooms catalog page which I had set up earlier? Please help me out since my research on google and YouTube has me worried I will need to spend 250 USD to buy the WooCommerce extension for Hotel Bookings for me to set up the form. Kindly reach out to me as quickly as possible.

    Reply
    • damien says

      March 27, 2022 at 12:37 pm

      @Joel – I have two ideas: 1) Make two versions of the Reservation page e.g. Reservation (for the cheaper room) and Premium Reservation.You can create two versions of the CF7 form and change the email you receive to say which page the email came from. I think that this is the simple version.

      2) You can use the CF7 Dynamic Text Extension plugin. Then change the Book Now buttons to add to the link e.g. /reservation?room=1br and /reservation/?room=premium

      Then the CF7 form will use a Dynamic Text item with a value of: CF7_GET key=’room’ The shortcode will look like:

      [dynamictext roomtype “CF7_GET key=’room'”]

      and the email will have:

      Room type: [roomtype]

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Blog Categories

  • Being Green
  • Food
  • Grammar
  • Just Strange
  • Laziness
  • Personal
  • Portfolio
  • Race Reports
  • Shudder
  • Typo
  • Website Development
  • WooCommerce Tips
  • Facebook
  • Twitter

Copyright © 2013–2023 Damien Carbery · Using Genesis Framework · Privacy Policy