Magento: Filter products by Status

Magento: Filter products by Status

July 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: Filter products by Status

Magento: Filter Products by Status

Are you facing trouble filtering products based on their status in your Magento site? You’re not alone! Many developers struggle with this common issue.

The Problem at Hand

Let’s first understand what isn’t working for you:

$products = Mage::getModel('catalog/category')->load($category_id)
    ->getProductCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('status', array('eq' => 0));

This code is supposed to retrieve all disabled products in a specific category. However, it fails to deliver the expected results.

A Deeper Dive

Magento’s product collection filter for status uses a simple integer: 1 for enabled and 0 for disabled. The issue lies not with your syntax but with Magento’s filtering behavior.

By default, Magento only includes products that are visible in both the catalog and search layers when you retrieve products from a category. This is why even though you’re specifically asking for ‘disabled’ status (status = 0), it doesn’t work as expected.

Understanding the VisibleFilterToCollection Method

You tried using this method:

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);

This was indeed a possible solution, but you’re right that it may have been deprecated in later Magento versions. The method is no longer officially documented and might not work consistently across all installations.

So, how do we get around this? Here’s the recommended approach:

  1. Use the ‘visible’ attribute filter: Instead of trying to force a status filter, you can use the ‘visible’ attribute. In Magento, ‘visible = 0’ means the product is disabled.
$collection = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect(array('sku', 'name', 'price', 'visibility'))
    ->setStoreId(Mage::app()->getStore()->getId())
    ->addAttributeToFilter(array(
        array('attribute' => 'visibility',
            'eq' => Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE),
        array('attribute' => 'status', 'neq' => 2)
    ));

$products = $collection->getItems();

In this code:

  • We’re using the setStoreId method to ensure the collection is properly scoped.
  • The addAttributeToFilter method applies two filters: one for products that are not visible (visibility = Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE) and another for products that are not deleted (status ≠ 2).

Additional Best Practices

Remember these best practices when working with product collections:

  • Use proper scoping: Always ensure your collection is scoped to the correct store view using setStoreId.
  • Limit attributes: Only select the necessary attributes using addAttributeToSelect to improve performance.
  • Use getItems: Don’t rely on $collection->getData(), as it won’t return all products. Use $collection->getItems() instead.

Conclusion

Filtering products by status in Magento can be a bit tricky, but understanding the underlying filters and attributes is key. By using the ‘visible’ attribute and proper scoping techniques, you can successfully retrieve both enabled and disabled products from a category.

Remember to test your code thoroughly across different store views and product types to ensure it works as expected.

For more advanced Magento tutorials and tips, check out Laravel Company. We cover the latest best practices and help you stay ahead in the world of e-commerce development.

Catalog Pricing Errors

Our network