I managed to add custom fields to product variations with the piece of code bellow.
// ACF VARIATIONS
add_filter('acf/location/rule_values/post_type', 'acf_location_rule_values_Post');
function acf_location_rule_values_Post( $choices ) {
$choices['product_variation'] = 'Product Variation';
return $choices;
}
add_action( 'woocommerce_product_after_variable_attributes', function( $loop, $variation_data, $variation ) {
global $abcdefgh_i;
$abcdefgh_i = $loop;
add_filter( 'acf/prepare_field', 'acf_prepare_field_update_field_name' );
$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 ) {
if( $rule['param'] == 'post_type' && $rule['operator'] == '==' && $rule['value'] == 'product_variation' ) {
acf_render_fields( $variation->ID, acf_get_fields( $acf_field_group ) );
break 2;
}
}
}
}
remove_filter( 'acf/prepare_field', 'acf_prepare_field_update_field_name' );
}, 10, 3 );
function acf_prepare_field_update_field_name( $field ) {
global $abcdefgh_i;
$field['name'] = preg_replace( '/^acf\[/', "acf[$abcdefgh_i][", $field['name'] );
return $field;
}
add_action('woocommerce_save_product_variation', function ($variation_id, $i = -1) {
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 (is_array($val)) {
// 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_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);
function xxx_admin_head_post() {
global $post_type;
if ($post_type === 'product') {
wp_register_script( 'xxx-acf-variation', get_template_directory_uri() . 'https://afw.backyard.rs/wp-content/themes/woodmart-child/js/acf-variation.js', array(
'jquery-core',
'jquery-ui-core'
), '1.1.0',
true ); // Custom scripts
wp_enqueue_script( 'xxx-acf-variation' ); // Enqueue it!
}
}
/* actions fired when adding/editing posts or pages */
/* admin_head-(hookname) */
add_action( 'admin_head-post.php', 'xxx_admin_head_post' );
add_action( 'admin_head-post-new.php', 'xxx_admin_head_post' );
Now I’m trying to display those custom fields on my website.
So, each variation has different custom fields.
The idea is that when the user selects their variations (color, size …) these custom fields are displayed as a product description.
But it doesn’t work for me, those custom variation fields aren’t shown.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.