Magento2 Template – Product List Template Not Overriding with Set Template

magento2magento2.1.0

Template doesn't set from my block.

My di.xml

<?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="Kite\Boxes\Block\Rewrite\Product\ListProduct" />
</config>

My Block File is,

<?php

namespace Kite\Boxes\Block\Rewrite\Product;

class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
{
    protected function _construct()
    {
        $this->setTemplate('Kite_Boxes::catalog/product/list.phtml');
    }
}

Error occurred like,

Fatal error: Uncaught TypeError: Argument 1 passed to Magento\Catalog\Block\Product\ListProduct::__construct() must be an instance of Magento\Catalog\Block\Product\Context, none given, called in /var/www/vhosts/goweb-dev01.site/httpdocs/var/generation/Kite/Boxes/Block/Rewrite/Product/ListProduct/Interceptor.php on line 14 and defined in /var/www/vhosts/goweb-dev01.site/httpdocs/vendor/magento/module-catalog/Block/Product/ListProduct.php:66 Stack trace: #0 /var/www/vhosts/goweb-dev01.site/httpdocs/var/generation/Kite/Boxes/Block/Rewrite/Product/ListProduct/Interceptor.php(14): Magento\Catalog\Block\Product\ListProduct->__construct() #1 /var/www/vhosts/goweb-dev01.site/httpdocs/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(73): Kite\Boxes\Block\Rewrite\Product\ListProduct\Interceptor->__construct() #2 /var/www/vhosts/goweb-dev01.site/httpdocs/vendor/magento/framework/ObjectManager/ObjectManager.php(57): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Kite\\Boxes\\Bloc...', Array) #3 in /var/www/vhosts/goweb-dev01.site/httpdocs/vendor/magento/module-catalog/Block/Product/ListProduct.php on line 66

Does anyone have idea about this problem?

Best Answer

There is no issue you just need to delete generated folder and then check.

If the cache is enabled then you need to refresh cache using

php bin/magento cache:clean

Update your di.xml with this code,

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type  name="Magento\Catalog\Block\Product\ListProduct">
       <plugin name="template_of_product_list" sortOrder="1" disabled="false" type="Kite\Boxes\Plugin\ListProduct"/>
    </type>
</config>

Create Plugin Kite/Boxes/Plugin/ListProduct.php

<?php

namespace Kite\Boxes\Codepend\Plugin;

class ListProduct
{
    public function afterGetTemplate(
        \Magento\Catalog\Block\Product\ListProduct $subject,
        $result
    ) {
        return 'Kite_Boxes::catalog/product/list.phtml';
    }

}

After this you can check your productlist page.