Rewrite an Existing Magento 2 Helper

This article will help you understand how to rewrite an existing Magento 2 helper. In earlier versions of Magento, you had to specify the rewrite within the module’s config.xml file. In Magento 2, this is done in the module’s di.xml file.

This article will show an example of how to override the Magento\Catalog\Helper\Data class and add some debug logging in the getProduct() method. This will be done by adding a preference.

If you don’t have a custom module structure set up, create one by starting with the module.xml file.

<?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="Siphor_Custom" setup_version="0.0.1" />
</config>

Register the custom module by adding a registration.php file.

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Siphor_Custom',
    __DIR__
);

Run the command to enable the module from within the Magento root directory.

$ /path/to/your/php bin/magento module:enable --clear-static-content Siphor_Custom

After doing this and refreshing your Magento store, you might get encounter the following error.

Please upgrade your database: Run "bin/magento setup:upgrade" from the Magento root directory. 
The following modules are outdated:
Siphor_Custom schema: current version - none, required version - 0.0.1
Siphor_Custom data: current version - none, required version - 0.0.1

To rectify this, run the following command.

$ /path/to/your/php bin/magento setup:upgrade

Now that the custom module has been registered, the module’s di.xml file can be added.

This is where the helper override will be specified. The key information resides in the <preference> node.

The for attribute value specifies the class you intend to override, and the type attribute specifies your custom class name.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Helper\Data" type="Siphor\Custom\Helper\Catalog\Data" />
</config>

Finally, the custom helper class can be added with the method override.

<?php
namespace Siphor\Custom\Helper\Catalog;

use Magento\Catalog\Helper\Data as CatalogHelper;

class Data extends CatalogHelper
{
    public function getProduct()
    {
        $this->_logger->debug("Some debug message!");
        return $this->_coreRegistry->registry('current_product');
    }

If you then head to any of your product pages where the getProduct() method gets used, you’ll notice that the debug message appears in the var/debug/debug.log file.

[2016-09-12 19:41:36] main.DEBUG: Some debug message! {"is_exception":false} []

Although this is a basic example of how to rewrite an existing Magento 2 helper, the same steps apply to more complex helpers that need to be rewritten. When using this method, also check for any existing rewrites if there are any, to avoid rewrite conflicts.

It might be possible to rewrite a helper using Plugins, which give you the ability to extend functionality without creating rewrite conflicts. View the post for more information.

Note: This article is based on Magento CE version 2.1.