Change the add to cart text for a specific product

2019-02-11 06:31发布

问题:

I want to change the add to cart text on product archives on specific categories only. For example, on preorder category i want instead of add to cart text, to be Preorder. I don't know how to identify Preorder category in the below function.

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

function woo_archive_custom_cart_button_text() {

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

}

回答1:

Update: add_to_cart_text hook is obsolete & deprecated. It is replaced in Woocommerce 3+ by woocommerce_product_add_to_cart_text filter hook.

It can be 2 different things (as your question is not so clear)

1) To target products on a specific product category archive pages you should use the conditional function is_product_category() this way:

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

Code goes in function.php file of the active child theme (or active theme).


2) To target a specific product category on Woocommerce archives pages you will use has term() this way:

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

For single product pages you will use additionally this:

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

Code goes in function.php file of the active child theme (or active theme).

Tested and works.

Note: All filter hooked functions needs to return the main argument if you set some conditions, so in this case the argument $text


Related answer: Targeting product terms from a custom taxonomy in WooCommerce

Related docs: Woocommerce Conditional Tags



回答2:

You can use has_term() with condition. Updated code is as below.

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

where your-special-category will your category for which you want to replace add to cart text.