php - 在 Woocommerce 3 中更改特定产品类别的添加到购物车文本

标签 php wordpress woocommerce hook-woocommerce custom-taxonomy

我只想更改特定类别的产品文件中添加到购物车的文本。例如,在 preorder category 上,我想要 Preorder 而不是 add to cart 文本。我不知道如何在下面的函数中识别预购类别

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

function woo_archive_custom_cart_button_text() {

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

}

最佳答案

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

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

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

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()这样:

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

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

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

经过测试并且有效。

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


相关答案:Targeting product terms from a custom taxonomy in WooCommerce

相关文档:Woocommerce Conditional Tags

关于php - 在 Woocommerce 3 中更改特定产品类别的添加到购物车文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48354487/

相关文章:

php - 以编程方式创建的 WooCommerce 订单对新用户不征税

javascript - package.json 和 composer.json 是一样的吗?

php - 数据库中的 OpenID 重复结果

php - 替换字符串中最后一次出现的异常(exception)项

javascript - 将 php 添加到 javascript (wordpress)

php - 在 Admin Dashboard Stats Widget 中添加自定义订单状态

javascript - 从用户输入字段获取数据

php - 在 PHP 应用程序 Openshift 上提交更改后出现 404 Not Found 错误

php - MySQL 搜索结果 - 按相关性排序

wordpress - 如果产品中没有评论,Woocommerce 显示评级