1
votes

I have Variable products with Attributes and Term inside them. I created (with ACF) an additional Custom field for every Product Attribute term 'external_name'.

I am using this code to get custom rules in ACF (see the screenshot at the end):

add_filter( 'acf/location/rule_types', function( $choices ){
$choices[ __("Other",'acf') ]['wc_prod_attr'] = 'WC Product Attribute';
return $choices;} );

add_filter( 'acf/location/rule_values/wc_prod_attr', function( $choices ){
foreach ( wc_get_attribute_taxonomies() as $attr ) {
    $pa_name = wc_attribute_taxonomy_name( $attr->attribute_name );
    $choices[ $pa_name ] = $attr->attribute_label;
}
return $choices;} );

add_filter( 'acf/location/rule_match/wc_prod_attr', function( $match, $rule, $options ){
if ( '==' === $rule['operator'] ) {
    $match = $rule['value'] === $options['ef_taxonomy'];
} elseif ( '!=' === $rule['operator'] ) {
    $match = $rule['value'] !== $options['ef_taxonomy'];
}
return $match;}, 10, 3 );

I need to change Term's name in Attribute's dropdown depending on its Custom field (if not empty).

Here is my code from functions.php:

 function filter_woocommerce_variation_option_name( $term_name ) { 
    $attribute_taxonomies = wc_get_attribute_taxonomies();
    if ( $attribute_taxonomies )
    {
        foreach ($attribute_taxonomies as $tax)
        {
            if (taxonomy_exists(wc_attribute_taxonomy_name($tax->attribute_name))){
                $taxonomy = wc_attribute_taxonomy_name($tax->attribute_name);
                $terms = get_terms($taxonomy, 
                    array(
                        'orderby'    => 'name',
                        'order'      => 'ASC',
                        "hide_empty" => false
                    )
                );

                /** Loop through every term */
                $external = '';
                foreach($terms as $term){
                    $termId = $term->term_id;
                    if ($external = get_field('external_name', $term->taxonomy . '_' . $term->term_id)) {
                        ?><pre><?php print_r($term_name) ?></pre><?php
                        $term_name = $external;
                    }
                }
            }
        }
    }

    return $term_name;
};

add_filter( 'woocommerce_variation_option_name', 'filter_woocommerce_variation_option_name', 10, 1 );

For now I have 1 changed name taken from Custom field for ALL terms. Any thoughts?


Additional details:

I'm using ACF (not PRO).
Woocommerce - Version 3.3.5.
Wordpress - Version 4.9.5

I created a CF group "Attribute Fields" with custom field "External Attribute Name" inside… (see the screen shot below).

CF

1
Thanks for Your attention to my question. I added details. - Vasiliy Krasnopevtsev
I added rules. Thank you! - Vasiliy Krasnopevtsev

1 Answers

0
votes

Updated (Solve empty attribute values bug and code optimization)

Here is the correct code to replace product attribute Term Name by the ACF custom field value in the product attribute dropdown in single variable product pages:

add_filter( 'woocommerce_variation_option_name', 'filter_woocommerce_variation_option_name', 10, 1 );
function filter_woocommerce_variation_option_name( $term_name ) {
    global $product;

    // Loop through Product attributes used for variations in the current variable product
    foreach( $product->get_variation_attributes() as $taxonomy => $term_slugs ){
        foreach( $term_slugs as $slug ){
            $term = get_term_by( 'slug', $slug, $taxonomy );
            if ( $term->name == $term_name ){
                // Finding the matching term to be replaced
                $external_name = get_field('external_name', $taxonomy . '_' . $term->term_id );
                if ( isset($external_name) && ! empty($external_name) )
                    return $external_name;
            }
        }
    }
    return $term_name;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.