Menu Close
    Searching for an experienced Magento 2 Development Company ?

    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.

    . . .
    Discuss on Helpdesk

    Leave a Comment

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


    18 comments

  • 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

  • Maha
    how to override private function in model (Magento2)
    • Webkul Support

      Hello Maha,

      We can not override private functions in magento.
      You need to find alternate solution for this problem, like overriding child function or creating plugin for that child function.
      If you have any doubt or query let me know.

      Thanks

      Rahul Mahto

      • Maha
        But Plugin concept was not used to override non-public methods
        • Webkul Support

          Yes we can not create plugins for private class,But i am suggesting you to create plugin for child function (if possible).
          If there is any child function used in that private function. you can use this alternate method.

          Thanks

          Rahul Mahto

  • Nilay Chaudhari
    How can i Override Same class In two different custom module?
    • Webkul Support

      Hello Nilay,

      Unfortunately you can not override same class in different modules.One class can be rewrite in one module at a time.
      But you can use Plugins in magento2.Please check here
      https://webkul.com/blog/magento2-use-plugins/

      This might help you.

      If you have any doubt or issue let me know.

      Thanks

      Rahul Mahto

  • Manadir
    should i override block, model or controller to get a custom eav attribute (e.g. brand name) value in magento2?
  • Jaimin
    can you provide the article of override configurable.php this file is in vendor/magento/module-configurable-product/Block/product/view/type/configurable.php

    The method to override this file is different it is use plugin

    I try with this but its not working:-
    https://magento.stackexchange.com/questions/122247/magento-2-how-to-override-block-file-configurable-php

  • Michele Fantetti
    What if more extensions override the same method?
  • Rafael Corrêa Gomes ♛
    Thanks for sharing!
  • Attila Attila
    How we can override function from MagentoSetupModelInstaller (setup/src/Magento/Setup/Model/Installer.php) ?
  • Ankit Shah
    Thanks for amazing Article. It really helps. Can we use Constructor in Controller after overriding? I tried but seems not working. http://magento.stackexchange.com/questions/149550/magento-2-how-to-change-weight-field-for-dhl-shipping-method-solved
  • mac
    Hi, can you tell how can I enable or disable overriding, depending on the module configuration values? from di.xml file
    like the condition ifconfig or something?
  • ND
    #2 – Now create ListProduct.php Block file in Folder Webkul/Hello/Block/Rewrite/Product

    In this one bug i found in class

    class ListProduct extends MagentoCatalogBlockProductListProduct

    you forget “” before Magento

    • Zuko
      MagentoCatalogBlockProductListProduct = MagentoCatalogBlockProductListProduct

      is root namespace in php

      • Rahul
        Thanks ND and Zuko for your replies.
        Error is fixed in the blog now.
  • Back to Top