Pre Black Friday Sale. 20% OFF on all products. Coupon code: fme20off

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

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

As a leading Magento extensions company, our experience tells us that the topmost reason most people opt for Magento 2 is because of the customisability. With the right Magento development team, there is nothing you cannot customise in your store. This allows you to create a store that is personalised to your requirements and target audience preferences.

While customisability is a core feature in Magento 2, there are certain points to keep in mind. For example, it is never recommended to modify the core files of Magento community or enterprise version. Instead, our Magento experts recommend override block, model, and controller in Magento 2 if needed. Today, we will guide you about overriding helper, model, block and controller in Magento 2.

In this article, we will be overriding Magento 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 FME Extensions for Magento custom development or hire a dedicated Magento developer to handle your store management and customization tasks.

Why You Should Avoid Editing Magento 2 Core Files?

There are several reasons, including:

Updates Override Customisations

Suppose you have made customisations to the core files. These customisations are a core part of your website. Now, if you upgrade to the latest Magento version or even install a patch, the core files will be overwritten. It means that all your customisations will be removed. This can break your site.

Read More: How to Check Your Current Magento Version

Website Maintenance

When you change the core files, it can be difficult to track the changes. If you change your Magento team, the new team won’t have any idea why and where the changes were made. On top of it, since the code is interrelated to other parts, it can be difficult to ascertain how the custom code interacts with others or what was done to ensure seamless integration.

Compromises Store Security

Magento 2 is one of the most secure eCommerce platforms. You’ll notice that updates always introduce new security features. Besides this, the team behind the platform is always issuing patches to overcome vulnerabilities. When you make changes to the core file, you can introduce security vulnerabilities. Hackers can exploit this vulnerability to wreak havoc on your store and customers.

Incompatibility With Extensions

Lastly, a Magento extensions company does not know what changes you have made to the core files. Instead, all extension developers assume that the core files are behaving in a standard manner i.e. how the platform intended them to. Therefore, all the code in the extension is based on standard core files. If you have made changes to the core file, the extension may become unusable.

Overriding Magento Helper

Let's start with overriding a core Magneto 2 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.

Conclusion

This concludes our article on how to override Magento 2 helper, block, model and controller. We acknowledge that the instructions can be tricky to follow. If you run into any trouble, contact us. Our Magento 2 development experts will resolve your query right away.

Recommended Articles:

This blog was created with FME's SEO-friendly blog