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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀