Problem
Do not allow customer to checkout if cart amount is under a certain value for products in a specific category.
For example, don’t allow checkout if customer order $100 worth of T’shirts, if T’shirt category is set to $200 minimum.
The code must
- check cart items for a specific product category
- check the $ value is higher than is set in the code
- display a notice when a specific product category is found and the order is lower than X amount.
- and eventually redirect to cart page when user tries to checkout with an order lower than X amount.
Solution
Adding this code to your child theme’s functions.php file.
//Dont allow order if X Category under X amount
add_action( 'woocommerce_check_cart_items', 'check_cart_outlet_items' );
function check_cart_outlet_items() {
$categories = array('OUTLET'); // Defined targeted product categories
$threshold = 30; // Defined threshold amount
$cart = WC()->cart;
$cart_items = $cart->get_cart();
$subtotal = $cart->subtotal;
$subtotal -= $cart->get_cart_discount_total() + $cart->get_cart_discount_tax_total();
$found = false;
foreach( $cart_items as $cart_item_key => $cart_item ) {
// Check for specific product categories
if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
$found = true; // A category is found
break; // Stop the loop
}
}
if ( $found && $subtotal < $threshold ) {
// Display an error notice (and avoid checkout)
wc_add_notice( sprintf( __( "You must order at least %s" ), wc_price($threshold) ), 'error' );
}
}