I have created plugin to add link for every product as below:
<?php
namespace Vendorname\Modulename\Plugin;
class ProductData
{
protected $urlInterface;
protected $scopeConfig;
public function __construct(
\Magento\Framework\UrlInterface $urlInterface,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
) {
$this->urlInterface = $urlInterface;
$this->scopeConfig = $scopeConfig;
}
public function aroundGetProductDetailsHtml(
\Magento\Catalog\Block\Product\ListProduct $subject,
\Closure $proceed,
\Magento\Catalog\Model\Product $product
)
{
$result = $proceed($product);
return $result . '<a href="#">mydata</a>';
return $result;
}
}
Above is working fine added mydata link to every product. But it is not working on search page. Can anyone help me to add link to search page product using plugin
di.xml file
<?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="my-block"
type="Vendorname\Modulename\Plugin\ProductData"
sortOrder="10"/>
</type>
</config>
Best Answer
Basically the search page uses the same template as the product list BUT it uses a virtual type block that uses the
Magento\Catalog\Block\Product\ListProduct
class you're plugin.That's where the official documentation is confusing because in the list of plugin limitations it is said that:
However, the following example is given:
And it is said that:
Really confusing here, what I would try if I were you would still to try plugin the virtual type by updating your
di.xml
like this: