Save email template to database when install data Magento 2
-
06-10-2020 - |
题
I want create a new template, but not from Marketing > Email Templates i want to do it programmatically from my custom extension, the idea is the new template will be saved when Setup/InstallData.php
runs, is there any way to do this?
解决方案
You can make the InstallData.php
file look like this
<?php
namespace [Vendor]\[Module]\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Email\Model\TemplateFactory;
class InstallData implements InstallDataInterface
{
/**
* Template factory
*
* @var TemplateFactory
*/
private $templateFactory;
/**
* Init
*
* @param TemplateFactory $templateFactory
*/
public function __construct(TemplateFactory $templateFactory)
{
$this->templateFactory = $templateFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$template = $this->templateFactory->create();
$template->setTemplateCode('your template name here');
$template->setTemplateText('your template text here');
$template->setTemplateStyles('your template custom styles here');
$template->setTemplateSubject('your subject here');
$template->setOrigTemplateCode('code of the template you are replacing');
$template->save();
}
}
Watch out for syntax errors. I didn't test the code.