1

I want to override AbstractTemplate.php of vendor\magento\module-email\Model\ in magento 2.1.12.I want to change getUrl method which are declare in AbstractTemplate.php.How can do it?

1

You cannot override AbstractTemplate.php of vendor\magento\module-email\Model\

Magento\Email\Model\AbstractTemplate is an abstract class and it never gets instantiated.

you can 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.after that you can easily select the theme after overriding the template.php file .

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

| improve this answer | |
  • This is the correct answer as all the others go off into the standard Plugin or Rewrrite process and no one noticed the class is abstract and therefor not overridable. The question asks for override/rewrite not plugin around / replacment. Thanks Hafiz, you also solved my related problem (I also failed to notice the abstract keyword! D'oh!) – cygnus digital Oct 17 '18 at 14:04
0

You need use plugin , example :

di.xml

<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="my_product_url_path_generator" type="Namespace\Module\Model\PLugin\AbstractTemplate" sortOrder="99999"/>
    </type>
</config>

Namespace\Module\Model\Plugin\AbstractTemplate.php

<?php

namespace Namespace\Module\Model\Plugin;


    class AbstractTemplate 
    {
        //code , maby __construct()
        public function afterGetUrl(\Magento\Email\Model\AbstractTemplate $subject,$path)
        {
            //YOUR CODE change $path
            return $path

        }
    }
| improve this answer | |
  • In this case,Registeration.php or module.xml is required or not. – Lovely Setia Jun 21 '18 at 5:20
0

I hope that you want to change the behavior of getUrl() function. So my suggestion to define a Plugin over Magento\Email\Model\AbstractTemplate.

On this plugin use Before method beforeGetUrl() or after Method afterGetUrl() or aroundGetUrl() as per as your requirement.

https://www.mageplaza.com/magento-2-module-development/magento-2-plugin-interceptor.html

So, You should define plugin using di.xml at app\code\{Vendorname}\{CompanyName}\etc

<?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="change_url_value" sortOrder="1"
                type="{Vendorname}\{CompanyName}\Plugin\Email\AbstractTemplatPlugin" />
    </type>
</config>

Suppose,i have use around method

<?php

namespace {Vendorname}\{CompanyName}\Plugin\Email;

class AbstractTemplatPlugin 
{

    public function aroundGetUrl(
        \Magento\Email\Model\AbstractTemplate $subject,
        \closure $proceed,
    \Magento\Store\Model\Store $store,
     $route = '',
     $params = []   
    ) {
    // set $url from here
        return  $url;
    }

}
| improve this answer | |
  • In this case,Registeration.php or module.xml is required or not. – Lovely Setia Jun 21 '18 at 5:20
  • offcoruce they have also required also composer.json – Amit Bera Jun 21 '18 at 5:21
  • I will echo some messsage,but this is not showing.what is issue? – Lovely Setia Jun 21 '18 at 6:09
  • donot use echo under it, use x-debug for debugging purpose – Amit Bera Jun 21 '18 at 7:14
  • i want to redirect custom forgetpassword page with parameters id and token on angular frontend using this getUrl method. – Lovely Setia Jun 21 '18 at 7:30

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.