1

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?

  • Do you need the block or you just want to override list.phtml – Vivek Kumar Apr 5 '18 at 7:35
3

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.

  • error removed but my template is not calling – Pradip Garchar Apr 5 '18 at 7:23
  • @PradipGarchar I have updated my answer for template issue. Please check. – Emipro Technologies Pvt. Ltd. Apr 5 '18 at 7:28
  • not working template not applying – Pradip Garchar Apr 5 '18 at 7:33
  • @PradipGarchar don't forget to delete generated the folder before you check. – Emipro Technologies Pvt. Ltd. Apr 5 '18 at 7:33
  • @PradipGarchar have you create the file in following path Kite/Boxes/view/frontend/templates/catalog/product/list.phtml – Emipro Technologies Pvt. Ltd. Apr 5 '18 at 7:35
2

I think you can override list.phtml without overriding List.php. If you have custom module then you need to create catalog_category_view.xml under layout directory

app\code\Kite\Boxes\view\frontend\layout\catalog_category_view.xml

In this layout file, you can write

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="category.products.list">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Kite_Boxes::product/list.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>
  • i want to render my listing in some conditinoal case so it won't be possible using handle – Pradip Garchar Apr 5 '18 at 7:46
  • 1
    Can't you simply use xml with ifconfig for this? Or a helper function? Else a Plugin or observer would be preferable to defining a preference – simonthesorcerer Apr 5 '18 at 8:28
2

Another way, you can use do via Plugin

Create a plugin of class Magento\Catalog\Block\Product\ListProduct.

And after plugin method afterGetTemplate() of getTemplate.You can change the template file of that block class.

Plugin class:

<?php
namespace Kite\Boxes\Plugin\Block;

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

}

And 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">
    <type  name="Magento\Catalog\Block\Product\ListProduct">
       <plugin name="changes_template_of_product_list" sortOrder="1"  disabled="false"
       type="Kite\Boxes\Plugin\Block\ListProductPlugin"/>
    </type>

</config> 

Note that: At plugin class,you can get all public function $subject variable.

Most important:

  • You should do di complie

  • And Cache flush

  • And static content deploy for active plugin
0

Update your block code to following ;

<?php
namespace Kite\Boxes\Block\Rewrite\Product;
 class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
    {
       protected function _construct(\Magento\Framework\View\Element\Template\Context $context)
            {
                 $this->setTemplate('Kite_Boxes::catalog/product/list.phtml');
                 parent::__construct($context);
            }
    }
0

Step 1 – Create a di.xml file in a folder Kite/Boxes/etc

<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>

Step 2 - The next step to overriding Magento2 block is to create a ListProduct.php Block file in the folder Kite/Boxes/Block/Rewrite/Product

<?php

namespace Kite\Boxes\Block\Rewrite\Product;
class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
    {
        public function __construct()
            {
                echo "you can do this in block"; die();
            }
    }

Step3 - app/code/Kite/Boxes/view/frontend/layout/catalog_category_view.xml File,

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
        <!-- for magento catalog list overrider -->
        <referenceBlock name="category.products.list" template="Kite_Boxes::product/list.phtml" />          
</body>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.