6

This is my observer from where i am passing product name,prodcut price and product quantity in my custom email template file email_template.html .

$templateVars = array( 'store' => $this->storeManager->getStore(), 'order' => $order, 'items'=> $items, 'productName'=>$productName, 'productPrice'=>$productPrice, 'productQuantity'=>$productQuantity, 'payment_html' => $this->getPaymentHtml($order), 'formattedShippingAddress' => $this->getFormattedShippingAddress($order), 'formattedBillingAddress' => $this->getFormattedBillingAddress($order), ); $from = array('email' => "[email protected]", 'name' => 'Ramkishan'); $this->inlineTranslation->suspend(); $to = array($customerEmail); $transport = $this->_transportBuilder->setTemplateIdentifier('order_template') ->setTemplateOptions($templateOptions) ->setTemplateVars($templateVars) ->setFrom($from) ->addTo($to) ->getTransport(); $transport->sendMessage(); $this->inlineTranslation->resume(); 

In my email_template.html

<table class="email-items"> {{layout handle="email_product_list" items=$items area="frontend"}} </table> 

email_product_list.xml

product.phtml

<?php $_items = $block->getItems() ?> <table class="email-items"> <thead> <tr> <th class="item-info"> <?= /* @escapeNotVerified */ __('Items'); ?> </th> <th class="item-qty"> <?= /* @escapeNotVerified */ __('Qty'); ?> </th> <th class="item-price"> <?= /* @escapeNotVerified */ __('Price'); ?> </th> </tr> </thead> <tbody> <?php foreach ($_items as $_item): ?> <tr> <td><?php $_item->getName() ?></td> <td><?php $_item->getSku() ?></td> <td><?php $_item->getPrice() ?></td> </tr> <?php endforeach; ?> </tbody> </table> 

what i am doing wrong ?

8
  • try {{var productName.some}} here is some is "array key". eg: array([some] => Your product Name) Commented Sep 19, 2016 at 6:35
  • i think it will print only one value. suppose i have more than one element in array than what will i do ? Commented Sep 19, 2016 at 6:38
  • you want to loop the values? Commented Sep 19, 2016 at 6:39
  • i mean to say if i have more than one element in productName array than according to your suggestion it will print only first value in email template. how could i print another values ? Commented Sep 19, 2016 at 6:43
  • could you give example array value of productName Commented Sep 19, 2016 at 6:44

1 Answer 1

8

To loop the values in Email template

Step 1: you need to add one layout file in you module.

yourmodule/view/frontend/layout/email_product_list.xml

<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" label="Email Product List" design_abstraction="custom"> <body> <block class="Magento\Framework\View\Element\Template" name="additional.product.info" template="Vendor_Module::email/product.phtml"/> </body> </page> 

Step 2: Create Phtml file in yourmodule/view/frontend/templates/email/product.phtml

<?php $items = $block->getItems() ?> <table class="email-items"> <thead> <tr> <th class="item-info"> <?= /* @escapeNotVerified */ __('Items'); ?> </th> <th class="item-qty"> <?= /* @escapeNotVerified */ __('Qty'); ?> </th> <th class="item-price"> <?= /* @escapeNotVerified */ __('Price'); ?> </th> </tr> </thead> <tbody> <?php foreach ($items as $item): ?> <tr> <td><?= $item->getName() ?></td> <td><?= $item->getSku() ?></td> <td><?= $item->getPrice() ?></td> </tr> <?php endforeach; ?> </tbody> </table> 

Then in your email template add the below code

{{layout handle="email_product_list" items=$items area="frontend"}}

Note : Your template variable items should be object

  • php bin/magento setup:di:compile
  • php bin/magento clear:cache
16
  • phtml file is not calling # MeenakshiSundaram R Commented Sep 19, 2016 at 9:04
  • is there anything i need to add ? Commented Sep 19, 2016 at 9:04
  • Did you place the files in correct folder? if yes clear the cache and try Commented Sep 19, 2016 at 9:07
  • yes i did the same and clear the cache Commented Sep 19, 2016 at 9:12
  • 1
    @sumeetbajaj you should add the object/array in the email template variable Commented Jun 28, 2019 at 13:55

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.