Overriding Block, Model, Controller in Magento2

Here we will learn overriding blocks, models and controllers in magento2.

Overriding Block

Lets override catalog product ListProduct block. 

#1 – First of all create di.xml file in Folder Webkul/Hello/etc

<?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="Webkul\Hello\Block\Rewrite\Product\ListProduct" />
</config>


#2 – Now create ListProduct.php Block file in Folder Webkul/Hello/Block/Rewrite/Product

<?php
	/**
	 * Hello Rewrite Product ListProduct Block
	 *
	 * @category    Webkul
	 * @package     Webkul_Hello
	 * @author      Webkul Software Private Limited
	 *
	 */
	namespace Webkul\Hello\Block\Rewrite\Product;

	class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
	{
		public function _getProductCollection()
		{
			// Do your stuff here
		}
	}

You can override other blocks using same approach.

Overriding Model

Lets override catalog product model. 

#1 – First of all create di.xml file in Folder Webkul/Hello/etc

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


#2 – Now create Product.php Model file in Folder Webkul/Hello/Model/Rewrite/Catalog

<?php
	/**
	 * Hello Catalog Product Rewrite Model
	 *
	 * @category    Webkul
	 * @package     Webkul_Hello
	 * @author      Webkul Software Private Limited
	 *
	 */
	namespace Webkul\Hello\Model\Rewrite\Catalog;

	class Product extends \Magento\Catalog\Model\Product
	{
		public function isSalable()
		{
			// Do your stuff here
			return parent::isSalable();
		}

	}

You can override other models using same approach.

Overriding Controller

Lets override product view controller. 

#1 – First of all create di.xml file in Folder Webkul/Hello/etc

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


#2 – Now create View.php Controller file in Folder Webkul/Hello/Controller/Rewrite/Product

<?php
	/**
	 * Hello Rewrite Product View Controller
	 *
	 * @category    Webkul
	 * @package     Webkul_Hello
	 * @author      Webkul Software Private Limited
	 *
	 */
	namespace Webkul\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();
		}
	}

You can override other controllers using same approach.

. . .

Comments (18)

Add Your Comment

  • Kamlesh Kumar Jha
    Hi

    Can you please explain why return parent::isSalable(); is use in case of model calls rewrite but not use for block class rewrite?

    Thanks

    • Webkul Support

      Hello Kamlesh,

      You don’t need to return parent in any case (block, model, controller or helper).
      In rewrite method if you are not doing any modification then you can return parent method.
      Please don’t get confuse.
      However you can use return parent method in some cases.For Ex:
      1. If your condition does not match and you need to return the original output then you can use parent return method.
      2. If you want to do some calculation in method and don’t want to modify the output of method then you can use parent return method.

      I hope this will help you. If you have any doubt or query let us know.

      Thanks

      Rahul Mahto

  • css.php