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
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
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:
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.