Question

What is the best practice to override a helper?

is it through Preference or Plugin?

And how can I override a helper using Plugin?

Was it helpful?

Solution

Hey You can override helper using di.xml you need to pass preference in di.xml file like:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Helper\Product" type="FME\Test\Helper\Rewrite\Product" />

Where in preference "for" where you mention the helper class namespace and in type we pass our helper class namespace.

For more information you can go through this link

Plugins are use to change the behaviour of function means if we want to change any variable value before calling function or after function execution or during function then we should use plugin but make sure plugin can only used on public functions.

OTHER TIPS

Its not recommended to use preference until you have no option left. Using preference would override the core file class which may cause issue when Magento is upgraded. It is not stopped just here. In case there are two module rewriting the same class the functionality may not work as expected due to confection.

Plugin appears as the clever choice to rewrite block, model, controller, helper in Magento 2. With Plugin, you can execute the code before, after and around the code/target class’s function. Without replacing, this is just inserting some code before/after the core code and then observe the core/target class’s function and run our code in-between the core/target class’s function. In addition, it is possible to the plugins from multiple modules to insert their own code before/after/around the same core/target class’s function.

There are few limitions of using plugins such as

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

In short, try using a plugin when ever its possible.

Edit

As provided in reference link you can declare a plugin

<config>
    <type name="{ObservedType}">
      <plugin name="{pluginName}" type="{PluginClassName}" sortOrder="1" disabled="false" />
    </type>
</config>

So consider I need to add new params to buyrequest when addParamsToBuyRequest() method is called in Magento\Catalog\Helper\Product.php

<config>
    <type name="Magento\Catalog\Helper\Product">
      <plugin name="modify_buy_request" type="Vendor\ModuleName\Plugin\ProductHelper.php" sortOrder="1" />
    </type>
</config>

Vendor\ModuleName\Plugin\ProductHelper.php

class ProductHelper
{

    public function afterAddParamsToBuyRequest($subject, $result)
    {
        $result->setMyCustomParam("Test Param");
        return $result;
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top