Categories
CMS Mastering Development Wordpress

WooCommerce with ACF: custom fields per product variation

I’m a little stuck here. So I have variable products and I need to show the output of a custom field in a custom tab.

So I created a new field in ACF, called ‘test’. I’ve got this to work for the product itself, but the content does not change when I select another variable.

The code I have to add a ‘Post type’ = ‘Product variable’ in ACF.

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

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

and the code for the tab is

add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
    
    // Adds the new tab
    
    $tabs['test_tab'] = array(
        'title'     => __( 'Shipping', 'woocommerce' ),
        'priority'  => 50,
        'callback'  => 'woo_new_product_tab_content'
    );

    return $tabs;

}
function woo_new_product_tab_content() {

    // The new tab content

    echo 'New Product Tab';
    echo 'Here\'s your new product tab.';
        echo get_field('test', $post_id);
        print_r($post_id);
    
}

Can someone help me on my way with this? Much appreciated!!

Leave a Reply

Your email address will not be published. Required fields are marked *