/**
* Codeable Task 46269 - Holiday Checkout minimums
* Ensures that the minimum quantities for specific products are respected.
*
* @link https://app.codeable.io/tasks/46269
*/
function aelia_set_min_qty_per_product() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
// Product Id and Min. Quantities per Product
$product_min_qty = array(
22328 => 5, //Braised Turkey Hindquarters
22305 => 5, //Roasted Turkey Breast
22336 => 5, //Shrimp Cocktail
22335 => 5, //Roasted Leg Of Lamb
22332 => 5, //Center-Cut Pork Loin
22330 => 5, //New York Strip Loin
);
// Will hold information about products that have not
// met the minimum order quantity
$bad_products = array();
// Loop through the products in the Cart
foreach( wc()->cart->cart_contents as $product_in_cart ) {
$product_id = $product_in_cart['product_id'];
// If there is a minimum quantity for the product and the cart item is
// below such quantity, add the cart item's product to the "bad" list
if( isset( $product_min_qty[$product_id] ) && ( $product_in_cart['quantity'] < $product_min_qty[$product_id] ) ) {
$bad_products[] = array(
'id' => $product_id,
'in_cart' => $product_in_cart['quantity'],
'min_req' => $product_min_qty[$product_id],
);
}
}
// Time to build our error message to inform the customer
// About the minimum quantity per order.
if( !empty( $bad_products ) ) {
// Lets begin building our message
$message = '<strong>A minimum quantity per product has not been met.</strong><br />';
foreach( $bad_products as $bad_product ) {
// Append to the current message
$message .= get_the_title( $bad_product['id'] ) .' requires a minimum quantity of '
. $bad_product['min_req']
.' lbs. You currently have: '. $bad_product['in_cart'] .' lbs.<br />';
}
wc_add_notice( $message, 'error' );
}
}
}
add_action( 'woocommerce_check_cart_items', 'aelia_set_min_qty_per_product' );
Comments