How to move 'out of stock' products to the end of the lists after search?

How to move 'out of stock' products to the end of the lists after search?

October 19, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to move 'out of stock' products to the end of the lists after search?

Moving Out-of-Stock Products to the End of Magento Search Results

As a new Magento developer, it’s common to have questions about customizing search functionality. One frequent request is to display out-of-stock products at the end of search results lists. In this post, I’ll guide you through the process step-by-step using the catalogsearch module and database tables.

Understanding the Search Process

Before we dive into the code changes, let’s understand where the data for search results comes from:

  1. Search Query: When a customer enters a search term, it gets inserted into the catalogsearch_query table.
  2. Product Matching: Magento then queries the catalogsearch_result and other related tables for matching products based on the search terms.
  3. Result Delivery: The matched products are fetched from the database and displayed to the customer.

Checking Out-of-Stock Status

You’ve correctly identified that the cataloginventory_stock_item table contains stock information, with the is_in_stock flag indicating whether a product is currently available or not (0 = out of stock).

Your goal is to filter out these products and display them after the in-stock items in search results.

Adding an Order Condition

To achieve this, we need to modify the SQL query used for fetching the search results. This query is typically executed from the Mage_CatalogSearch_Model_Mysql4_Fulltext::getLoadSelect method inside the catalogsearch/fulltext module.

Here’s a basic example of how you can add an order condition using the Zend_Db_Expr class to sort out-of-stock products at the end:

$select->order(new Zend_Db_Expr('IF(is_in_stock = 0, 2, 1)'))
       ->order('name');

In this example, we’re using a conditional expression IF(is_in_stock = 0, 2, 1) which assigns the value ‘2’ to out-of-stock products and ‘1’ to in-stock ones. When ordering by these values, Magento will display all in-stock items first followed by the out-of-stock ones.

Implementing the Changes

To apply this code change:

  1. Locate the app/code/core/Mage/CatalogSearch/Model/Mysql4/Fulltext.php file.
  2. Open it and navigate to the getLoadSelect method around line 307.
  3. Paste or modify the order condition shown above in the order clause.

Remember that modifying core files is not recommended for long-term maintainability. Consider creating a custom module using the Magento extension development guidelines from Laravel Company.

Conclusion

Moving out-of-stock products to the end of search results in Magento requires an understanding of the search process and database structure. By adding a conditional order clause, we can effectively separate these items from the main search results.

As you become more comfortable with Magento development, you’ll find that many customizations like this one involve modifying SQL queries or creating observer events to intercept and modify data flow. Always remember to test your changes thoroughly before deploying them to production environments.

Happy coding!

Catalog Database Extensions Orders

Our network