Solved: Magento 2 Plugin Issue in Catalog Search
You can create a plugin in Magento 2 to insert code dynamically without changing the original class behavior. It allows extending the core functionality without any modification to the core files.
In Magento 1, you can rewrite a class to customize different classes and methods, but the drawback of it is, no module could rewrite the same class, and thus it has no flexibility. To overcome the rewrite conflicts and instability, Magento 2 is being introduced with inceptors or plugins!
But in Magento 2, whenever you create a virtual type plugin to override or extend functionality in the catalog search page, you get Magento 2 Plugin Issue in Catalog Search! The methods get overridden on the category page but remain unchanged and show no effect on the catalog search page. To solve the Magento 2 plugin issue in catalog search, I have come up with the following solution:
Guide to Solve Magento 2 Plugin Issue in Catalog Search:
Go to [Namespace]\[Module]\etc\frontend\di.xml and put below code:
1 2 3 4 5 6 |
<type name="Magento\Catalog\Block\Product\ListProduct"> <plugin name="[Namespace]_[Module]::ListProduct" type="[Namespace]\[Module]\Plugin\Catalog\Product\ListProduct"/> </type> <virtualType name="Magento\CatalogSearch\Block\SearchResult\ListProduct"> <plugin name="[Namespace]_[Module]::VirtualTypeListProduct" type="[Namespace]\[Module]\Plugin\Catalog\Product\ListProduct"/> </virtualType> |
That’s all about to solve the plugin issue in the catalog search in Magento 2. After overriding the above code, your extended functionality can be easily seen in effect even on the catalog search page.
We implemented the above solution to show assigned product labels on the catalog search page which was previously showing only on the category and product page.
Let me know how have you implemented the above solution to extend the functionality on the catalog search page! Do comment in the below section if you stuck somewhere or having any questions, I would be happy to help my valued readers!
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Get Value of Custom Attribute on Magento 2 Rest API
How to Get Product Image URL in Magento 2
Next