I'm trying to get a custom attribute from an order into the new order transactional email template, BUT I need to pass it through some custom PHP for a bit of formatting first.

In the email template I use the following declaration:

{{block type='mymodule/myblock' area='frontend' template='mynamespace/mymodule/mytemplate.phtml'}}

With a block like this:

class Mynamespace_Mymodule_Block_Myblock extends Mage_Core_Block_Template
    public function getValue()
        return "Some value";

and mytemplate.php like this:

echo $this->getValue();

This all works and I get "Some value" appearing where I expect in the transactional email.

The problem I have is that I cannot get any information about the order within the block or template. Using code such as this:

$orderid = Mage::getSingleton('checkout/session')->getLastOrderId();  
$order = Mage::getModel('sales/order')->load($orderid);

just gives me NULL. For some reason by the time it get to processing my block the session data has been cleared.

I've tried changing my block to extend Mage_Sales_Block_Items_Abstract as the core magento email template does, but then when I copy the code in app/design/frontend/base/default/template/email/order/items.phtml to my template the checkout process doesn't complete on step 5 and it just keeps showing the 'place order' button.

1 Answer 1


I think that the issue is with what the function getLastOrderId returns and not actually with the session, this can be tested by mage::log($orderid)

The function getLastOrderId actually returns the order increment id, that is the id that the user sees and not the entity_id on the order table.

But this is no issue as you can load the order by increment id as well as entity_id.


This would mean you could change your code as follows:

$orderid = Mage::getSingleton('checkout/session')->getLastOrderId();  
$order = Mage::getModel('sales/order')->loadByIncrementId($orderid);

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.