WooCommerce, Remote Work & Other Fancy Stuff

Just another WordPress site

Programmatically Add Gravity Form to WooCommerce Product

February 11, 2016 by Will 2 Comments

Please consider leaving a donation if you appreciate this information. Lightning network now available

The WooCommerce Gravity Forms Product Add-ons extension allows you to add some very powerful customization options to your WooCommerce products. However, the form must be added on a per-product basis. This works fine for most use-cases but there are cases where it may be desirable to attach the form to all products or to all products in a given category. This can be done dynamically with a little help from the WordPress provided filter named get_post_metadata here. When you add a Gravity Form to your WooCommerce product, then information is stored in the post meta for that product so by using a filter, it is possible to ‘short circuit’ the meta retrieval function and return something custom. Any non-null return value will short-circuit the function.

Before you get started on the code, however, you will need the serialized string that is stored in the database. Take these steps:

  1. Attach a form to one of your products – documentation here
  2. via phpMyAdmin (or the Adminer plugin) find the product meta in the beardedguy_postmeta table – copy the value for the _gravity_form_data key.
  3. In the snippet below, replace YOUR_SERIALIZED_STRING with the serialized string that you copied from the database (leave the single quotes in place).
  4. That’s it – the same form will now be attached to all your products.

That code can be tweaked if needed to add conditional logic to check against a product category or other conditions as appropriate.

Not sure what to do with code snippets? See What Do I Do With These Code Snippets?

Please consider leaving a donation if you appreciate this information. Lightning network now available

Filed Under: Gravity Forms Product Add-ons, WooCommerce Tagged With: snippet, snippets

Comments

  1. cnunez93 says

    March 5, 2021 at 8:37 pm

    Hi, thank you for the code I was driving crazy searching over the internet a way to link the form to all my products. Sadly I think I’am doing something wrong because I can’t get your code to work.
    I don’t know how to code so I just follow your instructions, I only replace YOUR_SERIALIZED_STRING with the key that I found on my database that looks like this:

    ‘a:28{s:2:”id”;s:1:”4″;s:7:”bulk_id”;i:0;s:13:”display_title”;b:0;s:19:”display_description”;b:0;s:25:”disable_woocommerce_price”;s:2:”no”;s:12:”price_before”;s:0:””;s:11:”price_after”;s:0:””;s:20:”disable_calculations”;s:2:”no”;s:22:”disable_label_subtotal”;s:3:”yes”;s:21:”disable_label_options”;s:3:”yes”;s:19:”disable_label_total”;s:2:”no”;s:14:”disable_anchor”;s:2:”no”;s:14:”label_subtotal”;s:8:”Subtotal”;s:13:”label_options”;s:7:”Options”;s:11:”label_total”;s:5:”Total”;s:8:”use_ajax”;s:2:”no”;s:16:”enable_cart_edit”;s:2:”no”;s:23:”enable_cart_edit_remove”;s:3:”yes”;s:17:”keep_cart_entries”;s:2:”no”;s:18:”send_notifications”;s:2:”no”;s:31:”enable_cart_quantity_management”;s:2:”no”;s:19:”cart_quantity_field”;s:0:””;s:22:”update_payment_details”;s:3:”yes”;s:23:”display_totals_location”;s:5:”after”;s:24:”structured_data_override”;s:2:”no”;s:25:”structured_data_low_price”;s:0:””;s:26:”structured_data_high_price”;s:0:””;s:29:”structured_data_override_type”;s:6:”append”;}’

    that is ok or I’am doing something wrong, please help me!

    Thanks!

    Log in to Reply
    • Will says

      March 7, 2021 at 9:47 pm

      It looks to me like Gravity Forms Product Add-ons has changed the way it works so this method is no longer viable.

      Log in to Reply

Leave a Reply Cancel reply

You must be registered and logged in to post a comment.

© 2025 ยท WooCommerce, Remote Work & Other Fancy Stuff · Epstein didn't kill himself

We use cookies to ensure that we give you the best experience on our website. Continued use of this site is interpreted as your acceptance of our cookie policy.OkSee Our Cookie Policy