Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Forcing multiple quantaties of items in cart
 
Mad Martigan
Member
 
Total Posts:  44
Joined:  2008-08-17
Medford, OR USA
 

Hi everyone.

This post will give some guidelines on how to modify Magento to force the customer to order only in multiple quantities on some items.  We use this for our store on some small items and we set the “minimum qty in cart” in Magento to force a minimum order amount.  However, not only do we need the customer to order a minimum amount but we also only sell certain products in multiples (like 10, 20, 30 etc) So, the hack is quite easy.

These instructions are going to be terse since I’m assuming the reader is familiar with how to create local modules and extend Magento classes.

1) Find class Mage_CatalogInventory_Model_Stock_Item which is located in Mage/CatalogInventory/Model/Stock/Item.php

2) copy this class to a local module for editing.

3) Add this code to the class…

if ($this->getMinSaleQty()) {
            
if ($qty $this->getMinSaleQty() != 0{
                $result
->setHasError(true)
                    ->
setMessage($helper->__('This product can only be ordered in multiples of %s.'$this->getMinSaleQty() * 1))
                    ->
setQuoteMessage($helper->__('Some products can only  be ordered in multiples, (see below)'))
                    ->
setQuoteMessageIndex('qty');
                return 
$result;
            
}
        }

4) modify and item and set the minimum qty in cart.  This will also be the qty multiplier for this item.  It would be easy to add a check for an attribute other than minimum qty in cart but for our needs they are always related so we just set a minimum qty in cart and that will always be the multiplier.

5) Load your item in the cart and see what happens.

Enjoy!!!!

Marty

 
Magento Community Magento Community
Magento Community
Magento Community
 
dudnik
Jr. Member
 
Total Posts:  3
Joined:  2009-07-07
 

very good work

 
Magento Community Magento Community
Magento Community
Magento Community
 
whebden
Jr. Member
 
Total Posts:  4
Joined:  2009-06-27
 

Thanks Mad Mardigan for getting me started!  Here was my solution for version 1.3:

Edit app/code/core/Mage/CatalogInventory/Model/Stock/Item.php and add the following code:

if ($this->getMinSaleQty() && ($qty) % $this->getMinSaleQty() != 0{
    $result
->setHasError(true)
         ->
setMessage(Mage::helper('cataloginventory')->__('This product can only be ordered in multiples of %s.'$this->getMinSaleQty() * 1))
         ->
setQuoteMessage(Mage::helper('cataloginventory')->__('Products must be ordered in correct multiples.'))
         ->
setQuoteMessageIndex('qty');
         return 
$result;
        
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
boxer1732
Jr. Member
 
Total Posts:  14
Joined:  2009-02-04
 
whebden - 21 January 2010 07:09 PM

Thanks Mad Mardigan for getting me started!  Here was my solution for version 1.3:

Edit app/code/core/Mage/CatalogInventory/Model/Stock/Item.php and add the following code:

if ($this->getMinSaleQty() && ($qty) % $this->getMinSaleQty() != 0{
    $result
->setHasError(true)
         ->
setMessage(Mage::helper('cataloginventory')->__('This product can only be ordered in multiples of %s.'$this->getMinSaleQty() * 1))
         ->
setQuoteMessage(Mage::helper('cataloginventory')->__('Products must be ordered in correct multiples.'))
         ->
setQuoteMessageIndex('qty');
         return 
$result;
        
}

Thanks all - the code above worked great for us!  Thanks for saving me a ton of time.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Pandeo
Jr. Member
 
Avatar
Total Posts:  19
Joined:  2010-06-15
Latvia
 

I tried to check on 1.7.0.2 but can’t find where to add the code.
Can you give a hint please?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Tylerzhang
Jr. Member
 
Total Posts:  3
Joined:  2011-11-16
 

under the method checkQuoteItemQty()

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top