Skip to content (Press Enter)

marioernestoms

WordPress Specialist

  • About

WooCommerce – Block add to cart button

by marioernestomsupdated on 19 de December de 201919 de December de 2019

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 🙂

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)

Related

WordPress
0

marioernestoms

Post Navigation

Previous Article

Recent Posts

  • WooCommerce – Block add to cart button
  • WooCommerce – Remove add to cart button
  • Ajax Login System with WordPress
  • How to install WordPress on VPS with SSH
  • Troubleshoot WordPress update failure message

Categories

  • HTML/CSS/JS
  • Servidores
  • WordPress
Recommended for you...

Ajax Login System with WordPress

by marioernestoms
Taxonomy Term field ACF
Recommended for you...

Taxonomy Term field ACF with WooCommerce categorys.

by marioernestoms
Recommended for you...

WordPress Theme one page scroll

by marioernestoms

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© Copyright 2021 marioernestoms. All Rights Reserved. The Ultralight | Developed By Rara Theme. Powered by WordPress.