How can I override catalog search result in Magento 2?

How can I override catalog search result in Magento 2?

July 16, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How can I override catalog search result in Magento 2?

Overriding Catalog Search Results in Magento 2: A Comprehensive Guide

Search functionality is a critical component of any e-commerce platform, and Magento 2 provides robust built-in search capabilities. However, you may find that the default search results do not always meet your business needs. In such cases, you can override the catalog product search results to add, remove, or modify items as required.

1. Overriding Catalog Search Results Through a Plugin

In Magento 2, you can use the plugin pattern to interfere with the core logic of the search process. The Magento\Catalog\Model\Indexer\Category\Product class is responsible for retrieving products based on category and search criteria. You can create your own plugin using the Magento’s built-in plugin system.

Here’s an example of how you can create a plugin to modify the catalog product collection before it’s returned:

namespace YourVendor\YourModule\Plugin;

class CatalogProductCollection
{
    public function afterGetAllIds(\Magento\Framework\Data\Collection\AbstractDb $subject, $result)
    {
        // Add or remove product IDs from the result array here
        return $result;
    }
}

To install this plugin, you would add it to your module’s di.xml file:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Indexer\Category\Product">
        <plugin name="your_module_catalog_product_collection" type="YourVendor\YourModule\Plugin\CatalogProductCollection" sortOrder="10"/>
    </type>
</config>

2. Overriding Catalog Search Results HTML

If you need a more granular control, you can override the search results template. Magento 2 uses a block class Magento\Catalog\Block\Product\Search which is responsible for rendering the search results.

You can create your own block with the same interface as the core block and then use the plugin pattern to replace the core block:

namespace YourVendor\YourModule\Block;

class CustomSearch extends \Magento\Catalog\Block\Product\Search implements \Magento\Framework\View\Element\Template
{
    // Override necessary methods here to modify the HTML output
}

To install this, you would again use a plugin in your module’s di.xml file:

<type name="Magento\Catalog\Block\Product\Search">
    <plugin name="your_module_custom_search" type="YourVendor\YourModule\Plugin\CustomSearch" sortOrder="10"/>
</type>

Conclusion

Overriding catalog search results in Magento 2 is a powerful feature that allows you to tailor the user experience to your specific business needs. Whether you need to add or remove products from the result set, or modify the HTML output of the search results page, the plugin system provides a flexible and efficient way to achieve this.

Remember, when working with Magento 2’s plugin system, it is important to follow best practices such as keeping your plugins lightweight and performing minimal operations in them. Always test your changes thoroughly in a development environment before deploying them to a live site.

For more advanced customizations and development tips, I recommend checking out the official Magento Developer Documentation or visiting https://laravelcompany.com for expert insights on e-commerce development.

Happy coding!

Magento 2 Catalog Indexing Database

Our network