Magento 2.3 add custom block under each product in category page
-
07-03-2021 - |
Domanda
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? Please advise.
Soluzione
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.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange