Hide Add to Cart button on woocommerce Product description page of a particular product

admin2025-04-19  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?

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?

Share Improve this question edited Aug 9, 2016 at 18:47 bravokeyl 3,3776 gold badges27 silver badges33 bronze badges asked Aug 9, 2016 at 17:52 AaroneAarone 1311 silver badge6 bronze badges 1
  • 1 Thanks @bravokeyl for correcting the code formating and making it more readable! – Aarone Commented Aug 10, 2016 at 14:26
Add a comment  | 

2 Answers 2

Reset to default 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/Plugin_API/Action_Reference

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/add-to-cart-button-in-woocommerce-store/

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745058051a282523.html

最新回复(0)