【问题标题】:Change ‘Choose an Option’ variation dropdown label in WooCommerce在 WooCommerce 中更改“选择一个选项”变体下拉标签
【发布时间】:2019-06-25 02:46:22
【问题描述】:

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

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;
}

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

【问题讨论】:

    标签: php wordpress woocommerce custom-taxonomy


    【解决方案1】:

    您可以使用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;
    }
    

    代码进入您的活动子主题(或活动主题)的 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;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 2014-04-25
      • 2022-08-08
      • 1970-01-01
      • 2020-11-22
      • 2013-07-14
      • 2019-04-19
      • 1970-01-01
      • 1970-01-01
      • 2018-05-09
      相关资源
      最近更新 更多