Hiding Out of Stock Products from Search Results
Imagine you’re running an e-commerce store powered by Magento. You have a vast catalog of products, some of which are currently out of stock but still visible in your search results. This can confuse customers and lead to abandoned carts. In this post, we’ll walk through how to hide these out-of-stock items from the search page only.
Understanding the Standard Configuration
By default, Magento hides out-of-stock products across the entire storefront using the following configuration:
- Go to System > Configuration.
- Select Catalog under the Catalog menu.
- Click on Inventory.
- Find the Display Out of Stock Products setting and set it to No.
However, this hides all out-of-stock products everywhereâcategories, product lists, and even the search page. We want more control than that.
Creating a Custom Solution
To hide out-of-stock items only from the search results, we’ll need to create a custom plugin for Magento’s catalog search module. Here are the steps:
- Create a Module
First, create your own module called HideOutOfStockFromSearch. Follow best practices for creating modules as outlined in our Magento 2 Module Development Guide.
- Override the Search Results Collection
In your module’s codebase, navigate to the etc directory and open the di.xml file. You’ll need to add a new plugin for the Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection class.
<type name="Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection">
<plugin name="hide_out_of_stock_from_search" type="Vendor\HideOutOfStockFromSearch\Plugin\CatalogSearch"/>
</type>
- Create the Plugin Class
Now, create the CatalogSearch class in your module’s plugin directory:
namespace Vendor\HideOutOfStockFromSearch\Plugin;
class CatalogSearch
{
public function afterLoad($subject)
{
$collection = $subject->getItems();
foreach ($collection as $item) {
if (!$item->isAvailable()) {
$subject->removeItemByKey($item->getId());
}
}
return $subject;
}
}
This plugin removes any item from the search results collection that is not available (i.e., out of stock). It does so only for the search results, leaving the rest of your product catalog unaffected.
- Compile and Enable Your Module
After saving all changes:
- Run
php bin/magento setup:upgradeto install your module. - Run
php bin/magento setup:di:compileto ensure your plugin is properly compiled. - Run
php bin/magento cache:flushto clear the cache.
Testing Your Setup
Now, when a customer searches for an out-of-stock product, it will not appear in the search results. Only products that are actually available for purchase will be shown.
Remember, this custom solution requires some development knowledge and may need adjustments depending on your specific Magento version or theme customizations. Always ensure you have proper backups before making changes to your production environment.
Conclusion
Hiding out-of-stock products from search results is a simple yet powerful way to improve the user experience in your e-commerce store. By following this guide, you can create a clean and effective solution that doesn’t disrupt other parts of your catalog or inventory management.
If you’re looking for more advanced customization techniques in Magento, consider exploring our Magento Development Services. We offer expert consultation and development to help you maximize the potential of this powerful platform.