更改 WooCommerce 中的“选择选项”变体下拉标签

p_e*_*_88 5 php wordpress custom-taxonomy woocommerce

我尝试将“选择选项”的值更改为属性名称。因此,在您可以选择颜色的下拉列表中,它应该是名称颜色作为第一个颜色。到目前为止,它可以使用此代码片段

add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'cinchws_filter_dropdown_args', 10 );

function cinchws_filter_dropdown_args( $args ) {
    $var_tax = get_taxonomy( $args['attribute'] );
    $args['show_option_none'] = apply_filters( 'the_title', $var_tax->labels->name );
    return $args;
}
Run Code Online (Sandbox Code Playgroud)

我现在遇到的问题是它显示“产品颜色”或“产品材质”,因此它在属性值之前添加了产品。如何只获取属性值?

下拉字段

Loi*_*tec 8

您可以使用wc_attribute_label()专用函数来获取产品属性标签名称:

add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'filter_dropdown_variation_args', 10 );
function filter_dropdown_variation_args( $args ) {
    $args['show_option_none'] = apply_filters( 'the_title', wc_attribute_label( $args['attribute'] ) );

    return $args;
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。

或者你可以使用singular_name而不是name像:

add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'filter_dropdown_variation_args', 10 );
function filter_dropdown_variation_args( $args ) {
    $args['show_option_none'] = apply_filters( 'the_title', get_taxonomy( $args['attribute'] )->labels->singular_name );

    return $args;
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。