7

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>
  • 2
    Could you post your di.xml file please ? – Raphael at Digital Pianism Jun 29 '16 at 9:36
  • @RaphaelatDigitalPianism I have added di.xml code it is working fine but how can i achieve same function on search page. – Prashant Valanda Jun 29 '16 at 9:41
  • @PrashantValanda I have facing same issue if you have got solution so please put here. – Deexit Sanghani Dec 14 '17 at 11:42
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>
  • I have tried with your given solution it is not working. Having any other way? – Prashant Valanda Jun 29 '16 at 10:02
  • 1
    @PrashantValanda I'm trying to find out the clarification about virtual types – Raphael at Digital Pianism Jun 29 '16 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> 

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.