Magento 2 Plugins
Manifest Desires
Magento 2 Plugins
A plugin, or interceptor, is a class that modifies the behavior of public class functions by intercepting a function call and running code before, after, or around that function call.
This allows you to substitute or extend the behavior of original, public methods for any class or interface.
Plugins can not be used on following:
Final methods
Final classes
Non-public methods
Class methods (such as static methods)
__construct
Virtual types
Objects that are instantiated before Magento\Framework\Interception is bootstrapped
Declaring a plugin
Root Module( etc/di.xml)
<config>
<type name=”{ObservedType}”>
<plugin name=”{pluginName}” type=”{PluginClassName}” sortOrder=”1″ disabled=”false” />
</type>
</config>
Root of Module / plugin
namespace My\Module\Plugin;
class ProductAttributesUpdater
{
public function beforeSetName(\Magento\Catalog\Model\Product $subject, $name)
{
return [‘(‘ . $name . ‘)’];
}
}