douji5746 2018-06-02 20:01
浏览 73
已采纳

根据Woocommerce中的父产品类别更改“添加到购物车”按钮文本

I have a Woocommerce shop with parent categories and subcategories.

For example, the parent category is Menswear and its two subcategories are Shirts and Pants.

I want to change the Add To Cart button text on all of the single product pages for all of the products in the Menswear parent category. That is, all of the products in both the Shirts and Pants subcategories.

The following code achieves this, but there must be a better way using the parent category in the if statement rather than both of the subcats.

Unfortunately I don't know how to do this can anyone please help?

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 ) {

    if( has_term( array('shirts','pants'), 'product_cat') )
        $text = __( 'Buy Menswear', 'woocommerce' );

    return $text;
}

Just to clarify:

What I was hoping for was a solution that uses the parent category (Menswear) in the array and not the sub categories Shirts and Pants. In my site I have just four parent categories, but each of these have dozens of subcategories. I was looking for a solution that avoids having to list dozens of subcats in the array, and just uses the four parent categories. Is it possible to change the add to cart text based on a parent category? (that is, the change will occur to all products in the subcategories belonging to that parent).

Further clarification:

Let say I have four parent categories: Red, Yellow, Green and Blue.

Each of these parent categories has multiple subcategories as follows:

RED
  Red subcat 1
  Red subcat 2

YELLOW
  Yellow subcat 1
  Yellow subcat 2

GREEN
  Green subcat 1
  Green subcat 2

BLUE
  Blue subcat 1
  Blue subcat 2

I want the products in Red subcat 1 and Red subcat 2 to have the add to cart text BUY RED.

I want the products in Yellow subcat 1 and Yellow subcat 2 to have the add to cart text BUY YELLOW.

I want the products in Green subcat 1 and Green subcat 2 to have the add to cart text BUY GREEN.

I want the products in Blue subcat 1 and Blue subcat 2 to have the add to cart text BUY BLUE.

In my actual website, each of the parent categories has more than 50 subcategories so it is not practical to list these as an array.

Thank is why I am looking for a solution to change add to cart text based on parent category.


UPDATE

Based on @LoicTheAztec working code (thanks), this is my final working code:

// Utility function to get the childs array from all parent categories
function get_the_childs( $product_category ){
    $taxonomy = 'product_cat';
    $parent   = get_term_by( 'slug', sanitize_title( $product_category ), $taxonomy );
    return get_term_children( $parent->term_id, $taxonomy );
}

// Changing the add to cart button text for product based on parent category
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 ) {
    global $product;
    if( has_term( get_the_childs('reds'), 'product_cat', $product->get_id() ) )
        $text = __( 'Buy Red', 'woocommerce' );
    elseif( has_term( get_the_childs('yellow'), 'product_cat', $product->get_id() ) )
        $text = __( 'Buy Yellow', 'woocommerce' );
    elseif( has_term( get_the_childs('green'), 'product_cat', $product->get_id() ) )
        $text = __( 'Buy Green', 'woocommerce' );
    elseif( has_term( get_the_childs('blue'), 'product_cat', $product->get_id() ) )
        $text = __( 'Buy Blue', 'woocommerce' );

    return $text;
}

FURTHER UPDATE: This solution has a bug. If one of the parent categories is empty the code does not work.

See here:

Bug in Change Add To Cart button text based on parent product categories in Woo

  • 写回答

2条回答 默认 最新

  • dtjw6660 2018-06-02 20:32
    关注

    Updated

    First you will find below a a utility function to get the childs array from the parent product category. Then you will be able to use it in your hooked function.

    You can handle multiple parent product categories with different text buttons.

    The code:

    // Utility function to get the childs array from a parent product category
    function get_the_childs( $product_category ){
        $taxonomy = 'product_cat';
        $parent   = get_term_by( 'slug', sanitize_title( $product_category ), $taxonomy );
        return get_term_children( $parent->term_id, $taxonomy );
    }
    
    // Changing the add to cart button 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 ) {
        global $product;
        if( has_term( get_the_childs('Menswear'), 'product_cat', $product->get_id() ) )
            $text = __( 'Buy Menswear', 'woocommerce' );
        elseif( has_term( get_the_childs('Womenswear'), 'product_cat', $product->get_id() ) )
            $text = __( 'Buy Womenswear', 'woocommerce' );
        elseif( has_term( get_the_childs('Childswear'), 'product_cat', $product->get_id() ) )
            $text = __( 'Buy Childswear', 'woocommerce' );
    
        return $text;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来