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.Preference configurations are falls inside the di.xml file of module.
Preferences are also answerable for Abstraction-Implementation mappings. ObjectManager used the preference to identify the default implementation class for the respective interface. Example: Magento\Framework\Logger\Monolog is the default implementation class for the Psr\Log\LoggerInterface
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 Mageclues\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:Acl/etc/acl.xsd">
<preference for="Magento\Catalog\Block\Product\ListProduct" type="Mageclues\Override\Block\Rewrite\Product\ListProduct" />
</config>
Step 2: You need to create ListProduct.php Block file in directory Mageclues\Override\Block\Rewrite\Product
<?php
namespace Mageclues\Override\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 Mageclues\Override\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="Mageclues\Override\Model\Rewrite\Catalog\Product" />
</config>
Step 2: You need to create Product.php Model file in directory Mageclues\Override\Model\Rewrite\Catalog
<?php
namespace Mageclues\Override\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 Mageclues\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="Mageclues\Override\Controller\Rewrite\Product\View" />
</config>
Step 2: You need to create View.php controller file in directory Mageclues\Override\Controller\Rewrite\Product
<?php
namespace Mageclues\Override\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();
}
}
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.
Thank you):