1

How to add custom module content in magento 2.0.7 list.phtml

Without modifying core list.phtml i need to append my custom module content in magento 2.0.7 list.phtml

2
  • Can you be more specific? Commented Jul 7, 2016 at 12:09
  • ya sure ,in magento 2.0.7 category list page i want to add custom content through my custom module for example i want add custom text above the product image Commented Jul 7, 2016 at 12:25

1 Answer 1

2

You can do that if you create new module to override this block:
vendor\magento\module-catalog\Block\Product\ListProduct.php

to override this block, you need to create di.xml at ,
app\code\Vendor\Module_Name\etc

di.xml content:

<?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="Vendor\Module_Name\Block\Product\ListProduct" /> </config> 

Create new file name ListProduct.php at
app\code\Vendor\Module_Name\Block\Product

ListProduct.php content:

namespace Vendor\Module_Name\Block\Product; class ListProduct extends \Magento\Catalog\Block\Product\ListProduct { public function getProductDetailsHtml(\Magento\Catalog\Model\Product $product) { $html = $this->getLayout()->createBlock('Magento\Framework\View\Element\Template')->setProduct($product)->setTemplate('Vendor_ModuleName::test.phtml')->toHtml(); $renderer = $this->getDetailsRenderer($product->getTypeId()); if ($renderer) { $renderer->setProduct($product); return $html.$renderer->toHtml(); } return ''; } } 

You can change block Magento\Framework\View\Element\Template to your block

create yourphtml.phtml file at app\code\Vendor\Module_Name\view\frontend\templates

23
  • I am getting "Invalid block type: Magento\Catalog\Block\Product\ListProduct" My file structure and type like this Type: Vendor\Module_Name\Block\Catalog\Product file path : Vendor\Module_Name\Block\Catalog\Product\ListProduct.php Commented Jul 7, 2016 at 13:02
  • yes i got fixed ,i missed Catalog in TYPE Vendor\Module_Name\Block\Catalog\Product\ListProduct Commented Jul 7, 2016 at 13:08
  • Hi RJ07 Thank you for your answer , i have few more doubts Its adding content above the add to cart i want to add my custom content into above the category image And i want to access helper data(that mean my custom module admin settings like enable disable ) in yourphtml.phtml Commented Jul 7, 2016 at 13:15
  • Does it helped you to call your block on list page with above code? yeah please go on. Commented Jul 7, 2016 at 13:16
  • Hi RJ07 Thank you for your answer , i have few more doubts Its adding content above the add to cart i want to add my custom content into above the category image And i want to access helper data(that mean my custom module admin settings like enable disable ) in yourphtml.phtml Commented Jul 7, 2016 at 13:18

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.