4

I have added a new Magento 2 email template from Marketing->Communications->Email Templates.

How do I get a list of them in my custom extension code? What class/model do I need to call to be able to get them all into an array, or at least load one template by template ID.

Thanks!

user56885
  • 41
  • 1
  • 3

2 Answers2

5
  $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
  $templateConfig = $objectManager->get('Magento\Email\Model\Template\Config');

  $availableTemplates = $templateConfig->getAvailableTemplates();
  print_r($availableTemplates);
May Noppadol
  • 257
  • 2
  • 7
4

If you just want to get your custom templates you added in the backend, you can use Magento\Email\Model\ResourceModel\Template\CollectionFactory.php. If you want all email templates from config, you can use Magento\Email\Model\Template\Config.php

namespace Vendor\Module\Helper;

use Magento\Email\Model\ResourceModel\Template\CollectionFactory;
use Magento\Email\Model\Template\Config;

class Data extends \Magento\Framework\App\Helper\AbstractHelper 
{
    /** @var CollectionFactory  */
    protected $_collectionFactory;

    /** @var Config  */
    protected $_emailConfig;

    /**
    * @param \Magento\Framework\App\Helper\Context $context
    * @param CollectionFactory $collectionFactory
    * @param Config $emailConfig
    */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        CollectionFactory $collectionFactory,
        Config $emailConfig,
        array $data = []
    )
    {
        $this->_collectionFactory = $collectionFactory;
        $this->_emailConfig = $emailConfig;

        parent::__construct($context, $data);
    }

    /**
    * Returns collection of custom templates
    *
    * @return mixed
    */
    public function getCustomTemplates()
    {
        return $this->_collectionFactory->create();
    }

    /**
    * Returns collection of all available templates
    *
    * @return array[]
    */
    public function getConfigTemplates()
    {
        return $this->_emailConfig->getAvailableTemplates();
    }
}
remklov
  • 331
  • 2
  • 10