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

Man*_*Joy 3 email-templates magento2

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

{{block type='core/template' area='frontend' template='email/files.phtml' links=$links}}
Run Code Online (Sandbox Code Playgroud)

这是 phtml 代码:

<?php foreach ($this->getLinks() as $_link): ?>
    <p><?php echo $_link; ?></p>
<?php endforeach; ?>
Run Code Online (Sandbox Code Playgroud)

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

小智 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."}}
Run Code Online (Sandbox Code Playgroud)

我已经添加 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; ?>
Run Code Online (Sandbox Code Playgroud)