Magento – Magento2 – Override Template order summary in sales email order

layoutmagento-2.1magento2PHPtemplate

I have the template that is using the following to render order summary

{{layout handle="sales_email_order_items" order=$order
area="frontend"}}

it is taking the code from the following template

/app/design/frontend/Corra/Mytheme/Magento_Sales/templates/email/items/order/default.phtml

So I want to override this file with my custom module file and use this one.

I created a module "Corra_Sales"

I put the .phtml here

/app/code/Corra/Sales/view/frontend/templates/email/items/order/default.phtml

Also, I created the layout with the file:

/app/code/Corra/Sales/view/frontend/layout/sales_email_order_renderers.xml

The code inside file is:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" label="Email Creditmemo Items List" design_abstraction="custom">
    <body>
        <referenceBlock name="sales.email.order.renderers">
            <block class="Magento\Sales\Block\Order\Email\Items\Order\DefaultOrder" as="default" template="Corra_Sales::email/items/order/default.phtml"/>
        </referenceBlock>
    </body>
</page>

Also, I tried with :

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" label="Email Creditmemo Items List" design_abstraction="custom">
    <body>
        <referenceBlock name="sales.email.order.renderers">
            <arguments>
                <argument name="template" xsi:type="string">Corra_Sales::email/items/order/default.phtml</argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

I clear cache, remove var folder, rebuild, etc.

But it still not working, someone could tell me what I am doing wrong that always take this file ->?

/app/design/frontend/Corra/Mytheme/Magento_Sales/templates/email/items/order/default.phtml

Best Answer

the block referenced by sales.email.order.renderers.default is the one to override the template

both layout definitions below should do the work and the first layout definition is now deprecated:

<?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="sales.email.order.renderers.default">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Mbs_EmailSaleOverride::sales_item.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

below is the new notation to take over the block template:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="sales.email.order.renderers.default">
            <arguments>
                <argument name="template" xsi:type="string">Mbs_EmailSaleOverride::sales_item.phtml</argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

Now, the emails in Magento are more complex than normal pages and I have today written a plugin that takes over the function getTemplate for the block \Magento\Sales\Block\Order\Email\Items\Order\DefaultOrder and this is finally successfully overriding the template as expected:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Block\Order\Email\Items\Order\DefaultOrder">
        <plugin name="takeovertemplate" type="Mbs\EmailSaleOverride\Plugin\OrderItemDefaultRenderer" />
    </type>
</config>

<?php
namespace Mbs\EmailSaleOverride\Plugin;

class OrderItemDefaultRenderer
{
    public function testafterGetTemplate()
    {
        return 'Mbs_EmailSaleOverride::sales_item.phtml';
    }
}