Home › Forums › General Issues › Variable products support
Hi,
Are there any plans to support Woocommerce variable products (the variations)?
ACF is a awesome plugin but I keep running into this limitation for multiple projects.
Thanks for listening 🙂
Hi,
This piece of code may help you. To use it, add a location to your field group where Taxonomy equals your attribute name. Please note, this only work with “basic” fields, ie. not relying on javascript (gallery or image buttons do not work).
$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] ) ) {
return;
}
$_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);
I am also in need of variable product support for a website I’m building on behalf of a client. I have 2 questions regarding implementation:
1) Which file should I add the above code to? is it functions.php in my child theme? All I need is the ability to add a UPC Code for each product which will display on the product page.
2) When you say, “add a locationto your field group where Taxonomy equals your attribute name“, could you elaborate on what you mean? I have bolded and italicized the terms I need help with.
I have made the following change in preparation for the code insertion. Please advise if I have done this correctly.
Thanks!
I have added the above code to my child theme’s functions.php, and made the changes mentioned as, “add a location to your field group where Taxonomy equals your attribute name”. Screenshot attached.
However, the UPC Code still fails to appear. I must have goofed somewhere. Any ideas?
Hey Rileym3,
In my understanding, you created a product attribute named “variable” (WordPress Dashboard > Products > Attributes). Did you then:
– create a “variable product” in WooCommerce, (“Product Data” section set to “variable product”),
– selected your “variable” attribute in the “Attributes” tab
– created a variation based on the selected attribute in the “Variations” tab?
You would then find your “UPC code” variable below the “Description” field after you opened the variations you created for the product.
Thanks for your post.
When I use this code my fields appear on my product page but when I add anything to them they dont save, I just get a spinner going round forever.
I just wondered if you had any ideas why this might be happening?
Thanks
John
@johnw6761 and anyone else who encounters this issue – if you check the error log you’ll probably see this error :
PHP Fatal error: Uncaught Error: Call to a member function save_post() on null
To fix this just replace acf()->input->save_post($variation_id);
with acf_save_post($variation_id);
and it should save as expected.
The topic ‘Variable products support’ is closed to new replies.
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.