Setting Minimum Order Quantity for Individual Simple Products in Magento
In Magento, you can set minimum order quantities (MOQ) for products through quantity discounts or forced quantity increments. However, these methods don’t work well when the MOQ applies to individual, non-existent SKUs. Here’s how you can enforce a minimum order quantity for unique, simple products.
Understanding the Problem
Let’s say you have a product like “Custom Printed T-shirts” that requires a minimum of 50 pieces per print run. Each print job is considered an individual item with its own SKU. Even though the product can physically exist in smaller quantities, the economics and logistics require a minimum order.
The issue arises when customers attempt to order a single “Custom Printed T-shirt” without realizing the MOQ requirement. This leads to frustrated customers and administrative overhead for explaining the refund process.
Solving the Issue: Custom Plugin
To solve this problem, you need to create a custom plugin that intercepts the checkout process before placing an order. Here’s a step-by-step guide:
-
Create a new module: Use Magento’s module creator tool or manually create a new module in
app/code/Vendor/MinimumOrderQuantity. -
Override core functionality: In your module, override the
sales_order_place_beforeevent handler.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_MinimumOrderQuantity" setup_version="1.0.0">
<sequence>
<module name="Magento_Checkout"/>
</sequence>
</module>
</config>
- Create the event handler: In
app/code/Vendor/MinimumOrderQuantity/etc/events.xml, define your new event.
<event name="sales_order_place_before">
<observer name="vendor_minimum_order_quantity_observer" instance="Vendor\MinimumOrderQuantity\Observer\PlaceOrderBefore"/>
</event>
- Check for MOQ violations: In
app/code/Vendor/MinimumOrderQuantity/Observer, create the observer that will check if any product in the cart violates the minimum order quantity.
public function execute(\Magento\Framework\Event\Observer $observer)
{
$order = $observer->getEvent()->getOrder();
$items = $order->getAllItems();
foreach ($items as $item) {
// Load your custom MOQ data for this product (e.g., from a database or configuration)
$minimumQuantity = 50; // Example minimum quantity
if ($item->getQty() < $minimumQuantity) {
throw new \Exception("Minimum order quantity of {$minimumQuantity} is required for '{$item->getName()}'. Please adjust your quantity.");
}
}
return true;
}
Conclusion
Enforcing a minimum order quantity for unique, individual products in Magento requires custom development. By creating a plugin to intercept the checkout process before an order is placed, you can validate each item against the required MOQ.
This approach allows you to maintain flexibility in your product offerings while preventing customer frustration and administrative overhead from refunding orders with invalid quantities.