Magento 2 Email – Customize Sales Email, Especially Items.phtml

emailmagento2templatetheme

We are struggling since hours with customization of the Order– and other sales emails. The part with the email templates (e.g. order_new_guest.html etc.) works easily fine.

However, we do not succeed in modifying items.phtml.

We did
– generate a xml sales_email_order_items.xml in <mytemplate>/Magento_Sales/layout

  • tried to reference our items.phtml with <action method="setTemplate">
  • written items.phtml in <mytemplate>/Magento_Sales/email
    but we don't get any change in the emails.

Probably, this is because we don't locate the .xml file in the proper directory? How do we have to reference our items.phtml within this xml-File? And which directory do we put the items.phtml in?

We have understood lots of thing in Magento theme inheritance during the last weeks… but email templates seem to be the next stage 🙁

Best Answer

The way I achieved this is by creating a file called items.phtml here:

my_template/Magento_Sales/templates/email/items.phtml

No XML file necessary. The path to items.phtml is defined in the Magento_Sales module: vendor/magento/module-sales/view/frontend/layout/sales_email_order_items.xml

<block class="Magento\Sales\Block\Order\Email\Items" name="items" template="email/items.phtml" cacheable="false">

You can use this code to test a New order email and you should see the TESTING TEMPLATE and TESTING SKU text appear:

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>

<?php $_order = $block->getOrder() ?>
<?php if ($_order): ?>
    <?php $_items = $_order->getAllItems(); ?>
    <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>
        <?php foreach ($_items as $_item): ?>
            <?php
                if ($_item->getParentItem()) {
                    continue;
                }
            ?>
            <tbody>
                TESTING TEMPLATE <br />
                TESTING SKU: <?= $_item->getSku(); ?>
                <?php foreach($_item as $item_content) { print_r($item_content); } ?>
                <?= $block->getItemHtml($_item) ?>
            </tbody>
        <?php endforeach; ?>
        <tfoot class="order-totals">
            <?= $block->getChildHtml('order_totals') ?>
        </tfoot>
    </table>
    <?php if ($this->helper('Magento\GiftMessage\Helper\Message')->isMessagesAllowed('order', $_order, $_order->getStore()) && $_order->getGiftMessageId()): ?>
        <?php $_giftMessage = $this->helper('Magento\GiftMessage\Helper\Message')->getGiftMessage($_order->getGiftMessageId()); ?>
        <?php if ($_giftMessage): ?>
            <br />
            <table class="message-gift">
                <tr>
                    <td>
                        <h3><?= /* @escapeNotVerified */  __('Gift Message for this Order') ?></h3>
                        <strong><?= /* @escapeNotVerified */  __('From:'); ?></strong> <?= $block->escapeHtml($_giftMessage->getSender()) ?>
                        <br /><strong><?= /* @escapeNotVerified */  __('To:'); ?></strong> <?= $block->escapeHtml($_giftMessage->getRecipient()) ?>
                        <br /><strong><?= /* @escapeNotVerified */  __('Message:'); ?></strong>
                        <br /><?= $block->escapeHtml($_giftMessage->getMessage()) ?>
                    </td>
                </tr>
            </table>
        <?php endif; ?>
    <?php endif; ?>
<?php endif; ?>