How to overide “Magento_Sales::email/items.phtml” in custom module?
-
15-04-2021 - |
题
I am trying to override core template file
Magento_Sales::email/items.phtml
in custom module, but not sure how to start Any thoughts please.
i have created a layout file in view/frontend/layout/sales_email_order_items.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">
<body>
<referenceBlock name="order_email_items">
<action method="setEmailItems">
<argument name="emailItems" xsi:type="string">Emp_Sales::email/items.phtml</argument>
</action>
</referenceBlock>
<referenceBlock name="order_totals">
<action method="setOrderTotals">
<argument name="orderTotals" xsi:type="string">Emp_Sales::order/totals.phtml</argument>
</action>
</referenceBlock>
</body>
</page>
Thankyou
解决方案
Please implement below code to override template file.
Step 1: Please create file sales_email_order_items.xml file under path PackageName/Module/view/frontend/layout
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance dc" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<referenceBlock name="items">
<action method="setTemplate">
<argument name="template" xsi:type="string">PackageName_Module::email/items.phtml</argument>
</action>
</referenceBlock>
<referenceBlock name="order_totals">
<action method="setTemplate">
<argument name="template" xsi:type="string">PackageName_Module::order/totals.phtml</argument>
</action>
</referenceBlock>
</page>
Step 2: Please create items.phtml file under path PackageName/Module/view/frontend/templates/email/
Copy paste all code from core file Magento_Sales::email/items.phtml and edit code now in your custom phtml file according to your requirement.
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// phpcs:disable Magento2.Templates.ThisInTemplate
/** @var $block \Magento\Sales\Block\Order\Email\Items */
?>
<?php $_order = $block->getOrder() ?>
<?php if ($_order) : ?>
<?php $_items = $_order->getAllItems(); ?>
<table class="email-items">
<thead>
<tr>
<th class="item-info">
<?= $block->escapeHtml(__('Items')) ?>
</th>
<th class="item-qty">
<?= $block->escapeHtml(__('Qty')) ?>
</th>
<th class="item-price">
<?= $block->escapeHtml(__('Price')) ?>
</th>
</tr>
</thead>
<?php foreach ($_items as $_item) : ?>
<?php if (!$_item->getParentItem()) : ?>
<tbody>
<?= $block->getItemHtml($_item) ?>
</tbody>
<?php endif; ?>
<?php endforeach; ?>
<tfoot class="order-totals">
<?= $block->getChildHtml('order_totals') ?>
</tfoot>
</table>
<?php if ($this->helper(\Magento\GiftMessage\Helper\Message::class)
->isMessagesAllowed('order', $_order, $_order->getStore())
&& $_order->getGiftMessageId()
) : ?>
<?php $_giftMessage = $this->helper(\Magento\GiftMessage\Helper\Message::class)
->getGiftMessage($_order->getGiftMessageId()); ?>
<?php if ($_giftMessage) : ?>
<br />
<table class="message-gift">
<tr>
<td>
<h3><?= $block->escapeHtml(__('Gift Message for this Order')) ?></h3>
<strong><?= $block->escapeHtml(__('From:')) ?></strong> <?= $block->escapeHtml($_giftMessage->getSender()) ?>
<br /><strong><?= $block->escapeHtml(__('To:')) ?></strong> <?= $block->escapeHtml($_giftMessage->getRecipient()) ?>
<br /><strong><?= $block->escapeHtml(__('Message:')) ?></strong>
<br /><?= $block->escapeHtml($_giftMessage->getMessage()) ?>
</td>
</tr>
</table>
<?php endif; ?>
<?php endif; ?>
<?php endif; ?>
Step 3: Please create totals.phtml file under path PackageName/Module/view/frontend/templates/order/
Copy paste all code from core file Magento_Sales::order/totals.phtml and edit code now in your custom phtml file according to your requirement.
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* @var $block \Magento\Sales\Block\Order\Totals
* @see \Magento\Sales\Block\Order\Totals
*/
?>
<?php foreach ($block->getTotals() as $_code => $_total) : ?>
<?php if ($_total->getBlockName()) : ?>
<?= $block->getChildHtml($_total->getBlockName(), false) ?>
<?php else :?>
<tr class="<?= $block->escapeHtmlAttr($_code) ?>">
<th <?= /* @noEscape */ $block->getLabelProperties() ?> scope="row">
<?php if ($_total->getStrong()) : ?>
<strong><?= $block->escapeHtml($_total->getLabel()) ?></strong>
<?php else : ?>
<?= $block->escapeHtml($_total->getLabel()) ?>
<?php endif ?>
</th>
<td <?= /* @noEscape */ $block->getValueProperties() ?> data-th="<?= $block->escapeHtmlAttr($_total->getLabel()) ?>">
<?php if ($_total->getStrong()) : ?>
<strong><?= /* @noEscape */ $block->formatValue($_total) ?></strong>
<?php else : ?>
<?= /* @noEscape */ $block->formatValue($_total) ?>
<?php endif?>
</td>
</tr>
<?php endif; ?>
<?php endforeach?>
其他提示
Declare in di.xml
<preference for="Magento\Sales\Block\Order\Email\Items" type="Emp\Sales\Block\Order\Email\Items" />
Then create a class
Emp\Sales\Block\Order\Email\Items
<?php
namespace Emp\Sales\Block\Order\Email;
class Items extends \Magento\Sales\Block\Order\Email\Items
{
}
Then Run Following commands
php bin/magento setup:upgrade &&
php bin/magento setup:di:compile &&
php bin/magento setup:static-content:deploy -f &&
php bin/magento cache:clean