1

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!

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

  $alltemplele_data =$alltemplele->getAvailableTemplates();
  print_r($alltemplele_data);
1

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();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.