Question

I added a custom .html email template at THEME\Magento_Email\email\usp_block.html (i also tried THEME\Magento_Email\view\frontend\email\usp_block.html).

Then I added the file THEME/etc/email_templates.xml with this content:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
    <template id="design_email_usp_block_template" label="USP Block" file="usp_block.html" type="html" module="Magento_Email" area="frontend"/>
</config>

Now I added a new template in the Magento Backend at Marketing -> E-Mail Templates and tried to include my template like this:

{{template config_path="design/email/usp_block_template"}}

Then I flushed the cache. But if I preview, then I get this:

Error while filtering the template: The email template 'design_email_usp_block_template' is not defined.

Was it helpful?

Solution 2

The User aligent-skumar on GitHub provided the answer:

MODULE = app/code/NameSpace/ModuleName

  1. Create your template:

MODULE\view\frontend\templates\usp.phtml

<div>This is a test</div>
  1. declare template in email_template.xml:

MODULE/etc/email_templates.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
    <template id="custom_email_usp_template" label="USP Block" file="usp.html" type="html" module="Magento_Email" area="frontend"/>
</config>
  1. Create layout file:

MODULE/view/frontend/layout/custom_email_usp_template.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="Subscription renew notification email items" design_abstraction="custom">
    <block name="test.email.viewmodel" class="Magento\Framework\View\Element\Template" template="NameSpace_ModuleName::usp.phtml">
    
    <!-- OPTIONAL:
        <arguments>
            <argument name="view_model" xsi:type="object">NameSpace\ModuleName\ViewModel\<ViewModelClass></argument>
        </arguments>
    -->
    </block>
</page>
  1. Define E-Mail template definieren MODULE\view\frontend\email\usp.html
 {{template config_path="design/email/header_template"}}
     {{layout handle="custom_email_usp_template" area="frontend"}}  <-- now you can include your template like this
 {{template config_path="design/email/footer_template"}}
  1. Now you can use your template in the backend:

Marketing -> Communication: E-Mail Templates -> Create new template.

You will find this in the dropdown now:

Magento E-Mail:
     USP-Block

OTHER TIPS

I think that what you are trying to do is something similar to how the header and footer blocks are inserted inside email templates. However, for these 2 blocks have their path defined as a system configuration path, it's not a naming convention (see vendor/magento/module-email/etc/di.xml enter image description here

In vendor/magento/framework/Filter/Template.php you have enter image description here

So what I get from this is that if you want to include your custom block the same way that the email header&footer are, as global blocks, then it means that you also have to define it the same way, by declaring the config path where this template is registered and saving the values, given the instruction <PATH> equals the XPATH to the system configuration value that contains the value of the template. Which means that you need a custom module for creating this new global block.


But I am not sure you actually need it to be a global block, you could add a simple email template and load it in admin (select template from dropdown and click button 'load template').

Check this link https://magento.stackexchange.com/a/179107/35330 that can help you. Worth to mention that the 'module' property inside email_templates.xml should point to your custom module, not Magento_Email, or otherwise it will try to look for it inside Magento_Email module and once again, not find it.

Customize email templates:

Email templates are stored in the <module_dir>/view//email directory of their respective modules. For example, the template for the new order transactional email for the Sales module is located in <Magento_Sales_module_dir>/view/frontend/email/order_new.html.

We strongly recommend you not change the default Magento files. If you want to customize the default templates, you should create your custom templates and configure Magento to use them instead of the default templates.

You can add custom templates as physical files in your custom theme or create them using the Magento Admin. Both approaches are described in the following sections.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top