Change the add to cart text for a specific product category in Woocommerce 3

谁都会走 提交于 2019-12-18 04:30:15

问题


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.



来源:https://stackoverflow.com/questions/48354487/change-the-add-to-cart-text-for-a-specific-product-category-in-woocommerce-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!