Reputation: 41
I'm using following code in my email template:
{{block type='core/template' area='frontend' template='email/files.phtml' links=$links}}
and here is the phtml code:
<?php foreach ($this->getLinks() as $_link): ?>
<p><?php echo $_link; ?></p>
<?php endforeach; ?>
But its not working. Even when I write something in phtml apart from loop, that is not being shown either.
Upvotes: 3
Views: 9714
Reputation: 63
How to Loop a Value in a Magento 2 Email Template (handling array values in custom email templates)
1. Add a layout file in your 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="email/product.phtml"/>
</body>
</page>
2. Create a phtml file in: yourmodule/view/frontend/template/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>
3. Lastly, add this code in your email template:
{{layout handle="email_product_list" items=$items area="frontend"}}
Note: Your template variable items
should be an object.
Upvotes: 3
Reputation: 333
With Magento 2, you can create phtml templates for transactional emails and use variables in this templates :
For example in app/design/frontend/Vendor/theme/Magento_Sales/email/shipment_new_guest.html
Put this code :
{{block class='Magento\\Framework\\View\\Element\\Template' area='frontend' template='Magento_Sales::email/shipment/track.phtml' shipment=$shipment order=$order customMessage="You can follow the delivery progress using the following tracking number."}}
I've added customMessage
In app/design/frontend/Vendor/theme/Magento_Sales/templates/email/shipment/track.phtml
You can retrieve variables and work with in your php template :
<?php $_shipment = $block->getShipment() ?>
<?php $_order = $block->getOrder() ?>
<?php $_customMessage = $block->getData("customMessage") ?>
<?php if ($_shipment && $_order && $_shipment->getAllTracks()): ?>
<?php if ($_customMessage) : ?>
<tr>
<td height="10" class="row-separator"> </td>
</tr>
<tr>
<td>
<p class="center">
<?php echo /* @escapeNotVerified */ __($_customMessage); ?>
</p>
</td>
</tr>
<?php endif; ?>
<tr>
<td height="30" class="row-separator"> </td>
</tr>
<tr>
<th class="center">
<?php echo /* @escapeNotVerified */ __('Tracking number'); ?>:
</th>
</tr>
<tr>
<td height="20" class="row-separator"> </td>
</tr>
<tr>
<td>
<?php foreach ($_shipment->getAllTracks() as $_item): ?>
<p class="center">
<strong><?= /* @escapeNotVerified */ __('Shipped By') ?></strong>: <?= $block->escapeHtml($_item->getTitle()) ?>
</p>
<p class="center">
<strong><?= /* @escapeNotVerified */ __('Tracking Number') ?></strong>: <?= $block->escapeHtml($_item->getNumber()) ?>
</p>
<?php endforeach ?>
</td>
</tr>
<tr>
<td height="30" class="row-separator row-hr"> </td>
</tr>
<?php endif; ?>
Upvotes: 6
Reputation: 21
What you provided is for Magento1. Below code will work for Magento2:
{{block class='Magento\\Framework\\View\\Element\\Template' area='frontend' template='Magento_Sales::order/email/ordercomments.phtml' order=$order}}
<?php
/** @var Magento\Sales\Model\Order $order */
$order = $this->getOrder();
if(count($order->getVisibleStatusHistory())>0){
/** @param \Magento\Sales\Model\Order\Status\History $history */
foreach($order->getVisibleStatusHistory() as $history){
echo "". $history->getComment() . "<br/>";
}
}
Upvotes: 2