Guide To Overriding Magento 2 Helper, Block, Model And Controller

0 869

Magento is a dedicated eCommerce platform that caters to various business needs with various built-in features. As we know, every business is different in nature, scope, and target audience, so there is always a need to customize the application.

These customizations are performed either by the addition of new elements or overriding the default elements with advanced ones. There are various benefits of personalizing the Magento application of your eCommerce website.

It starts with easing the user interface to facilitate your staff in better understanding and utilizing the platform. The simplicity of adding and modifying product pages improves performance and efficiency. The benefit of customization extends to achieving customer satisfaction as well. The addition of a feature helps them get to know more about products and easily inquire about a product feature. This ultimately results in higher conversions and increased revenues.

Magento customization offers limitless possibilities of molding the application as your business requirements. The experts suggest overriding only the required elements rather than the core files of the Magento Enterprise or Community edition.

This tutorial will help you overwrite or override helper, block, model, and controller. Specifically speaking, you will find how to override product helper, product model, product view controller, and a product block list in this post.

If you face any difficulties before, during, or after override Magento elements for customization, you can seek assistance from a dedicated Magento developer for the flawless fixture.

Overriding Magento 2 Helper

Pursue overwriting the Product helper with the following method. You can apply the code if you aim to modify the core product helper element.

Step # 1 – Navigate to FME/Test/etc and Create a di.xml file

<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” />

</config>

Step # 2 – Find product rewrite folder that is FME/Test/Helper/Rewrite/Product and create a Product.php block file

<php

/**
* Catalog Product Rewrite Helper
*
* @category    FME
* @package     FME_Test
* @author      Fme Extensions Development Company
*
*/

namespace FME\Test\Helper\Rewrite;
class Product extends \Magento\Catalog\Helper\Product
{
public function __construct()
{
echo “Helper Rewrite Working”; die();
}
}

This code is helpful in overriding any Magento 2 helper element. So, for other helper modifications, you can follow the same method.

Overriding Magento 2 Blocks

Move to overwrite the List Product Blocks. Follow the procedure if you need to alter the core block element for product list.

Step # 1 – Develop a di.xml file in FME/Test/etc folder

<config xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”urn:magento:framework:ObjectManager/etc/config.xsd”>

<preference for=”Magento\Catalog\Block\Product\ListProduct” type=”FME\Test\Block\Rewrite\Product\ListProduct” />

</config>

Step # 2 – Use the below code and develop a ListProduct.php in the product rewrite folder that can be traced as FME/Test/Block/Rewrite/Product

<php
/**
* Rewrite Product ListProduct Block
* @category    FME
* @package     FME_Test
* @author      Fme Extensions Development Company
*/
namespace FME\Test\Block\Rewrite\Product;
class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
{
public function __construct()
{
echo “Block Rewrite Working”; die();
}
}

You can utilize the same code and method for overriding any of the Magento 2 block elements.

Overriding Magento 2 Model

Overwriting a Magento 2 model is slightly different than modifying the blocks or other elements. But, the method is simple and easy to follow. The method described below is for overriding Product Model, whereas you can use it for modifying any model element.

Step #1 Add a di.xml file in FME/Test/etc folder with the code below.

<config xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”urn:magento:framework:ObjectManager/etc/config.xsd”>

<preference for=”Magento\Catalog\Model\Product” type=”FME\Test\Model\Rewrite\Catalog\Product” />

</config>

Step #2 Custom develop a Product.php Model file with the code below in the FME/Test/Model/Rewrite/Catalog folder

<php
/**
* Catalog Product Rewrite Model
* @category    FME
* @package     FME_Test
* @author      Fme Extensions Development Company
*/

namespace FME\Test\Model\Rewrite\Catalog;

class Product extends \Magento\Catalog\Model\Product
{
public function __construct()
{
echo “Model Rewrite Working”; die();
}
}

With the help of this method, you can overwrite any model of Magento 2 platform.

Overriding Magento 2 Controller

Customizing Magento 2 controller elements by overriding is somehow different than modifying the block and model elements. Proceed with a quick two-step method to successfully amend Magento 2 Product View Controller. The same procedure applies to overwrite any of the Controller elements.

Step # 1: Develop di.xml file in FME/Test/etc folder by copying the code below. This step is exactly the same as overriding the block or model elements.

<config xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”urn:magento:framework:ObjectManager/etc/config.xsd”>

<preference for=”Magento\Catalog\Controller\Product\View” type=”FME\Test\Controller\Rewrite \Product\View” />

</config>

Step # 2: Add a View.php Controller file, with the code below, to the Fme/Test/Controller/Rewrite/Product folder

<?php
/**
* Rewrite Product View Controller
* @category    FME
* @package     FME_Test
* @author      Fme Extensions Development Company
*/

namespace FME\Test\Controller\Rewrite\Product;

class View extends \Magento\Catalog\Controller\Product\View
{
public function execute()
{
echo “Controller Rewrite Working”; die();
}
}

For overwriting any Magento 2 controller, you can make use of this method.

Conclusion

Overriding is an amazing feature that helps you customize a website without altering the core Magento code. Being a beginner, one must strictly refrain from making changes to the core code as it may result in several bugs. With the above method, you can easily override Magento 2 elements that include helper, block, model, and controller. To be on the safe side, one shall always seek professional assistance for Magento 2 custom development to ensure the personalization is perfectly well and error-free.


Author: Zeeshan Khalid is a Web Architect, an eCommerce Specialist and an Entrepreneur. He is the CEO and founder of FME Dubai, a leading e-commerce web design and development agency. Over the years, FME Extensions has successfully delivered projects in Magento, WordPress, WooCommerce, Joomla and other CMS/shopping cart platforms. You can find him on the LinkedIn.

Leave A Reply

Your email address will not be published.