Mini Tutorial: How to Add Wholesale Quantity to your Cart in Magento

A client recently requested me to change how the “add to cart” feature works. He explained that their customers are required to buy products in a full case, which the quantity can be 6, 12, etc, but their inventory quantity is in a single unit. So I couldn’t just raise the price to match the price of a case. After digging around a bit, I found that there is an observer I can hook into, to change the add to cart quantity.

First, You will need a new product attribute to store the case size. I called it “case_size”

And then you will create an extension to observe “sales_quote_item_qty_set_after” event. I assume you have experience on creating an event observer! If you don’t… Here is a tutorial

The following code is the magic.

$item = $observer->getItem();
if($item->getQtyToAdd())
{
    $case_qty =  floor($item->getQtyToAdd());
    $case_size =  $item->getProduct()->getCaseSize();

    if(Mage::app()->getRequest()->getActionName() == 'reorder') {
        $item->setData('qty',$case_qty);
    }
    else
    {
        $qty = $case_qty * $case_size;
        if($item->getQty() > 0)
        {
            $qty += $item->getQty() - $case_qty;
        }
        $item->setData('qty',$qty);
    }
}
else if($item->getQty())
{
    $caseSize = Mage::getResourceModel('catalog/product')
->getAttributeRawValue($item->getProduct()->getId(), 
'case_size', Mage::app()->getStore());
    
    $origin_qty =  $item->getOrigData('qty');
    $current_qty =  floor($item->getData('qty'));
    if($origin_qty != $current_qty)
    {
         if(!$current_qty) {
              $current_qty  = 1;
         }
        $qty = $current_qty * $caseSize;
        $item->setData('qty',$qty);
    }
}

The above code manipulates the input quantity by multiplying it to the case size. However, there are a few cases that you don’t want to apply the multiplication, e.g the item is being added to cart by the reorder function.

Good luck & have fun!!!