我有可变产品,其中包含属性和术语。 我为每个产品属性术语“external_name”创建了(使用ACF)一个额外的自定义字段。
我正在使用此代码在ACF 中获取自定义规则(请参见最后的屏幕截图):
import sys
blocks = [1,2,3,4,5,6,7,8,9]#list of blocks
p = [[] for i in blocks]#list of direct precedents
p[0] = []
p[1] = []
p[2] = []
p[3] = []
p[4] = []
p[5] = [1,2,3]
p[6] = [2,3,4]
p[7] = [3,4,5]
p[8] = [6,7,8]
我需要在属性的下拉列表中更改Term的名称,具体取决于其自定义字段(如果不为空)。
以下是import sys
blocks = [1,2,3,4,5,6,7,8,9]#list of blocks
p = [[] for i in blocks]#full list of precedents
p[0] = []
p[1] = []
p[2] = []
p[3] = []
p[4] = []
p[5] = [1,2,3]
p[6] = [2,3,4]
p[7] = [3,4,5]
p[8] = [1,2,3,4,5,6,7,8]
的代码:
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 );
现在,我从自定义字段中获取了1个已更改的名称,用于所有条款。 有什么想法吗?
其他详细信息:
我正在使用ACF(不是PRO)
Woocommerce - 版本3.3.5。
Wordpress - 版本4.9.5
我创建了一个CF组“属性字段”,其中包含自定义字段“外部属性名称” ... (请参阅下面的屏幕截图)
答案 0 :(得分:0)
已更新 (解决空属性值错误和代码优化)
以下是使用单变量产品页面的产品属性下拉列表中的ACF自定义字段值替换产品属性Term Name的正确代码:
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;
}
代码进入活动子主题(或活动主题)的function.php文件。经过测试并正常工作。