Magento Wholesale Qty – Add by Case Size

We service a lot of wholesale companies in the UK. Although Magento is a very capable e-commerce system it still requires modification in most instances to make it function exactly how you want it to.

For our wholesale customers we needed a way to change how the “add to cart” feature works to make it easier for customers ordering in bulk.

Most wholesale customers are required to buy products in case quantities, where the quantity is usually something like 6, 12, 48, 144 etc, we normally recommend their inventory quantity is measured in a single unit so our solution to this is to hook into an observer to change the add to cart quantity.

if you would like to do this, You will need to create a new product attribute to store the case size. I called it “case_size”

And then you will need to create an extension to observe “sales_quote_item_qty_set_after” event. (I assume you have experience on creating an event observer!)

Then the following code should do the trick.

$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.

Please also note this will not work out of the box on a multi-store environment but will require a bit more if you are looking for a wholesale store and retail store scenario.

1 Comment

  1. I’ve used this code and it works great! Now we have another problem that when you want to update the QTY of a product then the case_size fills the QTY in product page so if you have a product with case size of 100PCS then when you want to update it will make the total amount of the order to 100 !!! so the customer wanted to buy just 2 cases for example but the system fills 100pcs and not 1.

    Any suggestions on how to solve this problem

    many thanks

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.