This code displays a notice if the minimum amount of variations or quantity is below the number you set in the code. In this example, we force customers to purchase at least 5 different flavors of chocolates to make up a box before they can checkout.
- The code works for both single product quantities for products in a specific category and
- Variations from one specific product using the product id.
Copy and paste the PHP code to the end of your child themes functions file or custom functionality/code snippets plugin.
For use with single products, swap out the category id on line 11 and the minimum quantity on line 12.
For use with a variable product, swap out the product id on line 37 and the minimum amount of line 38.
How The Code Works
here’s an explanation of the functions used in the code:
add_action('woocommerce_check_cart_items', 'force_minimum_quantity_for_variations'): This line of code hooks into the
woocommerce_check_cart_items action in WooCommerce and specifies that the
force_minimum_quantity_for_variations function should be executed when this action is triggered.
function force_minimum_quantity_for_variations(): This is a custom PHP function that we define. It’s responsible for enforcing a minimum quantity for variations of a specific variable product and displaying an error notice if the condition is not met.
$product_id: This variable stores the ID of the variable product for which you want to enforce the minimum quantity rule. You should replace
123 with the actual product ID you’re working with.
$required_quantity: This variable specifies the minimum quantity required for the variations of the specified product. You can change the value to set your desired minimum quantity.
$count: This variable is used to keep track of the total quantity of variations in the cart that match the specified product and meet the criteria.
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item): This loop iterates through all the items in the cart. It retrieves each cart item’s details, including the product data and quantity.
$product = $cart_item['data']: This line extracts the product data from the cart item, allowing us to check its type and parent ID.
if ($product->is_type('variation') && $product->get_parent_id() == $product_id): This condition checks if the product is a variation (
is_type('variation')) and if its parent product ID matches the
$product_id we specified earlier. If both conditions are met, it indicates that the product is a variation of the desired variable product.
$count += $cart_item['quantity']: If a variation that meets the criteria is found, its quantity is added to the
$count variable, keeping track of the total quantity of such variations in the cart.
10. The code continues to loop through all cart items, accumulating the quantities of the eligible variations.
if ($count > 0 && $count < $required_quantity): This condition checks if there are eligible variations in the cart (count > 0) and if the total quantity of these variations is less than the required quantity (
$required_quantity). If these conditions are met, it means the cart does not meet the minimum quantity requirement.
wc_add_notice(sprintf('You must add at least %d chocolates to make up a box before you can checkout.', $required_quantity), 'error'): If the condition is met, this code adds an error notice to WooCommerce with a message indicating that the customer must have at least the specified minimum quantity of the eligible variations in their cart. This prevents the customer from proceeding to checkout until the requirement is met.