How to Override a Helper File in Magento 2

img How to override a helper file in Magento 2

Magento projects always end up at customization to fulfill the needs of a store owner or to enhance store functionality & shopping experience. This customization includes adding or overriding existing elements, provides a personal touch and enhance the functionality of Magento 2 stores, indirectly reflects to business revenue. We have never recommended modifying default Magento core files, but alternative way is to override core files. There are several methods to override helper class, but pick any of it depending on the situation.

Magento Malware Removal

Compared to Magento 1, overriding and manipulating with classes is becoming much easier for a developer & allows us more ways of extending the native functionalities. One of our client was facing an issue while overriding helper file so we found and implement the solution. Here I’ve come up with the easiest way to override a helper file in Magento 2. You can even come to us with your requirement to customize your Magento store as per your need.

Magento 2 Speed & Performance Optimization Services

Step 1: First, we need to create a “di.xml” file inside the extension in the below directory

app\code\Vendor\Extension\etc

<?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\Bundle\Helper\Data" type="Vendor\Extension\Helper\Bundel" />
</config>

Step 2: lastly, we need to create a “Bundel.php” file inside the extension in the below directory

Vendor\Extension\Helper\Bundel.php

<?php 
namespace Vendor\Extension\Helper; 
use Magento\Bundle\Helper\Data as MainHelper;

class Bundel extends MainHelper 
{ 
    public function getAllowedSelectionTypes() 
    { 
        //write your logic here 
    } 
}

It’s done. You have successfully override helper class using this method.
If you have faced any issue while overriding helper class, let us know in the comment section.

hyva theme development
Happy Overriding!

Previous Article

Top 10 MageComp Magento Extensions to Power Up Selling This Holiday Season

Next Article

How to Install Magento SUPEE 10415 With or Without SSH

Write a Comment
  1. bhavin kapadia

    i have over ride
    Magento\ConfigurableProduct\Helper\Product\Options\Loader
    flile but admin page below issues generate
    (Type Error occurred when creating object: Magento\ConfigurableProduct\Model\OptionRepository)

    1. Please confirm you have implemented the code properly and after override given the fill path into the file where a short path is given for the same directory.

  2. Angel Patel

    Hi
    I am also facing the same Issue while over ride
    Magento\ConfigurableProduct\Helper\Product\Options\Loader file
    Error : Notice: Undefined property: $extensionAttributesJoinProcessor

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨