2

This is my email_template.html

 <table class="order-details"> <tr> <td class="address-details"> <h3>{{trans "Billing Info"}}</h3> <p>{{var formattedBillingAddress|raw}}</p> </td> {{depend order.getIsNotVirtual()}} <td class="address-details"> <h3>{{trans "Shipping Info"}}</h3> <p>{{var formattedShippingAddress|raw}}</p> </td> {{/depend}} </tr> </table> 

and in my observer

$templateVars = array( 'store' => $this->storeManager->getStore(), 'message' => 'We processed your order ID We will contact you soon in mail for the acknowledgement if you not receive mail within 4 hours please get help from [email protected]', 'order' => $order, 'store'=> $store, 'productName'=> $productName ); ->setTemplateVars($templateVars) 

My question is how can i set values of billing info, shipping info and payment method in this email template.

7
  • Which is event you catch? Commented Sep 16, 2016 at 4:42
  • checkout_onepage_controller_success_action Commented Sep 16, 2016 at 4:44
  • @Ramkishan you ask how to pass/set value in email templates? or how to get values in observer? Commented Sep 16, 2016 at 4:54
  • @Ramkishan, My observer file has code like below, $transport = $objectManager->create('Magento\Framework\Mail\Template\TransportBuilder'); $templateVars = array( 'myvar1' => 'testing data' ); $transport->setTemplateVars($templateVars); But could't retrive this value in my Email... In Email template , {{var myvar1}} Commented Mar 1, 2017 at 13:27
  • You should ask a new question @Vigna. beacuase in comments i can't explain you whole thing. Commented Mar 2, 2017 at 4:06

3 Answers 3

1

Inject class Renderer used for formatting an order address and some classes for Payment into your constructor.

/** * @var \Magento\Sales\Model\Order\Address\Renderer */ protected $addressRenderer; /** * @var \Magento\Payment\Helper\PaymentHelper */ protected $paymentHelper; public function __construct( \Magento\Sales\Model\Order\Address\Renderer $addressRenderer, \Magento\Payment\Helper\PaymentHelper $paymentHelper ) { $this->addressRenderer = $addressRenderer; $this->paymentHelper = $paymentHelper; } 

Add to $templateVars:

$templateVars = array( 'store' => $this->storeManager->getStore(), 'message' => 'We processed your order ID We will contact you soon in mail for the acknowledgement if you not receive mail within 4 hours please get help from [email protected]', 'order' => $order, 'store'=> $store, 'productName'=> $productName, 'payment_html' => $this->getPaymentHtml($order), 'formattedShippingAddress' => $this->getFormattedShippingAddress($order), 'formattedBillingAddress' => $this->getFormattedBillingAddress($order), ); 

Build the methodes for shippping , billing address and payment:

/** * @param Order $order * @return string|null */ protected function getFormattedShippingAddress($order) { return $order->getIsVirtual() ? null : $this->addressRenderer->format($order->getShippingAddress(), 'html'); } /** * @param Order $order * @return string|null */ protected function getFormattedBillingAddress($order) { return $this->addressRenderer->format($order->getBillingAddress(), 'html'); } /** * Get payment info block as html * * @param Order $order * @return string */ protected function getPaymentHtml(Order $order) { return $this->paymentHelper->getInfoBlockHtml( $order->getPayment(), $this->identityContainer->getStore()->getStoreId() ); } 

See a good sample here:

vendor/magento/module-sales/Model/Order/Email/Sender/OrderSender.php vendor/magento/module-sales/view/frontend/email/order_new.html

2
  • Thank you @Khoa i got the values. your post was very helpful. Commented Sep 16, 2016 at 5:29
  • @Khoa TruongDinh, My observer file has code like below, $transport = $objectManager->create('Magento\Framework\Mail\Template\TransportBuilder'); $templateVars = array( 'myvar1' => 'testing data' ); $transport->setTemplateVars($templateVars); But could't retrive this value in my Email... In Email template , {{var myvar1}} Commented Mar 1, 2017 at 13:30
0
$templateVars = array( 'store' => $this->storeManager->getStore(), 'message' => 'We processed your order ID We will contact you soon in mail for the acknowledgement if you not receive mail within 4 hours please get help from [email protected]', 'order' => $order, 'store'=> $store, 'productName'=> $productName ); 

you can use the above variable in email templates like

for write static text you can use -> {{trans "Dear"}}

for include variable you can use -> {{var productName}}

for example you greet your customer in mail templates like

<h1 style="font-size:22px;font-weight:normal;line-height:22px;margin:0 0 11px 0;">{{trans "Dear "}} {{var customer_name}},</h1> 

In this above example customer name is the $templateVars array index. You need to get customer name from observer after that you can pass that value into $templateVars array.

2
  • i need to print billing info and shipping info in my templates. how can i do this ? Commented Sep 16, 2016 at 4:52
  • do you get that values in observer? pls show your full observer code Commented Sep 16, 2016 at 4:54
0

For magento 2.3, replace this

\Magento\Payment\Helper\PaymentHelper $paymentHelper 

with

\Magento\Payment\Helper\Data $paymentHelper 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.