Magento 2How To GuideNovember 28, 2016

How to Override Magento 2 Helper, Block, Model and Controller

How to Override Magento 2 Helper, Block, Model and Controller

There is always a certain level of customization required for every Magento project. This can involve adding entirely new elements or overriding the existing ones. Customizing your Magento store according to your preferences gives you a personalized feel and ease of use while handling tasks. It also improves the customer user experience in several levels that can benefit your business with increased conversions and revenues. However, it is never recommended to modify the core files of Magento community or enterprise version. Instead of completely modifying core files, Magento recommends to override/overwrite the core files if needed.

Today, we will guide you about overriding helper, model, block and controller in Magento2. Here in example we will be overriding product helper, list product block, product model and product view controller. You can use the similar approach to override other block, model and controllers in Magento.

You can also contact FMEExtensions for Magento custom development or hire a dedicated Magento developer to handle your store management and customization tasks.

Overriding Magento 2 Helper

Let's start with overriding core Product helper. Suppose you need to make some additions in Magento 2 Product helper.

Step # 1 – Create a di.xml file in a folder FME/Test/etc
<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 - The next step to overriding Magento2 Helper is to create a Product.php Block file in the folder FME/Test/Helper/Rewrite/Product
 <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();
                }
        }
You can rewrite other Magento 2 helper using the same method.

Overriding Magento 2 Blocks

Lets start with overriding core ListProduct block. Suppose you need to make some additions in Magento 2 ListProduct block. Step # 1 – Create a di.xml file in a folder FME/Test/etc
<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 - The next step to overriding Magento2 block is to create a ListProduct.php Block file in the folder 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 rewrite other Magento 2 blocks using the same method.

Overriding Magento 2 Model

Overriding Magento2 models is not very different from overriding blocks. However, there is a simple method to override models in Magento 2. In the following example we will override Magento2 product model. You can follow the same method to override any other Magento2 Model. Step#1 Create a di.xml file in Folder FME/Test/etc
<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 Create Product.php Model file in Folder FME/Test/Model/Rewrite/Catalog
<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();
                }
        }
You can rewrite other Magento 2 model using the same method.

Overriding Magento 2 Controller

Overriding Magento2 controllers in not exactly same as overriding Magento2 blocks and Models. In the following example will be override product view controller of Magento2. Using the same approach you can override other controllers in Magento2. Step # 1: Just like for Models and Blocks create di.xml file in folder FME/Test/etc
<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: In the second step of overriding controller in Magento 2, create a View.php Controller file in Folder Fme/Test/Controller/Rewrite/Product
<?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();
                }

        }

You can rewrite other Magento 2 controller using the same method.

Recommended Articles: