4

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' => "ramkishan.suthar@ranosys.com", '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 ?

  • try {{var productName.some}} here is some is "array key". eg: array([some] => Your product Name) – MeenakshiSundaram R Sep 19 '16 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 ? – Ramkishan Suthar Sep 19 '16 at 6:38
  • you want to loop the values? – MeenakshiSundaram R Sep 19 '16 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 ? – Ramkishan Suthar Sep 19 '16 at 6:43
  • could you give example array value of productName – MeenakshiSundaram R Sep 19 '16 at 6:44
4

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
  • phtml file is not calling # MeenakshiSundaram R – Ramkishan Suthar Sep 19 '16 at 9:04
  • is there anything i need to add ? – Ramkishan Suthar Sep 19 '16 at 9:04
  • Did you place the files in correct folder? if yes clear the cache and try – MeenakshiSundaram R Sep 19 '16 at 9:07
  • yes i did the same and clear the cache – Ramkishan Suthar Sep 19 '16 at 9:12
  • 1
    @sumeetbajaj you should add the object/array in the email template variable – MeenakshiSundaram R Jun 28 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.