Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #347103

    Hello,
    How to disable Add to Cart button in a product category? ex: Free Gift (slug: free-gift)
    I have tried with this code, but not success. Please help.

    add_action('init','remove_add_to_cart_button');
    function remove_add_to_cart_button() {
    	if ( has_term( 'free-gift', 'product_cat' ) ) {
    		remove_action( 'woocommerce_after_shop_loop_item', 'avia_add_cart_button', 16);
    		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    	}
    }

    Regards

    #347132

    Hey ciptanegara!

    Thank you for using our theme.

    Probably the init hook is to early for the has_term function.

    Try the following:

    
    add_action('init','remove_add_to_cart_button', 1000);
    
    function remove_add_to_cart_button() {
                    remove_action( 'woocommerce_after_shop_loop_item', 'avia_add_cart_button', 16);
    		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    }
    
    add_action( 'woocommerce_after_shop_loop_item', 'my_add_to_cart_button', 16 );
    add_action( 'woocommerce_single_product_summary', 'my_add_to_cart_button_single', 30 );
    
    function my_add_to_cart_button() {
         if ( ! has_term( 'free-gift', 'product_cat' ) ) {
                      avia_add_cart_button();
           }
    }
    
    function my_add_to_cart_button_single() {
         if ( ! has_term( 'free-gift', 'product_cat' ) ) {
                      woocommerce_template_single_add_to_cart();
           }
    }
    

    Best regards,
    Günter

    #347160

    Perfect Günter! It worked. Thanks.

    Best regards,
    me

Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘Disable Add to Cart button in a product category?’ is closed to new replies.