3

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.

3

3 Answers 3

8

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.

1
  • You should also avoid overriding an abstract class.
    – Wolfack
    Commented Aug 20, 2019 at 13:15
6

You can use the plugin to make changes in this function.

Create following files in your custom module.

app/code/Vendor/Module/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">
    <type name="Magento\Email\Model\AbstractTemplate">
        <plugin name="email_setforced_area" type="Vendor\Module\Plugin\Email\Model\AbstractTemplate" />
    </type>
</config>

app/code/Vendor/Module/Plugin/Email/Model/AbstractTemplate.php

<?php

namespace Vendor\Module\Plugin\Email\Model;

class AbstractTemplate
{
    private $emailConfig;

    public function __construct(\Magento\Email\Model\Template\Config $emailConfig)
    {
        $this->emailConfig = $emailConfig;
    }

    public function aroundSetForcedArea(\Magento\Email\Model\AbstractTemplate $subject, \Closure $proceed, $templateId)
    {
        if (!isset($subject->area)) {
            $subject->area = $this->emailConfig->getTemplateArea($templateId);
        }
        return $subject;
    }
}
3
  • perfect answer!!! made my day!!! +1 :) Commented Oct 9, 2018 at 8:42
  • In phpstorm I see a red line with message "Member has private access" (regarding $subject->area). (Magento version 2.2.5 ).
    – amitshree
    Commented Feb 15, 2019 at 6:47
  • @amitshree it works. I've just tried, even with PhpStorm nagging about the $subject->area visibility. Commented Aug 15, 2019 at 19:47
0

same issue i faced i also want to override the file AbstractTemplate.php of vendor\magento\module-email\Model\ but can't.

Marius is right: "You cannot override that class. Magento\Email\Model\AbstractTemplate is an abstract class and it never gets instantiated."

then i override the template.php of vendor\magento\module-email\Model\

adding this function public function setForcedArea($templateId) { if (!isset($this->area)) { $this->area = $this->emailConfig->getTemplateArea($templateId); } return $this; } . and issue resolved . theme easily selected after overriding the template.php file .

here is complete override module you can download here and solve the problem https://github.com/hafizjee/Area-code-is-already-set

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.