Yesterday I had a request from one client. He asks me about the possibility to block add to cart button if had less or equal to 5 products in the cart. I don’t like this approach, but I made a solution for this. Based on people’s search a lot about this on StackOverflow. let’s see the solution:
/**
* Disable checkout button if total cart less or equal than 5 products.
*/
function disable_checkout_button() {
global $woocommerce;
$total = $woocommerce->cart->cart_contents_count;
if ( $total <= 5 ) {
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
echo '<a href="#" class="disable button alt wc-forward">Proceed to checkout</a>';
}
}
add_action( 'woocommerce_proceed_to_checkout', 'disable_checkout_button', 1 );
That’s it you only need to put this on functions.php file or in one plugin. And create some CSS to custom class disable, or you can use JavaScript to for the block button. Another thing that you can do is puts this as an alert message.
For disable button with CSS, I put this code on style.css for the class disable:
.disabled {
pointer-events: none;
cursor: default;
}
This solution works well and the client was very happy. So I post here to make more people that need something closer happy to. this is my motto 🙂