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.
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):
Not sure what to do with code snippets? See What Do I Do With These Code Snippets?
Leave a Reply
You must be registered and logged in to post a comment.