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

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

问题描述

我只想更改特定类别的产品档案中添加到购物车的文本.例如,在 预购类别 上,我希望将 Preorder 而不是 add to cart 文本.我不知道如何在下面的函数中识别预购类别.

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.

它可以是两种不同的东西(因为你的问题不是很清楚)...

It can be 2 different things (as your question is not so clear)

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 档案页面上的特定产品类别,您将使用 hasterm() 这样:

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 文件中.

经过测试并有效.

注意:如果你设置了一些条件,所有过滤器挂钩函数都需要返回主参数,所以在这种情况下参数$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天全站免登陆