如何在Magento2的电子邮件模板中使用循环

3

我在我的邮件模板中使用以下代码:

{{block type='core/template' area='frontend' template='email/files.phtml' links=$links}}

这里是phtml代码:

<?php foreach ($this->getLinks() as $_link): ?>
    <p><?php echo $_link; ?></p>
<?php endforeach; ?>

但是它不起作用。即使我在phtml中写了一些循环之外的内容,也没有显示出来。

3个回答

6

在 Magento 2 中,您可以为事务邮件创建 phtml 模板,并在这些模板中使用变量:

例如,在 app/design/frontend/Vendor/theme/Magento_Sales/email/shipment_new_guest.html

放入以下代码:

{{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."}}

我已添加customMessage

app/design/frontend/Vendor/theme/Magento_Sales/templates/email/shipment/track.phtml

您可以检索变量并在php模板中使用:

<?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">&nbsp;</td>
        </tr>
        <tr>
            <td>
                <p class="center">
                    <?php echo  /* @escapeNotVerified */  __($_customMessage); ?>
                </p>
            </td>
        </tr>
    <?php endif; ?>
    <tr>
        <td height="30" class="row-separator">&nbsp;</td>
    </tr>
        <tr>
        <th class="center">
            <?php  echo /* @escapeNotVerified */  __('Tracking number'); ?>:
        </th>
    </tr>
    <tr>
        <td height="20" class="row-separator">&nbsp;</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">&nbsp;</td>
    </tr>

<?php endif; ?>

3

如何在Magento 2电子邮件模板中循环值(处理自定义电子邮件模板中的数组值)


1. 在您的模块中添加一个布局文件: 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. 在以下路径中创建phtml文件: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. 最后,在您的电子邮件模板中添加以下代码:

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

注意:您的模板变量items应该是一个对象。


2
您提供的是Magento1的内容。 以下代码适用于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/>";
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接