2

What is the best practice to override a helper?

is it through Preference or Plugin?

And how can I override a helper using Plugin?

  • what is your purpose to overide the helper? Based on that you can take decision. – Dhiren Vasoya Apr 24 at 10:08
  • lets say I want to override a core helper, what would be the best practice to do this? – Shoaib Munir Apr 24 at 10:12
  • Which function you want to override? – Rohan Hapani Apr 24 at 10:13
2

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.

  • Thanks Shubham, I know how to override using preference, but I want to know if preference or plugin is best practice to do this. I also want to know how can I do it using plugin? Anyways, +1 for the efforts that you have given to answer my question – Shoaib Munir Apr 24 at 10:16
  • 1
    hey as far as i know plugin won't override any helper what plugin do is it intercept the function and only public function.for that we have to mention it in di.xml too. – Shubham Khandelwal Apr 24 at 10:18
  • I think this clarify things for me. Thanks :) – Shoaib Munir Apr 24 at 10:23
2

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;
    }

}
  • Actually I am unable to find any tutorial or guide to override helper using plugin. I will highly appreciate if you can provide it – Shoaib Munir Apr 24 at 10:55
  • If you can provide the guide to override Helper through Plugin, it would be great. I am still voting for your answer because you have given effort answering my question +1 :) – Shoaib Munir Apr 24 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.