如何修改woocommerce产品详情页Choose an option文字内容?

Submitted by phifans on Sun, 04/21/2024 - 17:10

woocommerce独立站产品详情页商品变体variation选项那里有Choose an option文字。为了表述更加明确有时需要改成自定义的文字,你们如何修改woocommerce产品详情页Choose an option文字内容呢?

PHP代码修改woocommerce产品详情页Choose an option文字

  1. 登录WordPress网站后台,
  2. 点击Appearance-Theme Editor,
  3. 找到当前激活的主题的function.php文件并打开,如果只有一个选项输入以下代码:

    add_filter('woocommerce_dropdown_variation_attribute_options_args', 'custom_woocommerce_product_add_to_cart_text', 10, 2 );
            function custom_woocommerce_product_add_to_cart_text ($args) {
                // Args attribute
                    $args['show_option_none'] = __( 'Select Color', 'woocommerce' );     
                return $args;
            }
  4. 如果有两个或者以上选项输入以下代码:

    add_filter('woocommerce_dropdown_variation_attribute_options_args', 'custom_woocommerce_product_add_to_cart_text', 10, 2 );
            function custom_woocommerce_product_add_to_cart_text ($args) {
                // Args attribute
                $attribute = $args['attribute'];
                if ( $attribute == 'color' ) {
                    $args['show_option_none'] = __( 'Select Color', 'woocommerce' );     
                } elseif ( $attribute == 'pa_size' ) {
                    $args['show_option_none'] = __( 'Select Size', 'woocommerce' );         
                } else {
                    $args['show_option_none'] = __( 'Choose an option', 'woocommerce' );         
                }
                return $args;
            }

    超过两个的话,可以继续添加if else语句修改。需要注意的是这里size后台保存的是pa_size。

  5. 保存后刷新网站缓存,就可以看到woocommerce产品详情页Choose an option文字就改成自定义的内容了。

需要的朋友赶紧试一下吧。

栏目