Overriding Block, Model, Controller in Magento2

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.