Magento – Getting an item from quote object

magento-1.9quotequoteitem

I have extended Mage_Checkout_Model_Type_Onepage class via my custom module and overriding saveOrder() method. All this code is written inside saveOrder() method.

I am trying to get every item from the current quote object and save it to a new array.

Here is the code:

    $quote = $this->getQuote();

    //following line printing the quote id
    Mage::log("Quote id is : " . $quote->getId() , null, 'mylog.log');

    $myArray = array();

    foreach ($quote->getAllItems() as $item) 
    {
        $myArray[] = $item;
    }

    //====following lines doesn't log anything=========
    Mage::log($myArray,null,'mylog.log',true);
    Mage::log(print_r($myArray,1),null,'mylog.log');

    foreach ($myArray as $currItem) 
    {
        // Empty current quote
        foreach ($quote->getAllItems() as $item) 
        {
            $quote->getItemsCollection()->removeItemByKey($item->getId());
        }

        //inserting item to quote from myArray
        $quote->addItem($currItem);
    }

But I think quote item is not inserting into $myArray and that is why $quote->addItem method is also not working here.

Best Answer

Try Below code and let me know if it is not working

$session = Mage::getSingleton('checkout/session');
foreach ($session->getQuote()->getAllItems() as $item) {
       print_r($item->getData());
}

Or

In your Foreach loop

Write this

$myArray[] = $item->getProduct()

and try again

Related Topic