更改Woocommerce 3中特定产品类别的“添加到购物车"文本 [英] Change the add to cart text for a specific product category in Woocommerce 3

查看:76
本文介绍了更改Woocommerce 3中特定产品类别的“添加到购物车"文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想更改产品归档中"特定类别"上的添加到购物车"文本.例如,在预订类别上,我想要的不是add to cart文字,而是Preorder.我不知道如何在以下功能中识别预购类别.

I want to change the add to cart text on product archives on specific categories only. For example, on preorder category i want instead of add to cart text, to be Preorder. I don't know how to identify Preorder category in the below function.

add_filter( 'add_to_cart_text', 'woo_archive_custom_cart_button_text' );    // < 2.1

function woo_archive_custom_cart_button_text() {

        return __( 'Preorder', 'woocommerce' );

}

推荐答案

更新:add_to_cart_text挂钩已过时&不推荐使用.在Woocommerce 3+中,它已由woocommerce_product_add_to_cart_text过滤器挂钩替换.

Update: add_to_cart_text hook is obsolete & deprecated. It is replaced in Woocommerce 3+ by woocommerce_product_add_to_cart_text filter hook.

可能有两件事(因为您的问题不太清楚)

1)要在特定产品类别归档页面上定位产品,您应该使用条件功能这样的 is_product_category() :

1) To target products on a specific product category archive pages you should use the conditional function is_product_category() this way:

add_filter( 'woocommerce_product_add_to_cart_text', 'product_cat_add_to_cart_button_text', 20, 1 );
function product_cat_add_to_cart_button_text( $text ) {
    // Only for a specific product category archive pages
    if( is_product_category( array('preorder') ) )
        $text = __( 'Preorder', 'woocommerce' );

    return $text;
}

代码进入活动子主题(或活动主题)的function.php文件.

2)要在Woocommerce存档页面上定位特定的产品类别,您将使用 has term() 这样:

2) To target a specific product category on Woocommerce archives pages you will use has term() this way:

add_filter( 'woocommerce_product_add_to_cart_text', 'product_cat_add_to_cart_button_text', 20, 1 );
function product_cat_add_to_cart_button_text( $text ) {
    // Only for a specific product category
    if( has_term( array('preorder'), 'product_cat' ) )
        $text = __( 'Preorder', 'woocommerce' );

    return $text;
}

对于单个产品页面,您将另外使用以下内容:

For single product pages you will use additionally this:

add_filter( 'woocommerce_product_single_add_to_cart_text', 'product_cat_single_add_to_cart_button_text', 20, 1 );
function product_cat_single_add_to_cart_button_text( $text ) {
    // Only for a specific product category
    if( has_term( array('preorder'), 'product_cat' ) )
        $text = __( 'Preorder', 'woocommerce' );

    return $text;
}

代码进入活动子主题(或活动主题)的function.php文件.

经过测试,可以正常工作.

Tested and works.

注意:如果设置了某些条件,所有过滤器挂钩函数都需要返回main参数,因此在这种情况下,参数$text

Note: All filter hooked functions needs to return the main argument if you set some conditions, so in this case the argument $text


相关答案:从WooCommerce中的自定义分类法

相关文档: Woocommerce条件标签

这篇关于更改Woocommerce 3中特定产品类别的“添加到购物车"文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆