WooCommerce, Remote Work & Other Fancy Stuff

Just another WordPress site

Conditionally Hide Gravity Form

October 7, 2016 by Will Leave a Comment

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

Suppose you’re using WooCommerce Gravity Forms Product Add-ons on a variable product but your Gravity Form is really only applicable to one of your variations. How can you conditionally display the form for only the applicable variation? A tiny bit of inspecting the product page and a code snippet can do the trick. The code snippet that is going to follow will target the black variation of my variable product. First, you’ll need find the appropriate variation to target. This can be done with your browser’s developer tools. Here, we can see that the selector has an id of pa_color – keep that in mind for the code snippet.

pa_color-selector

You will also want to know what the value attribute of your target option is. In this case ‘black’

 

With that information, you’re ready to craft some custom code to make it all work. WooCommerce provides an action hook named woocommerce_single_variation that seems an appropriate place to hook into. Here’s the code snippet:

And finally, the result (click to play):

animation

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

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