Search code examples
phpwordpresswoocommerceproductcustom-taxonomy

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

}

Solution

  • 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.