Skip to content

Instantly share code, notes, and snippets.

@shreyans94
Created January 23, 2018 21:32
Show Gist options
  • Save shreyans94/05b10194cf2f57cf054a5cf3da3fd931 to your computer and use it in GitHub Desktop.
Save shreyans94/05b10194cf2f57cf054a5cf3da3fd931 to your computer and use it in GitHub Desktop.
Display ACF for woocommerce variations in backend
// Render fields at the bottom of variations - does not account for field group order or placement.
add_action( 'woocommerce_product_after_variable_attributes', function( $loop, $variation_data, $variation ) {
global $abcdefgh_i; // Custom global variable to monitor index
$abcdefgh_i = $loop;
// Add filter to update field name
add_filter( 'acf/prepare_field', 'acf_prepare_field_update_field_name' );
// Loop through all field groups
$acf_field_groups = acf_get_field_groups();
foreach( $acf_field_groups as $acf_field_group ) {
foreach( $acf_field_group['location'] as $group_locations ) {
foreach( $group_locations as $rule ) {
// See if field Group has at least one post_type = Variations rule - does not validate other rules
if( $rule['param'] == 'post_type' && $rule['operator'] == '==' && $rule['value'] == 'product_variation' ) {
// Render field Group
acf_render_fields( $variation->ID, acf_get_fields( $acf_field_group ) );
break 2;
}
}
}
}
// Remove filter
remove_filter( 'acf/prepare_field', 'acf_prepare_field_update_field_name' );
}, 10, 3 );
// Filter function to update field names
function acf_prepare_field_update_field_name( $field ) {
global $abcdefgh_i;
$field['name'] = preg_replace( '/^acf\[/', "acf[$abcdefgh_i][", $field['name'] );
return $field;
}
// Save variation data
add_action( 'woocommerce_save_product_variation', function( $variation_id, $i = -1 ) {
// Update all fields for the current variation
if ( ! empty( $_POST['acf'] ) && is_array( $_POST['acf'] ) && array_key_exists( $i, $_POST['acf'] ) && is_array( ( $fields = $_POST['acf'][ $i ] ) ) ) {
foreach ( $fields as $key => $val ) {
update_field( $key, $val, $variation_id );
}
}
}, 10, 2 );
@jeremypetrequin
Copy link

🙏thanks for this

@johnw6761
Copy link

Does this code go in functions.php?
I have added it but dont see product variations as an option under post type?
Any ideas?

@johnw6761
Copy link

@jeremypetrequin any ideas?

@sulym-roman
Copy link

@jeremypetrequin any ideas?

The code above only finds a field group that has location rule "Post Type = Product Variation".

But it doesn't extend the list of "Post Type" location values. Value "Product Variation" must be added manually.

You need smth like this

// Add "Product Variation" location rule values
function my_acf_location_rule_values_post_type($choices){

	$keys = array_keys($choices);
	$index = array_search('product', $keys);

	$position = $index === false ? count($choices) : $index + 1;

	$choices = array_merge(
		array_slice($choices, 0, $position),
		array('product_variation' => __('Product Variation', 'auf')),
		array_slice($choices, $position)
	);

	return $choices;
}

add_filter('acf/location/rule_values/post_type', 'my_acf_location_rule_values_post_type');


// Add "Product Variation" location rule match
function my_acf_location_rule_match_post_type($match, $rule, $options, $field_group){

	if ($rule['value'] == 'product_variation') {

		$post_type = $options['post_type'];

		if ($rule['operator'] == "==")
			$match = $post_type == $rule['value'];

		elseif ($rule['operator'] == "!=")
			$match = $post_type != $rule['value'];
	}

	return $match;
}

add_filter('acf/location/rule_match/post_type', 'my_acf_location_rule_match_post_type', 10, 4);

to be able to set location rule "Post Type = Product Variation".

And again "but"...

shreyans94's code works only for simple field types like "Text", "Text Area", "Select" etc.

It doesn't render complex fields properly. I mean fields like "Color Picker", "Taxonomy" with multi select and so on.

@nathanaelphilip
Copy link

If anyone needs to add images to the variation field, you’ll need to rebind the ACF events after the variations are loaded:

  add_action('acf/input/admin_footer', 'my_acf_input_admin_footer');

  function my_acf_input_admin_footer() {
?>
    <script type="text/javascript">
      (function($) {
        $(document).on('woocommerce_variations_loaded', function () {
          acf.do_action('append', $('#post'));
        })
      })(jQuery);	
    </script>
<?php      
  }

@mwalcott
Copy link

Thank you for the code. I added the fields and have them showing in the backend but am modifying the template to bring the fields in. Do you happen to have a snippet to pull the fields into the frontend or could you point me in the right direction?

@gerardcuadras
Copy link

Works perfect! Thanks!

@artprojectgroup
Copy link

artprojectgroup commented Nov 18, 2022

Fixed for repeater fields:

// Render fields at the bottom of variations - does not account for field group order or placement.
add_action( 'woocommerce_product_after_variable_attributes', function ( $loop, $variation_data, $variation ) {
    global $acf_variation; // Custom global variable to monitor index

    $acf_variation = $loop;
    // Add filter to update field name
    add_filter( 'acf/prepare_field', 'acf_prepare_field_update_field_name' );

    // Loop through all field groups
    $acf_field_groups = acf_get_field_groups();
    foreach ( $acf_field_groups as $acf_field_group ) {
        foreach ( $acf_field_group[ 'location' ] as $group_locations ) {
            foreach ( $group_locations as $rule ) {
                // See if field Group has at least one post_type = Variations rule - does not validate other rules
                if ( $rule[ 'param' ] == 'post_type' && $rule[ 'operator' ] == '==' && $rule[ 'value' ] == 'product_variation' ) {
                    // Render field Group
                    acf_render_fields( $variation->ID, acf_get_fields( $acf_field_group ) );

                    break 2;
                }
            }
        }
    }

    // Remove filter
    remove_filter( 'acf/prepare_field', 'acf_prepare_field_update_field_name' );
}, 10, 3 );

// Filter function to update field names
function acf_prepare_field_update_field_name( $field ) {
    global $acf_variation;

    $field[ 'name' ] = preg_replace( '/^acf\[/', "acf[$acf_variation][", $field[ 'name' ] );

    return $field;
}

// Save variation data
add_action( 'woocommerce_save_product_variation', function ( $variation_id, $i = -1 ) {
    // Update all fields for the current variation
    if ( !empty( $_POST[ 'acf' ] ) && is_array( $_POST[ 'acf' ] ) && array_key_exists( $i, $_POST[ 'acf' ] ) && is_array( ( $fields = $_POST[ 'acf' ][ $i ] ) ) ) {
        $unique_updates = array();
        foreach ( $fields as $key => $val ) {
            if ( strpos( $key, 'field_' ) === false ) {
                // repeater fields need to be parsed separately
                foreach ( $val as $repeater_key => $repeater_val ) {
                    if ( !array_key_exists( $repeater_key, $unique_updates ) || !empty( $repeater_val ) ) {
                        $unique_updates[ $repeater_key ] = $repeater_val;
                    }
                }
            } else {
                // non-repeater fields can be parsed normally
                // The repeater fields are repeated here, but empty. This causes the repeater that was updated above to be cleared
                if ( !array_key_exists( $key, $unique_updates ) || !empty( $val ) ) {
                    $unique_updates[ $key ] = $val;
                }
            }
        }
        // Only update each field once
        foreach ( $unique_updates as $key => $val ) {
            update_field( $key, $val, $variation_id );
        }
    }
}, 10, 2 );

// Add "Product Variation" location rule values
function my_acf_location_rule_values_post_type( $choices ) {
    $keys = array_keys( $choices );
    $index = array_search( 'product', $keys );

    $position = $index === false ? count( $choices ) : $index + 1;

    $choices = array_merge(
        array_slice( $choices, 0, $position ),
        array( 'product_variation' => __( 'Product Variation', 'auf' ) ),
        array_slice( $choices, $position )
    );

    return $choices;
}
add_filter( 'acf/location/rule_values/post_type', 'my_acf_location_rule_values_post_type' );

// Add "Product Variation" location rule match
function my_acf_location_rule_match_post_type( $match, $rule, $options, $field_group ) {
    if ( $rule[ 'value' ] == 'product_variation' && isset( $options[ 'post_type' ] ) ) {
        $post_type = $options[ 'post_type' ];

        if ( $rule[ 'operator' ] == "==" ) {
            $match = $post_type == $rule[ 'value' ];
        } elseif ( $rule[ 'operator' ] == "!=" ) {
            $match = $post_type != $rule[ 'value' ];
        }
    }

    return $match;
}
add_filter( 'acf/location/rule_match/post_type', 'my_acf_location_rule_match_post_type', 10, 4 );

//Campos imagen
function my_acf_input_admin_footer() {
?>
<script type="text/javascript">
  (function($) {
    $(document).on('woocommerce_variations_loaded', function () {
      acf.do_action('append', $('#post'));
    })
  })(jQuery);	
</script>
<?php
}
add_action( 'acf/input/admin_footer', 'my_acf_input_admin_footer' );

@8ctopotamus
Copy link

These snippets were very helpful and worked great! Thanks for sharing 👍

@LMAR6
Copy link

LMAR6 commented Jun 8, 2023

Thanks for sharing, very useful!

@bluebee-team
Copy link

This is sooo good. Thank you. The last bit I'm really struggling with is getting at the saved data on the front end. I have variable products with a relationship field in each and I can edit and save data correctly on the back end, but I can't work out how to access that data on the front end. I've been trying to access it via the variation product's meta data but it isn't saved there - or at least it's not returned as part of the array in get_post_meta($variation_id);

Any ideas?

@Emmilemmi
Copy link

Emmilemmi commented Nov 30, 2023

Hi @artprojectgroup, I need to populate "ordinary" acf fields and also relational fields for variable products. your snippet works perfect for relational fields while the other first one for the other acf fields. As I am not familiar whit coding I failed combining the two snippets into one. Can you please give me a hint? Or maybe you @nathanaelphilip or @sulym-roman ??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment