Magento – override vendor\magento\module-email\Model\AbstractTemplate.php

magento2.2override-model

I want to override method setForcedArea() because I'm using Magento 2.2.4 & whenever I tried to save Content->Configration->Design Configuration->Edit

I'm getting following error:-

Something went wrong while saving this configuration: Area is already set

Screenshot:-

http://nimb.ws/kV93zH

When i R&D then found that it's M2.2.4 issue so I've to modify this method.

Here is the Module :-

Step1:- app/code/Allin/Coreissueresolve/etc/registration.php =>

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Allin_Coreissueresolve',
    __DIR__
);

Step2:- app/code/Allin/Coreissueresolve/etc/module.xml =>

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Allin_Coreissueresolve" setup_version="1.0.0"></module>
        <sequence>
            <module name="Magento_Backend"/>
             <module name="Magento_Sales"/>
            <module name="Magento_Quote"/>
            <module name="Magento_Checkout"/>
        </sequence>
</config>

Step3:- app/code/Allin/Coreissueresolve/etc/di.xml =>

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Email\Model\AbstractTemplate" type="Allin\Coreissueresolve\Model\Magento\Email\AbstractTemplate" />
</config>

Step4:- app/code/Allin/Coreissueresolve/Model/MagentoEmail/AbstractTemplate.php =>

<?php

namespace Allin\Coreissueresolve\Model\Magento\Email;


abstract class AbstractTemplate extends \Magento\Email\Model\AbstractTemplate
{
     public function setForcedArea($templateId)
    {
        echo "Hello123" ; exit;
    }

}

After that i've run php/bin/magento setup:upgrade & also cache:flush.

But app/code/Allin/Coreissueresolve/Model/MagentoEmail/AbstractTemplate.php => setForcedArea() is not calling.

While vendor\magento\module-email\Model\AbstractTemplate.php => setForcedArea() is calling everytime.

Best Answer

You cannot override that class.
Magento\Email\Model\AbstractTemplate is an abstract class and it never gets instantiated.
Your code is correct but you don't see your method being called because your class is never instantiated.
preferences work only for classes that get instantiated.
You can do one of the following.
- Create an around plugin for the method. Plugins work with abstract classes because they will be called also in the child classes of the abstract ones.
- Rewrite the method in every child class of the AbstractTemplate class. I don't really recommend this since it's painful and you can easily miss something.

Related Topic