首页 文章

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

提问于
浏览
1

我想仅在 specific categories 上更改产品档案中的添加到购物车文本 . 例如,在 preorder category 上我想要而不是 add to cart 文本,是 Preorder . 我不知道如何 identify Preorder category 在下面的功能 .

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

function woo_archive_custom_cart_button_text() {

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

}

2 回答

  • 3

    您可以在条件中使用has_term() . 更新后的代码如下 .

    Solution- 1. Using filter add_to_cart_text

    add_filter( 'add_to_cart_text', 'woo_archive_custom_cart_button_text' );   
    
        function woo_archive_custom_cart_button_text() {
    
                global $product;
    
                 if(has_term('your-special-category', 'product_cat', $product->get_id())){
                  $text = __( 'Preorder', 'woocommerce' );
                 }
                  return $text;
        }
    

    Solution- 2. Using filter woocommerce_product_add_to_cart_text

    add_filter( 'woocommerce_product_add_to_cart_text', 'woo_archive_custom_cart_button_text' );   
    
        function woo_archive_custom_cart_button_text() {
    
                global $product;
    
                 if(has_term('your-special-category', 'product_cat', $product->get_id())){
                  $text = __( 'Preorder', 'woocommerce' );
                 }
                  return $text;
        }
    

    其中 your-special-category 将是您要替换的类别 add to cart 文本 .

  • 1

    更新:add_to_cart_text挂钩已过时且已弃用 . 它由woocommerce_product_add_to_cart_text过滤器钩子在Woocommerce 3中替换 .

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

    1)要定位产品 on a specific product category archive pages ,您应该使用条件函数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文件中 .

    经过测试和工作 .

    注意:如果设置了一些条件,所有过滤器挂钩函数都需要返回主参数,因此在本例中参数$ text ...


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

    相关文档:Woocommerce Conditional Tags

相关问题