10

I can successfully convert convert an order into quote but I still couldn't load it into current cart. I tried several methods but to no avail. Here's the simplest one:

$_quote = Mage::getModel('sales/convert_order')->toQuote($order)->save();
$_quote = Mage::getModel('sales/quote')->load($_quote->getId());
$_quote->setIsActive(true)->save();

I was able to get the quote ID but the quote object doesn't load to the current cart.

I also tried

$_quote = Mage::getModel('sales/convert_order')->toQuote($order)->save();
$_quote = Mage::getModel('sales/quote')->load($_quote->getId());

if ($_checkoutQuote->getId() != $_quote->getId() && $_quote->getId()) {
    $_checkoutQuote->merge($_quote)->save();
    Mage::getModel('checkout/session')
                    ->setQuote($_checkoutQuote)
                    ->init();
    Mage::getSingleton('checkout/session')->getQuote()->setIsActive(true)->save();
}

and similar approaches but still not loading the quote. Thank you very much!

2 Answers 2

9

@user1240207

Please use the below code. It will may help you.

Mage::getSingleton('checkout/session')->setQuoteId($_quote->getId());

Code convert an order to quote please used below:

   $quote = Mage::getModel('sales/quote')->load($order->getQuoteId());
    if ($quote->getId()) {
        $quote->setIsActive(1)
            //->setReservedOrderId(null)
            ->save();
        Mage::getSingleton('checkout/session')
            ->replaceQuote($quote);
           // ->unsLastRealOrderId();
    }

Here the process:

Get Current quote

$currenQuoteId=Mage::getSingleton('checkout/session')->getQuoteId();

Convert Order to quote

$OrderQuote = Mage::getModel('sales/quote')->load($order->getQuoteId());

Active Quote

if ($OrderQuote>getId()) {
    $OrderQuote>setIsActive(1)
        //->setReservedOrderId(null)
        ->save();
}

Merge your current quote and order reverse Quote

if ($OrderQuote->getId() && $currenQuoteId != $OrderQuote->getId()) {
    if ($currenQuoteId) {
    $OrderQuote->merge(Mage::getSingleton('checkout/session')->getQuote())
        ->collectTotals()
        ->save();
    }

set quote to checkout session

Mage::getSingleton('checkout/session')->setQuoteId($OrderQuote->getId());

 if (Mage::getSingleton('checkout/session')->_quote) {
            Mage::getSingleton('checkout/session')->delete();
        }
       Mage::getSingleton('checkout/session') = $OrderQuote;
    } else {
        Mage::getSingleton('checkout/session')->getQuote()->getBillingAddress();
        Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress();
        Mage::getSingleton('checkout/session')->getQuote()->setCustomer(Mage::getSingleton('customer/session')->getCustomer())
            ->setTotalsCollectedFlag(false)
            ->collectTotals()
            ->save();
    }
4
  • Y? what down? please anybody tell us?
    – Keyul Shah
    Commented Nov 7, 2014 at 7:29
  • No idea how it was downvoted. But I also checked if it would work, unfortunately it didn't do the trick. Commented Nov 7, 2014 at 7:34
  • @KeyulShah,I did not know why some people are down vote without any reason
    – Amit Bera
    Commented Nov 7, 2014 at 7:35
  • @AmitBera : hmm Better Luck Next time.
    – Keyul Shah
    Commented Nov 7, 2014 at 7:37
3

The previous answer only works if the quote is still available in the DB. I ran into a situation where this was no longer the case, so this code doesn't work:

$quote = Mage::getModel('sales/quote')->load($order->getQuoteId());
$quote->getId() => This is empty, in fact the whole model is empty!

Instead, we can use the Mage_Sales_Model_Convert_Order to convert an existing order back into a new quote using the toQuote(Mage_Sales_Model_Order $order) method. It takes an order as input and outputs a new quote object.

You do need to call extra functions for converting the billing/shipping address, items, etc. For each object you will find a function in this class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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