1

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?

0
foreach ($items as $item) //..

Your $items variable seems not to be an array... see:

$sortedItems[$_cat_name] = $item;  // <-- is here an $item an array? No.
  • Then how should I insert item to quote object? should I insert $items directly to quote? – vedu Aug 27 '16 at 11:07
  • Try just: $quote->addItem($items); without foreach, you are actually inside one foreach loop. – jimmy Aug 27 '16 at 11:35
  • ok let me try that but in the referenced link foreach is used. – vedu Aug 27 '16 at 11:40
  • I tried this but items are not adding to quote using $quote->addItem($items); – vedu Aug 27 '16 at 14:56
  • @vedu how you added item to quote can you please tell me – Pawankumar Jul 3 '18 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.