How to Override Block, Model, and Controller in Magento 2?

Today, we’re going to teach you guys how to override the block, model, and controller in Magento 2.

What is Block in Magento 2?

Blocks are basically PHP classes used for connecting or creating a link between the templates and layout of your store.

What is Model in Magento 2?

Models are the inherent part of the MVC architecture and they’re used for performing data operations such as Create, Read, Update, Delete (CRUD) in the database of your store.

What is Controller in Magento 2?

Just like models, the controllers in Magento 2 are also a very important part of the MVC architecture. It’s basically a class located in the module controller folder that is responsible for a URL or a group of URLs.

In this tutorial, we’re going to show you guys how to override the block, model, and controller in your Magento 2 store.

How to Override Block in Magento 2

In this demo, we will override the catalog product ListProduct block.

Step 1. For this, you need to first create a di.xml file in the MageDelight/Hello/etc directory and add the following code:

<?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\Catalog\Block\Product\ListProduct" type="MageDelight\Hello\Block\Rewrite\Product\ListProduct" />

</config>

After that, we need to create a Block file named ListProduct.php in the MageDelight/Hello/Block/Rewrite/Product directory and add the following code:

<?php

    /**

     * Hello Rewrite Product ListProduct Block

     *

     * @category    MageDelight

     * @package     MageDelight_Hello

     * @author      MageDelight Pvt. Ltd.

     *

     */

    namespace MageDelight\Hello\Block\Rewrite\Product;

    class ListProduct extends \Magento\Catalog\Block\Product\ListProduct

    {

       public function _getProductCollection()

       {

           // Do your stuff here

       }

    }

And it’s done!

This is how you can override block in Magento 2.

How to Override Model in Magento 2
To override a catalog product model, you need to first create a di.xml file in the MageDelight/Hello/etc directory and add the following code.

<?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\Catalog\Model\Product" type="MageDelight\Hello\Model\Rewrite\Catalog\Product" />

</config>

Step 2. Now, we also need to create a Model file named Product.php in the MageDelight/Hello/Model/Rewrite/Catalog directory and copy the below code:

<?php

    /**

     * Hello Catalog Product Rewrite Model

     *

     * @category    MageDelight

     * @package     MageDelight_Hello

     * @author      MageDelight Pvt. Ltd.

     *

     */

    namespace MageDelight\Hello\Model\Rewrite\Catalog;

 

    class Product extends \Magento\Catalog\Model\Product

    {

       public function isSalable()

       {

           // Do your stuff here

           return parent::isSalable();

       }

 

    }

And it’s done!

Similar to this, you can also override other models in your Magento 2 store.

How to Override Controller in Magento 2?

Step 1. To override a controller, we need to create a di.xml file in the MageDelight/Hello/etc directory and copy the below code:

<?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\Catalog\Controller\Product\View" type="MageDelight\Hello\Controller\Rewrite\Product\View" />

</config>

Step 2. After that, create a Controller file named View.php in the MageDelight/Hello/Controller/Rewrite/Product directory and add the following code:

<?php

    /**

     * Hello Rewrite Product View Controller

     *

     * @category    MageDelight

     * @package     MageDelight_Hello

     * @author      MageDelight Pvt. Ltd.

     *

     */

    namespace MageDelight\Hello\Controller\Rewrite\Product;

 

    class View extends \Magento\Catalog\Controller\Product\View

    {

       /**

         * @return \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\View\Result\Page

         */

       public function execute()

       {

           // Do your stuff here

           return parent::execute();

       }

    }

And it’s done!

You can use the same approach to override any controllers in your Magento 2 store.

Also read: How to Use viewModel in Magento 2?

Conclusion

There you have it!

This is how easy it is to override the block, model, and controller in Magento 2.

We hope that you found this tutorial helpful. If you have any questions, please ask them in the comments below.

And if you need our professional assistance, feel free to contact us anytime.