Website Building » WooCommerce » How Do I Disable a Button in WooCommerce?

How Do I Disable a Button in WooCommerce?

Last updated on January 18, 2023 @ 6:22 am

If you’re running a WooCommerce store, there may be times when you need to disable the Add to Cart button on certain products. For example, you might want to disable the button if a product is out of stock, or if it’s not available for purchase until a certain date.

Whatever the reason, disabling the Add to Cart button is easy to do with just a few lines of code. In this article, we’ll show you how to disable the Add to Cart button in WooCommerce.

First, you’ll need to open up your child theme’s functions.php file. If you don’t have a child theme set up yet, now is a good time to do that.

Adding a child theme is beyond the scope of this article, but you can find plenty of resources online about how to set one up. Once you have your child theme’s functions.php file open, add the following code:

/**
 * Disable the Add to Cart button on all products. */
add_filter( 'woocommerce_is_purchasable', '__return_false' );
 
/**
 * Disable the Add to Cart button on specific products. 

*/
function wc_remove_add_to_cart_buttons( $product ) {
    $product_id = $product->get_id();
    if ( in_array( $product_id, array( 10, 20 ) ) ) { // Replace 10 and 20 with the IDs of the products you want to disable add to cart for. 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' );     }  }  add-to-cart wc-product-10"></a>">Read More »</span></div></article></main><div id="secondary" class="widget-area" role="complementary"> </div><!-- .wrap --></div><!-- #content -->

That's all there is to it! You can now save your changes and test out your new settings. Remember, you can always tweak the code snippet to better suit your needs.

Conclusion
Disabling the Add to Cart button in WooCommerce is easy to do with just a few lines of code. Simply add the code snippet to your child theme's functions.php file and save your changes. You can also use this code snippet to disable the Add to Cart button on specific products.

Drew Clemente

Drew Clemente

Devops & Sysadmin engineer. I basically build infrastructure online.