Magento: Filter products by Status

Tristar Web Design picture Tristar Web Design · Jul 11, 2011 · Viewed 48.2k times · Source

I'm having some serious Magento issues here. As expected the following:

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

Will return all enabled products for my $category_id. However this:

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

Does not return disabled products. I can't seem to find a way to return disabled products, and I don't know why.

I've tried this:

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

Which was meant to have worked, but apparently may have been deprecated.

Does anyone know how to get all products in a category, enabled and disabled?

Answer

Jürgen Thelen picture Jürgen Thelen · Jul 12, 2011

Don't worry, you simply got trapped by a very unusual constant definition^^. Just try:

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

For whatever reasons Varien decided to define this STATUS_DISABLED constant with a value of 2, instead of the more intuitive (and commonly used) value of 0.