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>
3 Answers 3
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.
<virtualType name="Magento\CatalogSearch\Block\SearchResult\ListProduct" type="Magento\Catalog\Block\Product\ListProduct">
<arguments>
<argument name="catalogLayer" xsi:type="object">Magento\Catalog\Model\Layer\Search</argument>
</arguments>
</virtualType>
That's where the official documentation is confusing because in the list of plugin limitations it is said that:
Plugins cannot be used with Virtual types
However, the following example is given:
<config>
<type name="{ObservedType}">
<plugin name="{pluginName}" type="{PluginClassName}" sortOrder="1" disabled="true"/>
</type>
</config>
And it is said that:
type name. A class, interface, or virtual type, which the plugin observes.
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:
<?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>
<type name="Magento\CatalogSearch\Block\SearchResult\ListProduct">
<plugin name="my-search-block"
type="Vendorname\Modulename\Plugin\ProductData"
sortOrder="10"/>
</type>
</config>
-
I have tried with your given solution it is not working. Having any other way?Prashant Valanda– Prashant Valanda2016年06月29日 10:02:45 +00:00Commented Jun 29, 2016 at 10:02
-
1@PrashantValanda I'm trying to find out the clarification about virtual typesRaphael at Digital Pianism– Raphael at Digital Pianism2016年06月29日 10:05:02 +00:00Commented Jun 29, 2016 at 10:05
<type name="Magento\Catalog\Block\Product\ListProduct">
<plugin name="my-block"
type="Vendorname\Modulename\Plugin\ProductData"
sortOrder="10"/>
</type>
<virtualType name="Magento\CatalogSearch\Block\SearchResult\ListProduct" type="Magento\Catalog\Block\Product\ListProduct">
<plugin name="my-search-block"
type="Vendorname\Modulename\Plugin\ProductData"
sortOrder="10"/>
</virtualType>
add this in your di.xml. its displaying on catalog search page.
<type name="Magento\CatalogSearch\Block\SearchResult\ListProduct">
<plugin name="catalog-search-block" type="Vendor\Module\Plugin\ProductData" sortOrder="11"/>
</type>
di.xmlfile please ?di.xml