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.

Khoa TruongDinh
  • 32,054
  • 11
  • 88
  • 155
vedu
  • 857
  • 5
  • 19
  • 43
  • What is your this class? Please add more code lines about your current class? – Khoa TruongDinh Aug 28 '16 at 06: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 06: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 06:57

3 Answers3

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

Murtuza Zabuawala
  • 14,814
  • 9
  • 44
  • 75
  • Hello @murtuza , after adding $item->getProduct() , I got following error for addItem() method : Fatal error: 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 05: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 06:19
  • I tried this but its is throwing same above error only in network tab for saveOrder() method call. – vedu Aug 28 '16 at 06: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 06: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.

Khoa TruongDinh
  • 32,054
  • 11
  • 88
  • 155
  • 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 07:35
  • So, my answer helped you? – Khoa TruongDinh Aug 28 '16 at 08:01
  • Sorry but it did not work. :( – vedu Aug 28 '16 at 08:07
  • What is your current issue? – Khoa TruongDinh Aug 28 '16 at 08: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 08:16
  • Seem it is a different topic. These answers in this topic cannot help you Getting an item from quote object? Please, accept an answer if it helps you to get an item from quote object - not splitting quote. – Khoa TruongDinh Aug 28 '16 at 08:30
  • Ok. but ->addItem() is also not working. It is not adding a item to quote. – vedu Aug 28 '16 at 08:35
  • Yes I am using $quote->getItemsCollection()->removeItemByKey($item->getId()); but facing the problem while adding back the item from my array. Bacause ->additem() is not working. – vedu Aug 28 '16 at 08:49
  • Try to use $quote->removeItem($item->getId());, and then, try to add again: $quote->addItem($item). – Khoa TruongDinh Aug 28 '16 at 08:53
  • Yes item is removed properly but it is not adding again. I just checked by Mage::log(count( $quote->getAllItems() ),null,'mylog.log'); In strating it displays count properly. after removing it shows count 0 but after that items are not adding. – vedu Aug 28 '16 at 09:01
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 06:56