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?
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/