4

I've created a plugin to override getProductPrice() method of Magento\Catalog\Block\Product\ListProduct class. This is my app/code/vendorName/moduleName/etc/di.xml:

<?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="vendorName_moduleName_Plugin" type="vendorName\moduleName\Plugin\MyPlugin" />
    </type>
</config>

and this plugin class (app/code/vendorName/moduleName/Plugin/MyPlugin.php):

<?php

namespace vendorName\moduleName\Plugin;

class MyPlugin
{
    public function afterGetProductPrice(\Magento\Catalog\Block\Product\ListProduct $product, $result)
    {
        return $result.'<input type="hidden" class="my-class" />';
    }
}

It works fine when a category page is called but it doesn't override methods when I try to search a product.

Why?

2 Answers 2

5

Thanks to @fschmengler and reading Magento 2: virtual types plugins I solved using this app/code/vendorName/moduleName/etc/di.xml:

<?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="vendorName_moduleName_Plugin" type="vendorName\moduleName\Plugin\MyPlugin" />
    </type>
    <virtualType name="Magento\CatalogSearch\Block\SearchResult\ListProduct">
        <plugin name="vendorName_moduleName_VirtualPlugin"
                type="vendorName\moduleName\Plugin\MyPlugin" />
    </virtualType>
</config>
2
  • OK, then this probably has changed in 2.1, glad that you figured it out! Dec 19, 2016 at 16:22
  • In effect, I'm using 2.1.2 In any case, it's so strange.. type attribute in <virtualType> element is mandatory but it takes "parent" Plugin so you could write everything in there.
    – WaPoNe
    Dec 19, 2016 at 16:26
0

You need to create another plugin for it

app/code/vendorName/moduleName/etc/di.xml:

<?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\CatalogSearch\Block\SearchResult\ListProduct">
        <plugin name="vendorName_moduleName_Plugin" type="vendorName\moduleName\Plugin\MyPlugin" />
    </type>
</config>

Your plugin file at app/code/vendorName/moduleName/Plugin/MyPlugin.php

<?php

namespace vendorName\moduleName\Plugin;

class MyPlugin
{
    public function afterGetProductPrice(\Magento\CatalogSearch\Block\SearchResult\ListProduct $product, $result)
    {
        return $result.'<input type="hidden" class="my-class" />';
    }
}
6
  • But Magento\CatalogSearch\Block\SearchResult\ListProduct class doesn't exist.
    – WaPoNe
    Dec 19, 2016 at 13:18
  • If you go to layout of catalogsearch_result_index you will find <block class="Magento\CatalogSearch\Block\SearchResult\ListProduct" name="search_result_list" template="product/list.phtml" cacheable="false"> Dec 19, 2016 at 13:23
  • Ok but has it got getProductPrice() method? If I follow your advice, I receive this error message: Exception #0 (RuntimeException): Source class "\Magento\CatalogSearch\Block\SearchResult\ListProduct" for "Magento\CatalogSearch\Block\SearchResult\ListProduct\Interceptor" generation does not exist.
    – WaPoNe
    Dec 19, 2016 at 13:30
  • In any case, I'm trying to debug and when search result page is loaded, it calls getProductPrice() method of Magento\Catalog\Block\Product\ListProduct class but Plugin is not able to override it. It's so strange..
    – WaPoNe
    Dec 19, 2016 at 13:54
  • 1
    Magento\CatalogSearch\Block\SearchResult\ListProduct is a virtual type and the actual class is Magento\Catalog\Block\Product\ListProduct (see: github.com/magento/magento2/blob/…) - so the plugin should work. See: magento.stackexchange.com/questions/123366/… Dec 19, 2016 at 14:45

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.