Home Forums General Issues ACF – WooCommerce – Product Variation


ACF – WooCommerce – Product Variation

  • ACF – WooCommerce – Product Variation

    Hello, I like this plugin a lot. On my website I’m using WooCommerce, where I have some variable products. I also installed the plugin: ACF. On the configuration of the ACF plugin I selected for the field location: “Post Type” “is equal to” “product_variation”. But on the product variation configuration, the custom field doesn’t show up. In the other hand, if I do: “Post Type” “is equal to” “product”, then the custom field shows up. Is there any version of the ACF plugin (or extension) where the custom field shows up on the product variation?, maybe the premium version or extension?

    I tried one suggestion from a post 5 years back but it didn’t work:

    Probably because that suggestion is for a previous version.

    Any idea on how to achieve this?: “custom field on a WooCommerce product variation”?

    Thanks a lot for your attention.

  • Need this as well, following this topic

  • We were also using that old script, but updates have broken it for us as well. We used this to display extra info on invoices and sort the order of products on our invoices. We have about 2000 unique items, most of them colour variations of eachother, we need this functionality back asap or to revert to an older version of ACF.

  • Okay I have a pretty crispy workaround, but it works.
    I am using ACF Pro 5.7.11

    In the old thread find the first reply by shaun where he has a code block. Here’s the link:

    Use that code in functions.php or wherever.

    In ACF setup whatever custom fields you need, but in the rule select product instead of product_variation because that option isn’t present.

    Go to the ACF Tools page and export the .json of your custom fields. Open that file with a text editor and scroll down to the “locations” section. Simply change the “value” from “product” to “product_variation” and then on that same tools page re-upload the file. You should see custom fields in your product variations drop downs now on variable products.

  • This piece of code work for me, setting the location to “Taxonomy equals <<whatever attribute>>”. Note that, as variations are loaded dynamically, javascript events are not attached to fields. Thus, using the gallery or image does not work: click on buttons does nothing. Trying to find a fix…

    $GLOBALS['wc_loop_variation_id'] = null;
    function is_field_group_for_variation($field_group, $variation_data, $variation_post) {
    	return (preg_match( '/Variation/i', $field_group['title'] ) == true);
    add_action( 'woocommerce_product_after_variable_attributes', function( $loop_index, $variation_data, $variation_post ) {
    	$GLOBALS['wc_loop_variation_id'] = $variation_post->ID;
    	foreach ( acf_get_field_groups() as $field_group ) {
    		if ( is_field_group_for_variation( $field_group, $variation_data, $variation_post ) ) {
    			acf_render_fields( $variation_post->ID, acf_get_fields( $field_group ) );
    	$GLOBALS['wc_loop_variation_id'] = null;
    }, 10, 3 );
    add_action( 'woocommerce_save_product_variation', function( $variation_id, $loop_index ) {
    	if ( !isset( $_POST['acf_variation'][$variation_id] ) ) {
    	$_POST['acf'] = $_POST['acf_variation'][$variation_id];
    	acf()->input->save_post( $variation_id );
    }, 10, 2 );
    add_filter( 'acf/prepare_field', function ( $field ) {
    	if ( !$GLOBALS['wc_loop_variation_id'] ) {
    		return $field;
    	$field['name'] = preg_replace( '/^acf\[/', 'acf_variation[' . $GLOBALS['wc_loop_variation_id'] . '][', $field['name'] );
    	return $field;
    }, 10, 1);
  • @exnihilo_maxime Nice, though I am getting an error. acf()->input is null. Should that be acf_save_post($variation_id) instead?

  • @exnihilo_maxime I can add the fields but I cant save the data that I put in them. I just get an infinite spinner and the bellow error in the console:

    wp-admin/admin-ajax.php 500 (Internal Server Error)

    Any suggestions would be really appreciated.


  • @gila-monster Thanks for you advice.

    My fields are displaying in the back end under my product variations and the data is saving correctly but I cannot work out how to show the fields on the front end of my website?

    Do you display them in the same way as you normally would ACF fields? e.g.
    <?php the_field(‘text’); ?>

  • John, and those struggling to piece together the different parts of this solution together, Here is how to display it on the ‘single-product.php’ page.

    Add the following, or similar, function to ‘functions.php’.

    /* Displays ACF fields 'Frame Type' and 'Lens Type' 
    /* Displays on SINGLE PRODUCT page and QUICK VIEW via archive
    /* DEPENDENCIES: ACF Custom Fields
    /* $variations are outputted by "variation.php"
    function frame_style_lens_colour ( $variations ) {
    	$variations['frame_type'] = get_field('frame_type', $variations[ 'variation_id' ]);
    	$variations['lens_type'] = get_field('lens_type', $variations[ 'variation_id' ]);
    	return $variations;
    add_filter ( 'woocommerce_available_variation', 'frame_style_lens_colour ', 10, 1 );

    THEN copy ‘variation.php’ to your theme or child theme.
    Add the following to the file, or similar, and save it:

    <p><strong>Lens Style:</strong> {{{ data.variation.lens_type }}} Polarized Lens </p>
    <p><strong>Frame Style:</strong> {{{ data.variation.frame_type }}} </p>

    Now your variation specific ACF fields will appear on the front end of the website.

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘ACF – WooCommerce – Product Variation’ is closed to new replies.