Support

Account

Home Forums General Issues Variable products support Reply To: Variable products support

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