• 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 / Shortcode for Contact Form 7 Dynamic Text Extension

Shortcode for Contact Form 7 Dynamic Text Extension

Published: November 11, 2019 (Updated: August 27, 2023)

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

Update: After a query by Irfan about product categories I have added another shortcode function to return a simple comma separated list of the product’s categories.

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 did not redo the screenshots.

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

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.

Update: Another comment encountered similar issues. He is using the Get a Quote Button for WooCommerce plugin. Like ‘WooCommerce Quote or Enquiry Contact‘ plugin it uses $_POST variables – I just had to find the right one. I have rewritten both shortcode functions to work with both plugins as well as an in-page form.

The code

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.

Usage

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:

CF7DTE with product_sku shortcode

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

SKU: [sku-from-shortcode]

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.

Shortcode for Product Categories

In a comment on another post Irfan asked about adding product categories to the email. This requires another shortcode – [product_cats].

In the Contact Form 7 Form you add:

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

In the Contact Form 7 Mail tab you add:

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

Shortcode for Product title

Jas asked about getting the product name when using “Get a Quote Button for WooCommerce“. Here you go:

In the Contact Form 7 Form you add:

[dynamic_hidden product-name-from-shortcode "product_name"]

In the Contact Form 7 Mail tab you add:

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

Related Posts

Product Enquiry form using WooCommerce and Contact Form 7

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.

Product Enquiry form with Ninja Forms

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

Product Enquiry form for Variable WooCommerce Products

A product enquiry form for a variable product needs a dynamically configurable field - CF7 cannot deliver this but Ninja Forms is flexible.

Reader Interactions

Comments

  1. Andrea Guerra says

    November 29, 2019 at 12:54 pm

    Hi, if instead you wanted to take the image of the product and the short description of the product, how to create the shortcode, could you kindly help me?

    Reply
    • damien says

      November 29, 2019 at 6:33 pm

      @Andrea: I wrote code to attach the product image to the email that is sent. Would that work for you?

      The short description is stored in the post_excerpt field so this will work in the form: [dynamictext short-description “CF7_get_post_var key=’post_excerpt'”] then put this in the email: Short description: [short-description] You can change ‘dynamictext’ to ‘dynamichidden’ if you don’t want people to see the field and edit it.

      Reply
      • Andrea Guerra says

        November 29, 2019 at 8:18 pm

        Hi and thanks for your reply.

        For the image I followed your form and copied the two codes in the function file and in the module. But when I put this [dynamichidden product-id “CF7_get_post_var key = ‘ID'”] like in the code below that I report you in full, gives me these errors: Found 1 configuration error in this tab Multiple controls of the module are placed in a single label element.

        Car model [dynamichidden product-id “CF7_get_post_var key = ‘ID'”] [dynamictext dynamicname readonly product-name “CF7_get_post_var key = ‘title'”] [dynamichidden product-sku “CF7_get_custom_field key = ‘_ sku'”] [dynamichidden product-price “CF7_get_custom_field key = ‘_ price'”]

        [dynamictext short-description “CF7_get_post_var key = ‘post_excerpt'”] Name and Surname *     [text * your-name]

        Your e-mail *     [email * your-email] [dynamichidden page-url “CF7_URL”]

        [submit “Send”]

        For the short description I entered this: [dynamictext short-description “CF7_get_post_var key =’ post_excerpt ‘”]

        but in the mail I find the code written like this:

        <g transform = " etc etc..

        Reply
  2. DevOps says

    January 20, 2020 at 6:38 am

    Hi, Your post and all article are awesome.

    I am trying to get the Product name in the Popup contact form 7 with the using “Shortcode for Contact Form 7 Dynamic Text Extension”. But in the single product page Contact form 7 able to get the product title, but in the category list page only getting one product title in every product.

    // Request_a_callback_woocommerce_product_archive for category and single page button add_action( 'woocommerce_after_shop_loop_item', 'request_a_callback_woocommerce_product_archive', 35 ); function request_a_callback_woocommerce_product_archive() { global $product; if ( is_product_category() ) { if(!empty(get_post_meta( $product->get_id(), 'product_button', true ))){ echo '<span class="product_button">';?> <a class="product_button_type_buynow" href="">Buy Now</a> <?php echo '</span>'; } else { echo '<a href="void(0)" rel="nofollow ugc">'; echo '<span class="product_button_type_reback">'; echo 'Request a Callback'; echo '</span>'; echo '</a>'; } } }

    add_action( 'woocommerce_product_meta_end', 'woocommerce_show_product_sale_flash', 10 ); function woocommerce_show_product_sale_flash() { global $product; if(empty(get_post_meta( $product->get_id(), 'product_button', true ))){ echo '<a href="void(0)" rel="nofollow ugc">'; echo '<span class="product_button_type_reback">'; echo 'Request a Callback'; echo '</span>'; echo '</a>'; } }

    =============================

    Contact form 7

    Product Name

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

    Reply
    • damien says

      January 21, 2020 at 10:26 am

      What plugin or code are you using for the popup? Is there a url that I can look at? On the archive page you will need to look for a way to pass the product title or ID to the popup. If I saw the code you are using then there might be a way to send the product title or ID.

      Reply
  3. Anthony Dirisu Jr says

    May 13, 2020 at 10:43 am

    Where exactly do I write the code for the function. I’m new to WordPress and php and it’s kind of difficult locating where I am supposed to place the function code before I can use the shortcode in the form. Please reply soon

    Reply
    • damien says

      May 13, 2020 at 12:05 pm

      @Anthony: I made a video of how to use my code snippets that should help you.

      Reply
  4. priyam pathak says

    July 7, 2020 at 4:09 pm

    Helo,I made use of this plugin for my website and thanks for this information. But my pop up contact form is not submitting,it keep saying validation error. This is the link https://thaireefer.co.th/product/coupling-1-2-part-no-1540090/ if you could just check and tell what is causing this error will be very helpful.

    Reply
  5. damien says

    July 10, 2020 at 12:47 pm

    priyam and I exchanged a few emails. We found a bug in WooCommerce Quote or Enquiry Contact Form 7 plugin where does not work when a page has 2 CF7 forms. priymam submitted a bug report and I added to it.

    Reply
  6. anoop says

    January 28, 2021 at 2:19 pm

    Not getting value of products like sku etc.. even product id does not get in pop up form when click on quote button.

    Reply
    • damien says

      January 29, 2021 at 10:59 am

      I found out the issue. The Get a Quote Button for WooCommerce plugin is similar to the WooCommerce Quote or Enquiry Contact plugin – it uses $_POST variables. I find the right one and I have rewritten both shortcode functions to work with both plugins as well as an in-page form.

      Reply
  7. Richard says

    February 2, 2021 at 12:06 pm

    Just wanted to give you my best thanks for this very helpful tutorial and plugin. I managed to achieve the result I wanted thanks to you.

    Reply
  8. jas says

    March 15, 2021 at 7:19 pm

    Damien: appreciate you and all the knowledge you share :-)

    Im using Woocommerce Get A Quote plugin – and want to dynamically gather product name and sku and populate in a CF7 form. I know my form is correct because I can get those parameters in other popup plugins – but wont pass them in Get A Quote Button.

    Will any of your code give me a work around for this? I also see the Get A Quote CF7 form will not close after it is submitted?

    Help!!! Im a little bit beyond newbie here but need a plug in or snippet set up?

    many thanks

    Jas

    Reply
  9. jas says

    March 15, 2021 at 9:08 pm

    Damien used your plug in and now Get a Quote and Contact form 7 play nice together dynamically – get the sku and product id – but my form submits says sent and stays open?? how do I get i to close – no parameters of the like in contact 7 or get a quote button

    thanks JAS

    Reply
  10. jas says

    March 15, 2021 at 10:32 pm

    Damian: forgot to ask – i was able to overcome the bug in Woocommerce Get A Quote Button and CF7DTE with your sku and product category short code – but i simply need one to pass the product name (as the dynamic text version doesnt work with Get A Quote Button plugin – can you point me accordingly?

    Many thanks

    Reply
    • damien says

      March 16, 2021 at 2:46 pm

      @jas: Thanks for the questions. To get the product name I wrote another shortcode function.

      In your form: [dynamichidden product-name-from-shortcode "product_name"]

      In the Contact Form 7 Mail tab: Product name: [product-name-from-shortcode]

      Reply
  11. Artem says

    July 22, 2021 at 8:12 am

    Incredible article, thank you very much. Very helpfull. But i still have some questions(. Is it real to get options from variants and put them in popup form with Woocommerce Get A Quote Button plugin?

    Reply
    • damien says

      August 2, 2021 at 6:04 pm

      @Artem – I tried a number of things to fix this but nothing worked. When the Enquiry button is clicked the form elements are cleared. I had written JavaScript code to watch the variations dropdowns and copy their value into a text field in the form but this was cleared when the popup shown.

      If the plugin developer sent a trigger when the popup was opened then the trigger function could copy the variations dropdowns value again.

      Reply
  12. Devilfunk says

    March 13, 2022 at 7:41 pm

    hello, i am using it since long time but since last update i started getting error in getting product price on product page

    [dynamichidden product-price “CF7_PRODUCT_PRICE”] giving this on dashboard CF7_PRODUCT_PRICE

    for further details please have a look at web url is https://bit.ly/3tUMbXw

    and here is plugin and other error screenshots to check if there is any other issue gdrive url https://drive.google.com/drive/folders/1_7kcFBKbcEkRAAQE6PoInTl5dOBLgHhW?usp=sharing

    Thank you

    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