Blog

How to Override Block, Model, Controller in Magento2

Today, we’re going to teach you guys how to override the block, model, and controller in Magento 2. From the developers perspective, while developing a Magento site we need to customize some core files for its proper functioning. In such case, we can not modify the core files. It is not recommended to modify core files in Magento 2,  so we need to override the core files.

Here we use customization which involves adding new features or the overriding existing one. Here we can use di.xml file configuration through which dependencies are injected by the object manager and each module can have the global and area-specific di.xml file that can be used depending on scope.

Magento 2 introduced new concept called “preference” to override class.

What is Preference?

Preference are similar to class rewrites in Magento 1. Preference is used for override/rewrite the class.

Let’s we start to learn overriding blocks, models and controllers in magento2

Overriding Block

Step 1: You need to create di.xml file in directory Thecoachsmb\Catalog\etc and add configuration with preference.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
	<preference for="Magento\Catalog\Block\Product\ListProduct" type="Thecoachsmb\Catalog\Block\Rewrite\Product\ListProduct" />
</config>

 

Step 2: You need to create ListProduct.php Block file in directory Thecoachsmb\Catalog\Block\Rewrite\Product

<?php
	namespace Thecoachsmb\Catalog\Block\Rewrite\Product;

	class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
	{
		public function _getProductCollection()
		{
			// you can add/append your custom code here
		}
	}

 

Overriding Model

Step 1: You need to create di.xml file in directory Thecoachsmb\Catalog\etc and add configuration with preference.

<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="Thecoachsmb\Catalog\Model\Rewrite\Catalog\Product" />
</config>

Step 2: You need to create Product.php Model file in directory Thecoachsmb\Catalog\Model\Rewrite\Catalog

<?php
	namespace Thecoachsmb\Catalog\Model\Rewrite\Catalog;

	class Product extends \Magento\Catalog\Model\Product
	{
		public function isSalable()
		{
			// you can add/append your custom code here
			return parent::isSalable();
		}
	}

 

Overriding Controller

Step 1: You need to create di.xml file in directory Thecoachsmb\Override\etc and add configuration with preference.

<?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="Thecoachsmb\Catalog\Controller\Rewrite\Product\View" />
</config>

Step 2: You need to create View.php controller file in directory Thecoachsmb\Override\Controller\Rewrite\Product

<?php
	namespace Thecoachsmb\Catalog\Controller\Rewrite\Product;

	class View extends \Magento\Catalog\Controller\Product\View
	{
		public function execute()
		{
			// you can add/append your custom code here
			return parent::execute();
		}
	}

After these steps run the following commands in your command prompt.

sudo php bin/magento setup:upgrade
sudo php bin/magento setup:di:compile
sudo php bin/magento setup:static-content:deploy
sudo php bin/magento cache:flush

I thought you understand about how to override class. you can override other Block, Model and Controller classes.

I hope this blog is useful for you. Please left comment if you have any confusion.

2 thoughts on “How to Override Block, Model, Controller in Magento2

  1. Excellent article, thank you!

    Note that in the first example, the Module used for the preference type is wrong. Currently the attribute value is “Thecoachsmb\Override\Block\Rewrite\Product\ListProduct”, it should probably be “Thecoachsmb\Catalog\Block\Rewrite\Product\ListProduct”.

Leave a Reply

Your email address will not be published. Required fields are marked *