Plugins not called in magento 2
Question
I have created a plugin in my custom module to override the default method getName()
in Magento\Catalog\Model\Product.php
I've created my custom module named Firstmodule
inside app\code\Magento
. I am using the after
listener of plugins.
The file app\code\Magento\Firstmodule\etc\frontend\di.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Model\Product">
<plugin name="Magento_Firstmodule::before" type="Magento\Firstmodule\Model\Basic" sortOrder="1" />
</type>
</config>
And the Model app\code\Magento\Firstmodule\Model\Basic.php
<?php
namespace Magento\Firstmodule\Model;
class Plugin {
public function afterGetName(\Magento\Catalog\Model\Product $subject, $result)
{
return '|' . $result . '|';
}
}
Still there is no result on the frontend product view page.
Can anyone tell me what is missing.? thanks in advance.
Solution
You have specified a type of Magento\Firstmodule\Model\Basic but called the class 'Plugin' in your Basic.php file. It needs to be
class Basic {
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange