简体   繁体   中英

Change Add To Cart button text based on parent product categories in 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

Updated

First you will find below aa 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.

在此处输入图片说明

Thanks to @LoicTheAztec for his input.

After editing his code a little I came up with this.

Tested and works.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM