Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento: Filter products by Status

Tags:

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?

like image 888
Tristar Web Design Avatar asked Jul 11 '11 13:07

Tristar Web Design


1 Answers

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.

like image 182
Jürgen Thelen Avatar answered Feb 08 '23 23:02

Jürgen Thelen