Setting Minimum Order Quantity (MOQ) for Specific Categories in Magento
As a senior developer specializing in e-commerce platforms like Magento, I frequently encounter requests to implement minimum order quantities based on product categories. While the core configuration allows setting MOQs per customer group globally, it doesn’t provide this functionality at the category level out-of-the-box.
However, with some custom development, we can achieve this requirement efficiently. Let me guide you through the process of implementing a minimum quantity restriction for specific categories in Magento.
Step 1: Create an Extension Attribute
First, let’s create an extension attribute to store the MOQ value for each category.
- Navigate to Stores > Attributes > Product.
- Click on “Add New Attribute.”
- Enter ‘minimum_order_qty’ as the attribute code and fill in other details like frontend label, input type (Dropdown), etc. Ensure you select ‘Global’ scope.
- Save the new attribute.
Step 2: Configure MOQ for Each Category
Now, let’s configure the MOQ for each desired category using this extension attribute.
- Go to Stores > Categories.
- Find and open the specific category (e.g., Jewellery or Bra).
- In the “Display Settings” tab, you’ll now see a new field called “Minimum Order Quantity.”
- Enter the required MOQ value for that particular category.
Step 3: Implement Custom Validation
To enforce the configured MOQ during checkout, we need to create a custom validation rule.
- Navigate to your project’s app/code folder.
- Create a new local module in Magento (if you haven’t already).
For this example, let’s assume you have created a module called ‘Mymodule_Cart’.
- In the Mymodule/Cart/etc folder, open or create the config.xml file.
- Add the following XML code to define a custom validation rule:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/config.xsd">
<global>
<sales>
<quote>
<totals>
<mymodule_min_qty_validate before="grand_total">
<class>Mymodule\Cart\Model\Quote\Total\MinQtyValidator</class>
<after>
<custom_fee />
</after>
</mymodule_min_qty_validate>
</totals>
</quote>
</sales>
</global>
</config>
- Create the validation class:
- In the Mymodule/Cart/Model/Quote/Total folder, create a new file named MinQtyValidator.php.
- Implement the validate method to check if the item quantities meet the configured MOQ for their respective categories.
Step 4: Test Your Customization
Now, when customers add items from the specified category to their cart and proceed to checkout, Magento will enforce the configured minimum order quantity. If the customer tries to purchase less than the required amount, they will encounter an error message preventing them from proceeding until the MOQ is met.
This approach allows you to apply unique minimum order quantities for each individual category in your Magento store, providing a flexible solution tailored to your specific business requirements.
For more advanced customizations and best practices regarding Magento development, I recommend exploring the resources available at Laravel Company, which offers expert guidance on optimizing e-commerce platforms like Magento.