Woo commerce required minimum quantity to purchase products
Here is the code to restrict user to purchase minimum quantity of products. Below is the code to be add into theme functions.php file:
add_action( 'woocommerce_check_cart_items', 'required_min_cart_subtotal_amount' );
function required_min_cart_subtotal_amount() {
$q=0;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$q = $q + $cart_item['quantity'];
}
if($q < 24) {
// Display an error message if quantity is less than 24
?>
<script>
jQuery(document).ready(function(){
jQuery('.checkout-button').css('opacity','0.5');
jQuery('.checkout-button').css('pointer-events','none');
});
</script>
<?php
wc_add_notice( '<strong>' . sprintf( __("A minimum total quantity of 24 is required to checkout.") ) . '<strong><a href="/shop">SHOP NOW!</a>', 'error' );
}
}
Below is the javascript code to be added in theme javascript file so that page will refresh after quantity update on cart page:
jQuery( function($){
$(document.body).on(‘updated_cart_totals’, function () {
// Get the formatted cart total
window.location.reload();
});
});