Populate Field with Woocommerce Product Variation SKU via Merge Tag [RESOLVED]

Hi There!

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.

Is there a way to accomplish this in my form?

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.

Once you have that information, you can use the following filter to run your code to return the SKU value to the field before saving it to the database: gform_save_field_value - Gravity Forms Documentation

I think I have the code to get the variable product SKU, just not entirely sure how to implement it.

$sku = get_post_meta( $item['variation_id'], '_sku', true );

So would I need to use the Process Merge Tags filter in this case?

add_filter( 'gform_save_field_value', 'replace_merge_tags', 10, 4 );
function replace_merge_tags( $value, $entry, $field, $form ) {
    $value = GFCommon::replace_variables( $value, $form, $entry );
    return $value;
}

Actually, I determined I need to use the this filter:

add_filter( 'gform_field_value_email', 'your_function_name' );

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.

add_filter( 'gform_field_value_variable_sku', 'populate_product_variable_sku' );
function populate_product_variable_sku( $value ) {
	$sku = get_post_meta( $item['variation_id'], '_sku', true );
	return $sku;
}

Not sure if its because I need to wait to execute this filter until the product variation has been selected or not.

I know there isn’t official support for woo here, but maybe someone in the community might have an idea.

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.

Got this idea from the old forum: Use jQuery to fill in fields. « Gravity Support Forums

jQuery("#field_id").val('Custom value here');

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);
    });
});

Thank you for sharing that update.