简体   繁体   中英

Disable add to cart button on Woocommerce product category archive pages

How can I disable the "add to basket" button for products only on the "categories" page?

I still want it visible on the product page.

Many thanks

The following will disable add to cart button on product category archive pages:

// Disable add to cart on product category archive pages
add_filter( 'woocommerce_is_purchasable', 'disable_purchasable_on_product_category_archives', 10, 2 );
function disable_purchasable_on_product_category_archives( $purchasable, $product ) {
    if( is_product_category() )
        $purchasable = false;

    return $purchasable;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.


To target specific product archive pages you will replace use this instead:

add_filter( 'woocommerce_is_purchasable', 'disable_purchasable_on_product_category_archives', 10, 2 );
function disable_purchasable_on_product_category_archives( $purchasable, $product ) {
    // HERE define your product category terms
    $terms = array( 'shirts', 'games' ); 

    if( is_product_category( $terms ) )
        $purchasable = false;

    return $purchasable;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

See: Woocommerce Conditional Tags

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