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.
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.
Get Weekly Updates
Never miss Magento tips, tricks, tutorials, and news.
Thank you for subscribing.
Something went wrong.