0

I am working on creating a new payment integration in Magento and I need to load the current order inside my controller so I can't get the details such as firstname, lastname, email etc. so I can make a post to the paymen gateway's API.

As an exmaple of my problem I have an action:

public function testAction() { 
    echo "<h1>" . Mage::getSingleton('checkout/session')->getLastRealOrderId() . "</h1>";     
$_order = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());   
foreach ($_order->_data as $key => $value) {    echo "$key: $value<br />";}
}

Checking my magento system log it errors on the foreach:

Notice: Trying to get property of non-object

If I create an action:

Notice: Trying to get property of non-object

public function testAction() { 
$this->loadLayout();
    $block = $this->getLayout()->createBlock('Mage_Core_Block_Template',test, array('template' => 'test/test.phtml'));        
    $this->getLayout()->getBlock('content')->append($block);
    $this->renderLayout();
}

and then create the test.phtml view with the same code in it as the controller originally had:

echo "<h1>" . Mage::getSingleton('checkout/session')->getLastRealOrderId() . "</h1>";     
$_order = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());   
foreach ($_order->_data as $key => $value) {    echo "$key: $value<br />";}

Then the page brings back the properties of my order object and I am able to get the information I need from it.

I am new to Magento so I am sure I am missing something really obvious but I would appreciate any help to achieve being able to load the order object inside the controller.

As additional info in case this has any bearing on the issue my controller extends Mage_Core_Controller_Front_Action.

Update

Mage::getSingleton('checkout/session')->getLastRealOrderId() does bring back the correct order id. As it is echoed inside the tag. The problem seems to be the order object doesn't lget loaded, just a new instance created.

2 Answers 2

0

The method getLastRealOrderId return empty if the customer never finish an order.

I think better you load orders of a customer using:

$customer = Mage::getSingleton('customer/session')->getCustomer();
$customer_email = $customer->getEmail();
$_order = Mage::getModel('sales/order')->load($customer_email, 'customer_email');
1
  • Sorry, I should have mentioned that I am actually get the correct order id back. The order just isn't being put into the order object by the load by increment function.
    – John
    Feb 3, 2015 at 9:02
0

It seems that I needed to use the getData() function to get the data out of the order after I had loaded it:

echo "<h1>" . Mage::getSingleton('checkout/session')->getLastRealOrderId() . "</h1>";     
$_order = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());   
$data = $order->getData();
foreach ($data as $key => $value) {    echo "$key: $value<br />";}

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.