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