In our daily coding routine we need to customization with core files. For that we need to overriding or rewrite the core classes because modify the core classes is not good practice. If you modify the code classes when you update the new version your code will be loss. So today we learn how to Magento 2 overriding or Rewriting helper. In start we are overriding core Product helper. We are jut checking our rewrite is working or not. You can modify the helper as you want following this tutorial. But first you need to know How to create module in Magneto 2.For any suggestions & question, please feel free to drop a comment.
Overriding or Rewrite Magento 2 Helper
sometimes, you may want to add new classes or override different functions in your Magento 2 module. Therefore, Magento 2 came up with the Helpers that are the right entity to fulfill your needs. A Helper in Magento 2 is an object that contains practical methods.Now we start to overriding or rewriting core Product helper. The helper we are overriding is Product helper.
Create a di.xml file in a following directory QaisarSatti/HelloWorld/etc/
<preference for="Magento\Catalog\Helper\Product" type="QaisarSatti\HelloWorld\Helper\Rewrite\Product" />
The step2 to overriding or rewriting Magento2 Helper is to create a Product.php helper file in the following directory QaisarSatti/HelloWorld/Helper/Rewrite/
* Catalog Product overridingHelper
* @category QaisarSatti
* @package QaisarSatti_HelloWorld
* @author Qaisar Satti
class Product extends \Magento\Catalog\Helper\Product
public function __construct()
echo "Our Rewrite Working"; die();
As following code you can rewrite or override any Magento 2 helper using the same method.