Magento – pass custom variable/parameter from email template to phtml file

email-templatesorders

I am stuck in my custom code.

I want to pass a custom variable from email template to pthml file.

Edit file

app/code/local/Mage/Sales/Model/Order.php

in this function :

public function sendNewOrderEmail()
  {

--- default code start ----
$mailer->setTemplateParams(array(
                'order'        => $this,
                'test'        => 'XXXXX',
                'billing'      => $this->getBillingAddress(),
                'payment_html' => $paymentBlockHtml
            )
        );
--- default code end ----
}

and then I put this code in New Order email template:

{{layout handle="sales_email_order_items" order=$order  test=$test}}

template file located here :

app/locale/en_US/template/email/sales/order_new.html

and I am trying to get test variable Here:

app/design/frontend/default/default/template/email/order/items/order/default.phtml

like this: $test = $this->getItem()->getTest()

but get success. Please let me know where am I wrong? or what to do need to access this variable in phtml file?

Thanks in advance!

Best Answer

When passing a variable this way:

{{layout handle="sales_email_order_items" order=$order  test=$test}}

You make it available in the email/order/items.phtml template only.
You are able to get it there with: $this->getTest() or $this->getData('test').
To pass it further to the email/order/items/order/default.phtml edit the items.phtml file and add this

$item->setTest($this->getTest());

before calling

<?php echo $this->getItemHtml($_item) ?>

To make it shorter, replace:

<?php echo $this->getItemHtml($_item) ?>

with

<?php echo $this->getItemHtml($_item->setTest($this->getTest())) ?>