8

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>
asked Jun 29, 2016 at 9:34
4
  • 2
    Could you post your di.xml file please ? Commented Jun 29, 2016 at 9:36
  • @RaphaelatDigitalPianism I have added di.xml code it is working fine but how can i achieve same function on search page. Commented Jun 29, 2016 at 9:41
  • @PrashantValanda I have facing same issue if you have got solution so please put here. Commented Dec 14, 2017 at 11:42
  • Add an additional entry for SearchList class in di.xml Commented Jun 2, 2020 at 5:20

3 Answers 3

0

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>
answered Jun 29, 2016 at 9:49
2
  • I have tried with your given solution it is not working. Having any other way? Commented Jun 29, 2016 at 10:02
  • 1
    @PrashantValanda I'm trying to find out the clarification about virtual types Commented Jun 29, 2016 at 10:05
0
 <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> 
answered May 18, 2017 at 7:00
0

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>
answered Dec 9, 2018 at 13:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.