I am using the following code to make sure that a minimum of two products are purchased from a particular category, if there is less than the minimum added to the cart a message should be displayed upon checkout.
The error message I am getting is “There are some issues with the items in your cart. Please go back to the cart page and resolve these issues before checking out.”
add_action( 'woocommerce_check_cart_items', 'wc_min_item_required_qty' );
function wc_min_item_required_qty() {
$categories = array('Noten'); // The targeted product category
$min_item_qty = 5; // Minimum Qty required (for each item)
$display_error = false; // Initializing
// Loop through cart items
foreach(WC()->cart->get_cart() as $cart_item ) {
$item_quantity = $cart_item['quantity']; // Cart item quantity
$product_id = $cart_item['product_id']; // The product ID
// For cart items remaining to "Noten" producct category
if( has_term( $categories, 'product_cat', $product_id ) && $item_quantity < $min_item_qty ) {
wc_clear_notices(); // Clear all other notices
// Add an error notice (and avoid checkout).
wc_add_notice( sprintf( 'Please verify the minimums for your purchase. Your choice requires a minimum of %s. Your cart only contains %s items.', $min_item_qty , $item_quantity ), 'error' );
break; // Stop the loop
}
}
}
I have disabled all plugins, changed the theme nothing seems to help.
Sam Weinstein is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.