【问题标题】:Change the add to cart text for a specific product category in Woocommerce 3在 Woocommerce 3 中更改特定产品类别的添加到购物车文本
【发布时间】:2018-06-29 11:38:12
【问题描述】:

我只想更改特定类别的产品档案中添加到购物车的文本。例如,在 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' );

}

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce custom-taxonomy


    【解决方案1】:

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

    它可以是 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 文件中。

    经过测试并且有效。

    注意:如果你设置了一些条件,所有过滤器钩子函数都需要返回主参数,所以在这种情况下参数$text


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

    相关文档:Woocommerce Conditional Tags

    【讨论】:

      【解决方案2】:

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

      解决方案- 1. 使用过滤器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;
          }
      

      解决方案- 2. 使用过滤器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 文本。

      【讨论】:

      • 如果 add_to_cart_text 不起作用(因为它是旧过滤器),您可以使用另一个过滤器 woocommerce_product_add_to_cart_text 。我已经更新了我的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      • 1970-01-01
      • 2017-12-10
      • 1970-01-01
      • 2018-11-12
      • 1970-01-01
      相关资源
      最近更新 更多