Magento 2, PHP

Magento: 2 Overriding Block

Lets override catalog product ListProduct block.

Step: 1 – First of all create di.xml file in Folder Done/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="Done\Hello\Block\Rewrite\Product\ListProduct" />
</config>

Step:2 – Now create ListProduct.php Block file in Folder Done/Hello/Block/Rewrite/Product

<?php
namespace Done\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.

Tags :