Overriding Block Model in Magento 2: A Step-by-Step Guide

Posted on: 05 Jan 2019 by Arun Singh

Got stuck in overriding core Blocks, Models and Controllers in Magento2?

No worries!!

Read our entire blog to fix it.

Here is the 3 different steps you follow

  • Overriding Block
  • Overriding Magento 2 model
  • Overriding Controller

How to Override Block, Model, Controller In Magento 2 ?

As we know that Magento 2 has come out with all new features. Which helped Magento developers to implement core files a bit easier compared to before.

From the developers perspective, while developing a Magento site we need to customize some core files according to the custom functionalities needed.

Also it’s really necessary to override core module files instead of doing modifications to it directly.

Overriding the class in Magento 2 provides you the flexibility to modify the core functionality as per your Magento 2 module development demands.

Here we use customization which involves adding new features or the overriding existing one.

If we have compared Magento 2 with the old, we may notice that Magento 2 comes with some new concept of dependencies injection which helps you inject classes dependencies for an object which makes overriding pretty easier.

Here we can use di.xml file configuration through which dependencies are injected and each module can have the global and area-specific di.xml file that can be used depending on scope.

Now you might have a small idea, let’s see how Magento Development company uses override block, model, controller.

1. Override Block in Magento 2

Let’s override Magento 2 Catalog Product ListProduct Block.

Step 1 : First of all we have to add a di.xml file in  app/code/{vendor_name}/{module_name}/etc.

For example app/code/Egits/Customcatalog/etc 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 type="Egits\Customcatalog\Block\Product\ListProduct" for="Magento\Catalog\Block\Product\ListProduct"/> 
</config>
 

In preference for=”Magento\Catalog\Block\Product\ListProduct ” add the block which you want to override and in preference

type=”Egits\Customcatalog\Block\Product\ListProduct ” add our custom block where to override it.

Step 2 : Now override ListProduct.php

Create ListProduct.php in app/code/Egits/Customcatalog/Block/Product and add this code in it:

<?php
namespace Egits\Customcatalog\Block\Product;
use Magento\Catalog\Block\Product\ListProduct as ListProducts;
class ListProduct extends ListProducts
{
/**
*Please paste your doc block
*/
public function yourFunction()
{
//Write Your Code Here
}
}

 

Here, For example, some of you might want to add some additional data in the product listing page, that is to add a timer in product listing page in those cases then you can override ListProduct block to add custom functionalities.

<?php
namespace Egits\Customcatalog\Block\Product;
use Magento\Catalog\Block\Product\ListProduct as ListProducts;
class ListProduct extends ListProducts
{
/**
* Please paste your doc block
*/
public function getTimer()
{
$time = “Current Time”;
return $time;
}
}

Here we added our custom function getTimer() in the ListProduct block. The function simply returns the string in variable $time. And we need to call the function in our list.phtml file (that we have to override).

Now the block is overridden. You can use the same method for overriding any block in Magento 2.

2. Override model in Magento 2

Let’s override Catalog Product model

Step 1 : Create di.xml file in app/code/Egits/Customcatalog/etc to specify which model to override.

<?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="Egits\Customcatalog\Model\Catalog\Product" />
</config>

Step 2 : Now Create Product.php in app/code/Egits/Customcatalog/Model/Catalog

<?php
namespace Egits\Customcatalog\Model\Catalog;
use Magento\Catalog\Model\Product as Products;
class Product extends Products
{
/**
*  Please paste your doc block
*/
public function getSku()
{
// Please Write Your Code Here
return $this->getSku();
}
}

In this code, we have overridden the Product SKU.

3. Override Controller in Magento 2

Lets override Product View controller in magento2

Step 1 : Just like models and blocks override di.xml in app/code/Egits/Customcatalog/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="Egits\Customcatalog\Controller\Product\View" />
</config>

Step 2 : Now create View.php controller file in app/code/Egits/Customcatalog/Controller/Product

<?php
namespace Egits\Customcatalog\Controller\Product;
use Magento\Catalog\Controller\Product\View as Views;
class View extends Views
{
/**
*  Please paste your doc block
*/
public function execute()
{
// Write Your Code Here
}
}

You can use the same method for overriding any controller in magento2.

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