Magento 2 – How to Handle Array Values in Custom Email Templates

emailemail-templatesmagento2

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 ?

Best Answer

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
Related Topic