I am trying to split an order into multiple orders by referencing Magento multiple order on one checkout or order splitting.
I have extended Mage_Checkout_Model_Type_Onepage class
and written the new code in saveOrder
method.
class My_Module_Model_Type_Onepage extends Mage_Checkout_Model_Type_Onepage
{
public function saveOrder()
{
Mage::log('saveOrder() is called from mymodule....',null,'mylog.log');
$quote = $this->getQuote();
//First build an array with the items split by category
$sortedItems = array();
foreach($quote->getAllItems() as $item)
{
$product = $item->getProduct();
$cats = $product->getCategoryIds(); //every product has single category
foreach($cats as $category_id)
{
//$vendor = $item->getProduct()->getVendor(); // <- whatever you need
$_cat = Mage::getModel('catalog/category')->load($category_id) ;
$_cat_name = $_cat->getName();
if (! isset($sortedItems[$_cat_name]))
{
$sortedItems[$_cat_name] = $item;
}
}
}
foreach ($sortedItems as $cat => $items)
{
// Empty quote
foreach ($quote->getAllItems() as $item)
{
$quote->getItemsCollection()->removeItemByKey($item->getId());
Mage::log('removing from cart',null,'mylog.log');
}
foreach ($items as $item)
{
$quote->addItem($item);
Mage::log('adding to cart',null,'mylog.log');
}
// Update totals for categories
$quote->setTotalsCollectedFlag(false)->collectTotals();
// Delegate to parent method to place an order for each categpry
parent::saveOrder();
}
return $this;
}
}
Here, I am trying to split an order by categories. This newly written method is called when I click on place an order button.
But in the above code, adding to cart
text message is not printing in the log file. removing from cart
message is present in the log file. (I think, it means item is removed from the quote but again that item is not added).
Can anyone tell me what I am doing wrong here?