0

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

Can anyone help with this please?

  • 1
    Thanks @bravokeyl for correcting the code formating and making it more readable! – t2m Aug 10 '16 at 14:26
1

You are applying your hook to early. Wordpress is perfectly fine with removing that action, however it doesn't know what product/post ID is yet. You can rewrite the last line of your code like that:

add_action('wp','remove_product_description_add_cart_button');

Wordpress actions chronologically - https://codex.wordpress.org/Plugin_API/Action_Reference

  • Thankyou very much Max! Your suggestion that I'm applying hook too early was the actual reason why the code was not working. Instead of applying add_action to 'wp', however, applying it to woocommerce function 'woocommerce_single_product_summary' worked. But point is that you had corretly pointed out to the mistake. +1 for that. [I actually can not +1 your answer you see :-) ] – t2m Aug 10 '16 at 14:20
0

You can easily remove the add to cart button from product pages by adding this in woocommerce.php (located wp-content/plugins/woocommerce)

    function WpBlog() {
    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');
    return WooCommerce::instance();
    }

After adding this code, reload the page and you will see that the button has been hidden.

You can also remove the add to cart button from specific Product pages using this code in functions.php (located in the theme folder):

    add_filter('woocommerce_is_purchasable', 'wpblog_specific_product');
    function wpblog_specific_product($purchaseable_product_wpblog, $product) 
    {
    return ($product->id == specific_product_id (512) ? false : 
    $purchaseable_product_wpblog);
    }

For reference you can see the details to easily hide and show add to cart buttons https://www.wpblog.com/add-to-cart-button-in-woocommerce-store/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.