pass custom variable/parameter from email template to phtml file

会有一股神秘感。 提交于 2019-12-06 04:56:09

问题


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 not get success. Please let me know where am I wrong? or what to do need to access this variable in phtml file?


回答1:


Your problem here is that the 'test' value goes to the main block Mage_Sales_Block_Order_Email_Items that uses "email/order/items.phtml" tempalte.

In there you can find the data using:

<?php $test = $this->getTest(); // or $this->getData('test') ?>

You can then add this data into a registry.

But a better way is to send set this info onto the order items before the email. So, in email function before $mailer->setTemplateParams(); add a code like:

//$this = current order if you are in Mage_Sales_Model_Order
foreach ($this->getAllVisibleItems() as $item) {
    $item->setData('test', 'test_value_10');
}



回答2:


To get variable in your email template

{{var test}}

Take a look @ Defining Transactional Variables



来源:https://stackoverflow.com/questions/20786080/pass-custom-variable-parameter-from-email-template-to-phtml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!