Magento – Magento 2 create plugin for the module

magento2plugin

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.

<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>
Related Topic