CF7DTE is great but sometimes you need a custom shortcode to get product info into the form.
The Product Enquiry form using WooCommerce and Contact Form 7 post is the most popular post on my site. While the scenario I presented was quite simple, a number of comments have asked about different use cases or scenarios and I have updated the post a number of times to include the solutions for them. A recent scenario required the use of a custom shortcode.
Dynamic Text Extension is brilliant
The product enquiry form in the original post was only possible because of the Contact Form 7 Dynamic Text Extension (CF7 DTE) plugin. With it you can access a lot of post information e.g. title, slug, url, ID and even custom fields. I used this featured to include the product SKU in the form.
CF7 in a popup
Using the custom field feature to retrieve the product SKU has worked very well but a recent comment said it wasn’t working. When Marco gave me access to his site I saw that he was using the WooCommerce Quote or Enquiry Contact Form plugin. It replaces the “Add to Cart” button with an “Enquiry” button that displays the CF7 form in a popup using ajax.
By loading the CF7 form with ajax the SKU custom field of the product could not be accessed with the standard CF7 DTE feature. Luckily the plugin can access $_POST variables and it supports standard shortcodes.
I installed and activated the WooCommerce Quote or Enquiry Contact Form plugin. A quick search of the source showed me that product ID was passed via $_POST[‘product_id’]. I wrote a tiny shortcode function to access this and use it to retrieve the product SKU. A nice and simple solution.
Small note: I could have used wc_get_product($_POST[‘product_id’]) and the get_sku() member function but directly accessing the custom field was a quick solution.
When you activate the plugin above you can then add a new field to the contact form. The plugin provides a new shortcode, [product_sku].
Edit the form and either click the ‘dynamic text‘ button and enter the Name and Dynamic Value as shown in the screenshot or just add the following to the form:
[dynamictext sku-from-shortcode "product_sku"]
“product_sku” is the shortcode (without the square brackets) and the “sku-from-shortcode” is the name that will be used in the CF7 mail as follows:
Asking a good question
I exchanged a few emails with Marco with some debugging suggestions. It was a number of emails before the quote/enquiry plugin was mentioned. I would have solved the issue very quickly if I’d known about that at the start.
Like I said in my Debugging – Figuring it out yourself talk at WordCamp Dublin 2019, it’s important to include plenty of information when you ask others for help.