Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove add cart button in Woocommerce for a specific product category

I have an issue how to remove a cart from a category product. It works just fine if I apply it to a specific id or all in general, but I am unable to do it for a category. Below is my code I have done regarding it.

Also, I am struggling to apply this same pattern to Related Articles section, so any help would be appreciated.

Thank you.

//function for deleting ....

function remove_product_description_add_cart_button(){

    global $product;

    //Remove Add to Cart button from product description of product with id 1234    

    if ($product->id == 188){

    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );

    }

add_action('wp','remove_product_description_add_cart_button');

}
like image 453
SamsungBravo Avatar asked Jan 15 '17 11:01

SamsungBravo


People also ask

How do I customize add to cart button in WooCommerce?

Go to Appearance -> Customize, then go to WooCommerce -> Add to Cart Buttons to choose your settings. Change the Add To Cart button text and/or select the other options on this screen.

How do I remove the cart icon in WooCommerce?

To do this, please navigate to the Dashboard > Installed Plugins page. Furthermore, find the WooCommerce section and select the Deactivate option then the cart icon will be instantly deleted from your website.


1 Answers

Nov 2020 Update

To make it work with a product category you can use the WordPress conditional function has_term() this way:

add_action('woocommerce_single_product_summary', 'remove_product_description_add_cart_button', 1 );
function remove_product_description_add_cart_button() { // function for deleting ...
    // Set HERE your category ID, slug or name (or an array)
    $categories = array('your-category-1');

    //Remove Add to Cart button from product description of product with id 1234
    if ( has_term( $categories, 'product_cat', get_the_id() ) ) {
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    }
}

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

like image 185
LoicTheAztec Avatar answered Sep 22 '22 12:09

LoicTheAztec