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

paramuduraisamy
  • 162
  • 2
  • 13

1 Answers1

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

Rushvi
  • 2,843
  • 2
  • 14
  • 31
  • ok fine thank you for your response ,let me check – paramuduraisamy Jul 07 '16 at 12:33
  • 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

    – paramuduraisamy Jul 07 '16 at 13:02
  • yes i got fixed ,i missed Catalog in TYPE Vendor\Module_Name\Block\Catalog\Product\ListProduct – paramuduraisamy Jul 07 '16 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

    – paramuduraisamy Jul 07 '16 at 13:15
  • Does it helped you to call your block on list page with above code? yeah please go on. – Rushvi Jul 07 '16 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

    – paramuduraisamy Jul 07 '16 at 13:18
  • To do so first you needs to create admin configurations for enable/disable in system.xml as @Vendorname/Module_Name/etc/adminhtml/system.xml

    and after that you can get values of those content. Now to have clear understanding on this you can follow this links which will help you through creating configurations and accessing them. Here is the link @http://magento.stackexchange.com/questions/87789/magento2-how-to-get-data-from-system-configuration

    Also if you found something in above code you can upvote and accept it. Thanks

    – Rushvi Jul 07 '16 at 13:30
  • How i can get product id in yourphtml.phtml – paramuduraisamy Jul 11 '16 at 05:34
  • Add this code to your phtml file, $_product = $block->getProduct(); $pid = $_product->getId(); – Rushvi Jul 11 '16 at 05:42
  • Great!! RJ07 You Saved my day Thank you so much – paramuduraisamy Jul 11 '16 at 06:02
  • i have another question Can you please help me – paramuduraisamy Jul 11 '16 at 06:06
  • How we can display product with sale count in my custom module admin grid,I dont know how to create admin Grid in magento 2 – paramuduraisamy Jul 11 '16 at 06:06
  • Try this link, http://cedcommerce.com/magento-2-module-creator .. This will create skeleton of module for you and you can select grid type if you wants to create admin grid as well. Please let me know if it was helpful for you – Rushvi Jul 11 '16 at 06:31
  • Glad to know it worked for you :) It seems you forgot to upvote yet my reply :) would be glad to see your appreciation on this. Thanks in advance – Rushvi Jul 11 '16 at 06:31
  • Ya sure i do it ( It seems you forgot to upvote yet my reply ) where i need to make upvote – paramuduraisamy Jul 11 '16 at 08:53
  • http://www.awesomescreenshot.com/image/1386100/c4e88173cd39437fae205c9d2057ba98 – Rushvi Jul 11 '16 at 08:57
  • i Completed please check – paramuduraisamy Jul 11 '16 at 09:23
  • Yeah Thanks :) . Link which I gave you helped in creating grid for admin ? – Rushvi Jul 11 '16 at 09:24
  • i am currently working on that ,once complete i will let you know – paramuduraisamy Jul 11 '16 at 09:24
  • ok sure looking forward to hear from you. – Rushvi Jul 11 '16 at 09:25
  • How can i get Custom product attribute value in yourphtml.phtml – paramuduraisamy Jul 12 '16 at 05:40
  • I have updated ans here, http://magento.stackexchange.com/questions/125271/how-to-get-attribute-value-in-magento-2-0-6-product-view-and-list-page/125272?noredirect=1#comment169688_125272 . Way is same as I mentioned over there. – Rushvi Jul 12 '16 at 05:53
  • Hi How to submit the magento2 custom module config form using event observer – paramuduraisamy Jul 19 '16 at 13:22
  • @Rushvi, I am trying to show out of stock products only for specific category, how that can achieved, do i need to override this block file? – Jafar Pinjar Feb 25 '20 at 06:43