4

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.

  • What is your this class? Please add more code lines about your current class? – Khoa TruongDinh Aug 28 '16 at 6:02
  • 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. – vedu Aug 28 '16 at 6:04
  • Your code lines also make me confused. You tried to remove the current quote items and then tried to add them again. :) – Khoa TruongDinh Aug 28 '16 at 6:57
3

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

  • Hello @murtuza , after adding $item->getProduct() , I got following error for addItem() method : Fatal error</b>: Uncaught TypeError: Argument 1 passed to Mage_Sales_Model_Quote::addItem() must be an instance of Mage_Sales_Model_Quote_Item, instance of Mage_Catalog_Model_Product given – vedu Aug 28 '16 at 5:57
  • $cart = Mage::getModel('checkout/cart')->getQuote(); foreach ($cart->getAllItems() as $item) { $myArray [] = $item->getProduct(); } try this one and let me know – Murtuza Zabuawala Aug 28 '16 at 6:19
  • I tried this but its is throwing same above error only in network tab for saveOrder() method call. – vedu Aug 28 '16 at 6:26
  • Products are stored into $myArray. Now those products from $myArray are not inserting into quote object using ->addItem() method. It is throwing : Uncaught TypeError: Argument 1 passed to Mage_Sales_Model_Quote::addItem() must be an instance of Mage_Sales_Model_Quote_Item, instance of Mage_Catalog_Model_Product given – vedu Aug 28 '16 at 6:34
2

As far as I know, if we overridden Mage_Checkout_Model_Type_Onepage, we don't need to use Mage::getSingleton('checkout/session') to get quote items. This is because $this->getQuote() is equal to the quote checkout session:

    #app/code/core/Mage/Checkout/Model/Type/Onepage.php 

    /**
     * Quote object getter
     *
     * @return Mage_Sales_Model_Quote
     */
    public function getQuote()
    {
        if ($this->_quote === null) {
            return $this->_checkoutSession->getQuote();
        }
        return $this->_quote;
    }

So, shortly, your code should be:

$quote = $this->getQuote();
$myArray = $quote->getAllItems();

foreach($myArray as $item) {
    ....
    //Add new quote item
    /** @var Mage_Sales_Model_Quote_Item $item **/
    $quote->addItem($item);
}

P.S: Your code lines also make me confused. You tried to remove the current quote items and then tried to add them again.

  • Yes I am trying to split the order that's why I am trying to get items from the current quote and saving them into an array. Then using items from that array generating new orders. – vedu Aug 28 '16 at 7:35
  • So, my answer helped you? – Khoa TruongDinh Aug 28 '16 at 8:01
  • Sorry but it did not work. :( – vedu Aug 28 '16 at 8:07
  • What is your current issue? – Khoa TruongDinh Aug 28 '16 at 8:10
  • thank you for your help @Khoa . Check the issue here . Please let me know if you need any explanation. – vedu Aug 28 '16 at 8:16
1
$cart_items = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();

                foreach( $cart_items as $items )
                {
                     if($items->getTypeId() != 'configurable')
                     { 
                            $pro_id=$items->getProductId();

                     }
                }
  • This code working on my client's live site. – krishna singh Aug 29 '16 at 10:59
  • thank you for your answer @krishna.. the problem is items are not adding to quote via ->addItem() – vedu Aug 29 '16 at 16:39
  • $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('default')->getId()); $_product = Mage::getModel('catalog/product')->load($product_id); $product_info = array('qty'=>1); $quote->addProduct($product, new Varien_Object($product_info)); $quote->save(); – krishna singh Aug 31 '16 at 6:56

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.