Magento2 Plugin use for before , after and around

We can use plugins to intercept the public methods of core framework or third party modules or custom modules in Magento2.
Plugins seems to be useful than using preference because you need to override the complete class in preference but not in case of plugins. Let's know why is it so.

Rule To Remember: You can use plugins only on public methods.

Plugins uses three mechanisms to intercept the public methods:
1) before plugin
2) around plugin
3) after plugin

Let's create a plugin with all three mechanisms. We can use it as per our requirement.

Step 1: We need to create an module.xml in app/code/Magemeta/Override/etc/module.xml to register our custom module.

<?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="Magemeta_Override" setup_version="1.0.0"/>
</config>

Step 2: We need to create an di.xml in app/code/Magemeta/Override/etc/di.xml to register our plugin dependancy class on Magento core classes we are intercepting. 
<?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\Catalog\Model\Product">
        <plugin name="name_change_before_plugin"
                type="Magemeta\Override\Plugin\Name"
                sortOrder="10"
                disabled="false"/>
        <plugin name="name_change_around_plugin"
                type="Magemeta\Override\Plugin\Name"
                sortOrder="20"
                disabled="false"/>
        <plugin name="name_change_after_plugin"
                type="Magemeta\Override\Plugin\Name"
                sortOrder="30"
                disabled="false"/>
    </type>
</config>

Step 3: We need to create an Name.php in app/code/Magemeta/Override/Plugin/Name.php to register our custom plugin dependancy class. In this class, we have declared all the three plugin mechanisms for the demo purpose.

<?php

namespace Magemeta\Override\Plugin\Name;

use Magento\Catalog\Model\Product as ParentProduct;

class Name
{
/**
     * @param ParentProduct $subject
     * @param $result
     * @return mixed
     */
    public function beforeGetName(ParentProduct $subject)
    {
        $beforeTestString = "_TestBeforeName";
        $result .= $beforeTestString;

        return $result;
    }

    /**
     * @param ParentProduct $subject
     * @param $result
     * @return mixed
     */
    public function aroundGetName(ParentProduct $subject, callable $proceed)
    {
        $aroundTestString = "_TestAroundName";
        $result .= $aroundTestString;
        return $result;
    }

    /**
     * @param ParentProduct $subject
     * @param $result
     * @return mixed
     */
    public function afterGetName(ParentProduct $subject, $result)
    {
        if ($result) {
            $afterTestString = "_TestAfterName";
            $result .= $afterTestString;
        }

        return $result;
    }
}

Hope this helps you guys!

Leave a Reply