How To Load Email Template By Template ID In Magento 2
I have posted the solution to load Email template by template ID in Magento 2.
You can use the below code if you want to send customers an email with a customized email template.
The solution allows to load the default or custom template using the ID and get the template text.
Method to load Email template by template ID in Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?php namespace [Vendor]\[Module]\Helper; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\Email\Model\Template as coreTemplate; class Data extends AbstractHelper { protected $template; public function __construct( Context $context, coreTemplate $template ) { $this->template = $template; parent::__construct($context); } public function templateText($templateId) { if (is_numeric($templateId)) { $this->template->load($templateId); } else { $this->template->setForcedArea($templateId); $this->template->loadDefault($templateId); } // another method if (is_numeric($templateId)){ $template = $this->template->load($templateId, 'template_id'); }else { $template = $this->template->load($templateId, 'template_code'); } $templateText = $this->template->getTemplateText(); return $templateText; } } |
Create object of Helper Class and call the function.
That’s it. You can also get store name in Magento 2 email template to enhance your brand consistency, build trust with customers and also adds professional touch to your communication.
Any doubts about the implementation? Feel free to mention them in the Comments section below. I’ll help you out asap.
I request the readers to share the above solution with the Magento community via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
Adobe Summit 2020 – Key Takeaways Concerning Magento
How To Bypass CSRF Validation For Certain Requests In Magento 2
Next