У меня есть продукты с переменными атрибутами и термином внутри.Я создал (с ACF) дополнительное пользовательское поле для каждого термина атрибута продукта «external_name».
Я использую этот код для получения пользовательских правил в ACF (см. Скриншот в конце) :
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 );
Мне нужно изменить имя термина в раскрывающемся списке атрибутовв зависимости от его пользовательского поля (если не пустое).
Вот мой код от functions.php
:
<code> 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) ?>