Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

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

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' );

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...