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

时间:2017-01-16 07:28:39

标签: 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; ?>

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

3 个答案:

答案 0 :(得分: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/>";
    }
}

答案 1 :(得分:2)

使用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; ?>

答案 2 :(得分:2)

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

<强> 1。在模块中添加布局文件: yourmodule /视图/前端/布局/ 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 应该是一个对象。