how to add hide out of stock filter (layered navigation) in magento?

how to add hide out of stock filter (layered navigation) in magento?

September 28, 2026 · Illustrated author avatarBy Stefan Bogdanescu
how to add hide out of stock filter (layered navigation) in magento?

Adding an “Out of Stock” Filter to Magento Layered Navigation

If you’re looking to add a custom filter to your Magento layered navigation that hides out-of-stock items, you’ve come to the right place. In this guide, we’ll walk you through the process step-by-step.

Understanding the Problem

The issue often arises because the default Magento layered navigation doesn’t consider stock availability when filtering products. This is where our custom filter comes into play.

Step 1: Create a Custom Attribute for Stock Status

Before we dive into the code, let’s ensure you have an attribute in your product catalog that represents whether an item is out of stock or not. You can create this manually through the Magento admin panel under “Attributes” > “Manage Attributes”.

Create a new drop-down attribute called “Stock Status”. Add two options: “In Stock” and “Out of Stock”. Save this attribute.

Step 2: Create the Custom Filter

Now, let’s create the PHP file for our custom filter. Navigate to your Magento root directory via SSH or FTP, then go into app/code/local/Magento/Catalog/Model/Resource/Filter.

Create a new folder called “StockStatus”. Inside this folder, create two files:

  1. core.php
  2. index.php

Step 3: Implement the Filter

Open index.php and add the following code:

class Index extends Mage_Core_Model_Resource_Filter_Abstract
{
    public function getName()
    {
        return 'Stock Status';
    }

    protected $_filterModel = 'catalog/filter_stockstatus';

    public function apply(Mage_Catalog_Model_Layer $layer)
    {
        if (count($layer->getState()->getFilters())) {
            $this->_setCurrentFilter($layer);
            $this->_applyFacetedSearch($layer, 'catalog/filter_stockstatus');
            $this->_resetCurrentFilter();
        }
    }

    protected function _getAttributeModel()
    {
        return Mage::getSingleton('eav/config')->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'stock_status');
    }
}

This code sets up the basic structure for our filter and defines its behavior.

Step 4: Create the Filter Model

Now open core.php and add this code:

class Core extends Mage_Core_Model_Filter_Abstract
{
    public function getName()
    {
        return 'Stock Status';
    }

    protected $_requestVar = 'stock_status';

    protected function _getItemsData()
    {
        $collection = $this->_getAttributeModel()->getSource()->getAllOptions(false);
        $items = array();
        foreach ($collection as $item) {
            if ($item['label'] != '_empty_') {
                $items[] = array(
                    'value' => $item['value'],
                    'label' => $item['label']
                );
            }
        }
        return $items;
    }

    protected function _getItemsCountSql($countSql, $filter, $requestVar)
    {
        $stockStatus = $this->getRequest()->getParam($requestVar);
        if ($stockStatus != null) {
            $stockStatusValues = explode(',', $stockStatus);
            $joinCondition = '';
            foreach ($stockStatusValues as $stockStatusValue) {
                $joinCondition .= ' OR p.stock_status LIKE \'' . Mage::helper('core')->escapeHtml($stockStatusValue) . '\'';
            }
            $countSql->where('(p.entity_id IN (SELECT entity_id FROM ' . $this->_getTableName('cataloginventory/stock_item') . '
                    WHERE stock_status = 1 AND quantity > 0)) OR (' . substr($joinCondition, 3) . ')');
        } else {
            $countSql->where('(p.entity_id IN (SELECT entity_id FROM ' . $this->_getTableName('cataloginventory/stock_item') . '
                    WHERE stock_status = 1 AND quantity > 0))');
        }
        return $countSql;
    }

    protected function _getItemsSql($filter, $requestVar)
    {
        $stockStatus = $this->getRequest()->getParam($requestVar);
        if ($stockStatus != null) {
            $stockStatusValues = explode(',', $stockStatus);
            $joinCondition = '';
            foreach ($stockStatusValues as $stockStatusValue) {
                $joinCondition .= ' OR p.stock_status LIKE \'' . Mage::helper('core')->escapeHtml($stockStatusValue) . '\'';
            }
            return '(' . substr($joinCondition, 3) . ')';
        } else {
            return '1=0';
        }
    }

    protected function _getAttributeModel()
    {
        return Mage::getSingleton('eav/config')->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'stock_status');
    }
}

This file creates the logic for our filter model.

Step 5: Save and Clear Cache

Save these files and clear your Magento cache. You can do this from the admin panel or via SSH or FTP by deleting all files in var/cache/.

Step 6: Test Your New Filter

Navigate to a category page on your storefront that displays layered navigation. You should now see a “Stock Status” filter with options for “In Stock” and “Out of Stock”. When you select “Out of Stock”, it will hide all out-of-stock items from the product list.

Conclusion

Adding a custom filter to Magento’s layered navigation requires a few steps, but the process is straightforward once you understand how the system works. Following this guide should allow you to easily hide out-of-stock products from your storefront.

If you’re interested in learning more about Magento development and best practices, check out our blog at LaravelCompany. We cover a wide range of topics related to e-commerce development and optimization.

Catalog Indexing Admin Errors

Our network