• Resolved t2m

    (@t2m)


    I want to hide Add to Cart button on woocommerce Product description page of a particular product with product id, say, 1234.

    Following code hides Add to Cart button on woocommerce Product description pages of ALL the products:

    function remove_product_description_add_cart_button(){
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        }
    add_action('init','remove_product_description_add_cart_button');

    But when I try to apply the above remove_action for the particular product having product id 1234 using following code, it does not work?

    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 == 1234){
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        }
    }
    add_action('init','remove_product_description_add_cart_button');

    What is wrong with the later and how could it be corrected?

    Thanks!

    https://www.ads-software.com/plugins/woocommerce/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    If you remove the price, there will be no add to cart button.

    Hooking into init is too early though. Try just using the same hook with an earlier priority:

    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 == 1234 ){
        	remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        }
    }
    add_action( 'woocommerce_single_product_summary', 'remove_product_description_add_cart_button', 5 );
    Thread Starter t2m

    (@t2m)

    Hi Caleb Burks!

    Thank you very much for pointing out the mistake. It’s now working flawlessly.

    PS: I knew about the ‘removing price’ trick but it was not suitable for me.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Hide Add to Cart button on Product description page of a particular’ is closed to new replies.