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

如何在Magento2中使用电子邮件模板中的循环,magento2,email-templates,Magento2,Email Templates,我在电子邮件模板中使用以下代码: {{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; ?&g

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

{{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中除了循环之外还写了一些东西,也不会显示出来。

您提供的是针对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/>";
    }
}
{{block class='Magento\\Framework\\View\\Element\\Template'area='frontend'Template='Magento\u Sales::order/email/ordercomments.phtml'order=$order}}

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

例如在app/design/frontend/Vendor/theme/Magento_Sales/email/shipping_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

应用程序内/design/frontend/Vendor/theme/Magento_Sales/templates/email/shipping/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; ?>

:


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


1。在模块中添加布局文件: yourmodule/view/frontend/layout/email_product_list.xml

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

<?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>
<?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>
{{layout handle="email_product_list" items=$items area="frontend"}}