0

I need to overwrite the product listing (category) page, a custom form needs to show after that product information.

So I have found that already an extension is overwriting that page as below

<referenceBlock name="category.products.list">
        <action method="setTemplate">
            <argument name="template" xsi:type="string">Custom_ModuleName::list.phtml</argument>
        </action>
    </referenceBlock>

How to add that custom form after the each products from my custom module? enter image description here Please advise.

  • What is purpose of this block class.You have to call your block at Custom_ModuleName::list.phtml inside for each loop – Amit Bera Jul 12 at 8:11
  • Hi Amit, the purpose is I have to show grid-based form to fill quantity. How to call by block in third party extension? please share some code if possible – Antony Jul 12 at 8:33
  • Please check my last update also. – Rohan Hapani Jul 12 at 8:41
  • Hi Rohan, Thank you. Let me try and update you. Can I able to call a phtml file in that method? As I have to show a form to add to cart. – Antony Jul 12 at 8:45
  • Yes. You can do anything in this function. – Rohan Hapani Jul 12 at 9:00
2

Create di.xml file at /app/code/Vendor/Module/etc/frontend/di.xml :

<type name="Magento\Catalog\Block\Product\ListProduct">
    <plugin name="block-product-list" type="Vender\Module\Plugin\ProductList"/>
</type>

Create Plugin file ProductList.php at /app/code/Vendor/Module/Plugin :

<?php
namespace Vender\Module\Plugin;

class ProductList
{   
    protected $layout;

    public function __construct(
        \Magento\Framework\View\LayoutInterface $layout
    ) {
        $this->layout = $layout;
    }

    public function aroundGetProductDetailsHtml(
        \Magento\Catalog\Block\Product\ListProduct $subject,
        \Closure $proceed,
        \Magento\Catalog\Model\Product $product
    ) {
        return $this->layout->createBlock('Vendor\Moduele\Block\YourBlock')->setProduct($product)->setTemplate('Vendor_Module::yourPhtml.phtml')->toHtml();
    }               
}

UPDATE :

For display in catalog search also you need to create

<virtualType name="Magento\CatalogSearch\Block\SearchResult\ListProduct">
    <plugin name="block-product-list" type="Vendor\Module\Plugin\ProductList" />
</virtualType>

Clean cache and refresh page.

  • Thanks. I can see the text under each product, but can you share how to call a phtml file to show a form (for add to cart) – Antony Jul 12 at 9:02
  • Please check my updated code and let me know if still not working. – Rohan Hapani Jul 12 at 9:13
  • @Antony It's working now? – Rohan Hapani Jul 12 at 9:23
  • checking will update you. thanks again. – Antony Jul 12 at 9:49
  • 1
    using this I can able to get $block->getProduct()->getId(); – Antony Jul 12 at 14:59

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.