Magento 1.7 – How to Set Order Object in Session Quote

magento-1.7orderssession

I'm trying to set an $order object to the session_quote. However, when I get the $order object after setting it, the data and other information are null yet the object remains. How can I set the order object to the session_quote correctly so that the data is still within the order object?

Example:

$order= Mage::getModel('sales/order')->load($orderId);
Mage::getSingleton('adminhtml/session_quote')->setOrder($order);

Getting order from session:

$session = Mage::getSingleton('adminhtml/session_quote');
$sessionOrder = $session->getOrder();

Results in order object with no data or payment information.

$orderId = $sessionOrder->getId(); /* is null */
$orderData = $sessionOrder->getData(); /* is null */

Best Answer

in Mage_Adminhtml_Model_Session_Quote ->getOrder() is not a magic method, but a real method exists in the class:

/**
 * Retrieve order model object
 *
 * @return Mage_Sales_Model_Order
 */
public function getOrder()
{
    if (is_null($this->_order)) {
        $this->_order = Mage::getModel('sales/order');
        if ($this->getOrderId()) {
            $this->_order->load($this->getOrderId());
        }
    }
    return $this->_order;
}

From the logic above, is is apparent why you are getting a null'd order Object.

What you need to do is simply store the OrderId into the session object. When you call for getOrder(), it will then populate the order correctly in the result. See code in getOrder() method.

Thus use:

Mage::getSingleton('adminhtml/session_quote')->setOrderId($orderId);

When things don't work as expected, go have a look at the class that you are using.

Not all methods are magic methods in magento, sometimes a method can be a magic method in once class, and not in another. (one of the features that can make magento code both confusing, and powerful to extend)

Related Topic