How to remove add to cart button from a certain category Woocommerce?

In this tutorial we’ll learn how to remove add to cart button from a certain category page.

From backend of my site i’ve created two categories named as category1 and category2.

woocommerceNow i’m going to assign some products to these categories.

woocommerce1As you can see in the image i’ve created some products and assign them to newly created categories. Product1 and Product3 has been assigned to category1 and Product2 has been assigned to category2.

Now open the file functions.php and add the following lines of code at the end of file.

function western_custom_buy_buttons(){

   $product = get_product();

   if ( has_term( 'category1', 'product_cat') ){

       // removing the purchase buttons

                                remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );

 

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

       remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );

       remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 );

       remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );

       remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 );

   }

}

add_action( 'wp', 'western_custom_buy_buttons' );

You can access function.php file here:

woocommerce2Let’s say I want to remove add to cart button from category1’s product listing page and category1’s products. So i’ve settled category1 as parameter in code. Replace the category1 with the category name of your for which you want to remove add to cart button.

After putting the code save the file.

Now go to front end.

Here is what my category1 page will look like:

woocommerce3You can see add to cart button ahs been removed from this category page.

Now if you’ll go to category2 page, you’ll notice add to cart button hasn’t been removed for this category.

woocommerce4Now, go the product1 page. It will look like this:

woocommerceSince product1 was in category1 so add to cart button has been removed from here as well, but if you open product2 you’ll see add to cart button is there since this product is not in category1.

woocommerce

 

Editorial Staff

ThemeLocation Provides Wordpress & Woocommerce development & Customization services. Have a Project? Contact To discuss

2 thoughts on “How to remove add to cart button from a certain category Woocommerce?

  1. This is very useful – thanks for sharing this.
    Only issue is that when the product is displayed via the Woocommerce shortcode, the add to cart button appears (form.cart )
    Any help? thanks,

Leave a Reply

Your email address will not be published. Required fields are marked *