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?
3 Answers
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
-
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!) Oct 17, 2018 at 14:04
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
}
}
-
In this case,Registeration.php or module.xml is required or not. Jun 21, 2018 at 5:20
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;
}
}
-
In this case,Registeration.php or module.xml is required or not. Jun 21, 2018 at 5:20
-
-
I will echo some messsage,but this is not showing.what is issue? Jun 21, 2018 at 6:09
-
-
i want to redirect custom forgetpassword page with parameters id and token on angular frontend using this getUrl method. Jun 21, 2018 at 7:30