How can I implement stock filter in Magento way?

How can I implement stock filter in Magento way?

July 27, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How can I implement stock filter in Magento way?

Implementing an Out-of-Stock Filter in Magento: A Comprehensive Guide

Are you struggling with out-of-stock products appearing on your Magento website? Do you want to give your customers the option to exclude these items from their view? You’re not alone.

In this post, we’ll walk through a practical way to implement a custom filter that allows customers to hide or show out-of-stock products directly in the layered navigation. This solution will keep your e-commerce experience clean and customer-friendly while maintaining Magento’s core functionality.

Understanding the Default Behavior

By default, when you enable displaying out-of-stock products under System > Configuration > CATALOG > Inventory, all products—including those with zero stock—will be visible to customers in catalog views. This is a common setting that many merchants use to keep their entire product catalog accessible.

However, for an enhanced user experience and improved conversion rates, it’s often beneficial to let customers choose whether they want to see out-of-stock items or not. A custom filter provides this control without needing to change your Magento configuration.

The Step-by-Step Solution

1. Create a Custom Attribute

The first step is to create a new attribute that will act as a flag for our filter. This attribute should be of type “Yes/No” and can be named something like “Display Out-of-Stock”.

Here’s how you can add this new attribute:

  • Navigate to Catalog > Attributes
  • Click on Add New Attribute
  • Fill in the details, ensuring the Data configuration is set to Yes/No (Dropdown)
  • Click Save

2. Set Up the Filter in Layered Navigation

Next, we need to configure Magento’s layered navigation to recognize our new attribute as a filter option.

  • Go to Stores > Configuration > Catalog
  • Under the Inventory section, find the Layered navigation fieldset
  • Set the Enable filters in layered navigation for out of stock items option to No (to exclude out-of-stock products by default)
  • Save changes

3. Create a Custom Filter

Now let’s create a custom filter using Magento’s built-in options.

  • Navigate to Stores > Attributes > Manage attributes
  • Find your newly created “Display Out-of-Stock” attribute and click on Edit in the Actions column
  • In the Filterable (Storefront) section, set Yes
  • Click Save

4. Implement the Frontend Logic

To make our filter work correctly, we need to add some custom JavaScript code to handle user interactions.

Create a new JavaScript file (e.g., out-of-stock-filter.js) and add the following code:

document.addEventListener('DOMContentLoaded', function() {
  const outOfStockFilter = document.querySelector('.layered-filters .filter-item[data-code="display_out_of_stock"]');
  if (outOfStockFilter) {
    outOfStockFilter.addEventListener('click', function(e) {
      e.preventDefault();
      // Add your AJAX or custom logic here to update the product list
      alert('Out-of-stock filter clicked!');
    });
  }
});

This script targets the new “Display Out-of-Stock” filter item and prevents the default behavior when it’s clicked. You can replace the alert function with your own AJAX calls or other logic to update the product list accordingly.

5. Include the JavaScript File

Finally, include our custom JavaScript file in your Magento storefront:

  • Go to Stores > Configuration > General > Design
  • Find your theme and click on Edit
  • In the HTML Head section, add a new field for “Scripts and Style Sheets”
  • Enter the path relative to your Magento installation (e.g., /js/out-of-stock-filter.js) in the text box
  • Save changes

Best Practices and Recommendations

Use AJAX for Real-Time Updates

For a smoother user experience, consider using AJAX to update the product list without refreshing the entire page. You can leverage Magento’s built-in AJAX functionality or use third-party libraries like jQuery.

Style Your Filter Appropriately

Make sure your new filter stands out in the layered navigation menu with clear styling and easy-to-understand labels for both “Show” and “Hide” options.

Conclusion

Implementing a custom out-of-stock filter in Magento is an effective way to give customers more control over their shopping experience. By following this step-by-step guide, you can create a clean, user-friendly catalog that hides or shows out-of-stock products based on customer preference.

Remember, keeping your product catalog organized and relevant to your audience’s needs is key to driving conversions and building customer loyalty. Don’t hesitate to explore more customization options in Magento to tailor your e-commerce platform to your specific business requirements.

Visit Laravel Company for more practical guides on Magento development, optimization tips, and industry best practices.

Catalog Configuration Customers

Our network