0

I have patch for email template and I want to delete already existing email template via patch. I can also update the already existing one. I understand the process in theory to update that is, check if template exists, load it, apply changes and save it but, not sure how to do it in code. Also, not sure if I want to delete the email template via data patch, how to do it in code?

Below is what I have tried but, does not work.

  public function apply()
    {
        $email_template = $this->scopeConfig->getValue('aw_ca/email/new_company_user_created_template');
        $template = $this->templateFactory->create();
        $this->templateResource->load($template, $email_template);
        $templateText = $this->filesystem->fileGetContents($this->getDirectory(self::TEMPLATE_NAME . '.html'));
        $template->setTemplateCode('New Company User Invitation')->setTemplateSubject(self::TEMPLATE_SUBJECT)
            ->setTemplateText($templateText)->setTemplateType(TemplateInterface::TYPE_HTML);
        if($template->getTemplateCode() !== null) {
            $this->templateResource->delete($template);
            $this->configWriter->delete('aw_ca/email/new_company_user_created_template');
        }
        $this->templateResource->save($template);
        $this->configWriter->save('aw_ca/email/new_company_user_created_template', $template->getId());
    }

1 Answer 1

2

In this post I'll add 2 features:

  • Delete an existing email template programmatically
  • Update an existing email template programmatically

Please can choose one of these features as your need. Currently, I've commented out the delete feature to prevent you don't want to delete your existing email template.

You also don't need to save the email content in a separate HTML file, you can use Nowdoc to declare content inside the Data Patch file.

The Data Patch file: app/code/Vendor/Module/Setup/Patch/Data/UpdateEmailTemplate.php

<?php

namespace Vendor\Module\Setup\Patch\Data;

use Magento\Email\Model\TemplateFactory;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Email\Model\ResourceModel\Template as TemplateResource;

class UpdateEmailTemplate implements DataPatchInterface
{
    /**
     * @var TemplateFactory
     */
    private $templateFactory;

    /**
     * @var TemplateResource
     */
    private $templateResource;

    public function __construct(
        TemplateResource $TemplateResource,
        TemplateFactory $templateFactory
    ) {
        $this->templateResource = $TemplateResource;
        $this->templateFactory = $templateFactory;
    }

    /**
     * @throws \Exception
     */
    public function apply()
    {
        $template = $this->templateFactory->create();
        $templateCode = 'New Order Customize'; // This is the Template Name in admin
        $this->templateResource->load($template, $templateCode, 'template_code');

        /* Choose one of the features below as your need */

        // 1. Delete an existing email template
//        if ($template->getId()) {
//            $this->templateResource->delete($template);
//        }

        // 2. Update an existing email template
        $content = <<<'HTML'
YOUR EMAIL TEMPLATE CONTENT GOES HERE
HTML;

        $template->setTemplateText($content);
        $this->templateResource->save($template);
    }

    public static function getDependencies()
    {
        return [];
    }

    public function getAliases()
    {
        return [];
    }
}

5
  • What does 'template_code' do in this statement? $this->templateResource->load($template, $templateCode, 'template_code');. Is it supposed to be replaced by actual template code? Could u please explain this line a bit?
    – LosMos
    Commented Nov 11, 2022 at 14:02
  • Ok, I understood what 'template_code' is. It refers to the field in the table whose value is the second parameter of load() method.
    – LosMos
    Commented Nov 11, 2022 at 14:15
  • Hope you found it useful. Don't forget to mark the answer as accepted :)
    – Tu Van
    Commented Nov 11, 2022 at 15:56
  • Yes, it was useful. Thank you for your help :)
    – LosMos
    Commented Nov 12, 2022 at 14:01
  • Glad to help. Cheers!
    – Tu Van
    Commented Nov 12, 2022 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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