So I have a form hooked into my Woocommerce Single Product template that acts as an Inquiry form. I have the form setup so that the Name of the product is populated in a specific field. I also have a field where the product SKU can be populated using the merge tag {custom_field:_sku}. This is important because the SKU tells the store owner what combo the customer has requested info about.
The SKU population works just fine on a simple product, but it doesn’t work on a variable product. As far as I can tell, variable products don’t have a specific meta key for the SKU, and any code snippets I’ve found where someone was trying to get the sku of a variable product, they had to check first if the product was a variable_product, then grab the sku.
Gravity Forms doesn’t provide any built-in integration for WooCommerce, so you will want to reach WooCommerce support first to know how to obtain the SKU for your variable product using code.
But still having trouble getting the sku of that variable product. The filter works fine when returning a simple text string, but not with my $sku variable.
So I ended up using jQuery to grab the sku # off of the page (woocommerce already displays this after the variable product is configured) and placed it inside the text field using the field id.
There is also a function i added that only executes the script when the Purchase button is clicked, that way the sku text can be generated on the page first.
jQuery(document).ready(function($){
$("#wcd_openModal").click(function(){
var sku = $("#product-variation-sku").text();
$("#input_7_15").val(sku);
});
});