7
votes

I need to filter a magento product collection by disabled status. Magento seems to by default ignore disabled products when loading the collection.

So there are two parts to my question:

1 - How can i load a collection in magento containing only disabled products? 2 - Why is magento not loading disabled products in the collection to begin with?

I am using standard code to load the collection:

$collction = Mage::getModel('catalog/product')->getCollection()

this never loads disabled products.

5

5 Answers

8
votes

If you use the product flat structure, then

$col = Mage::getModel('catalog/product')->getCollection();

will use the flat table (eg. catalog_product_flat_1), and disabled products are not part of that table.

Change the config Use Flat Catalog Product to "NO" and you will have all products in the collection: catalog-flat

It will load all products this way.

6
votes

If you don't want to change your config in the back office (to keep good performances), I found this solution that changes the value just while your request is processed, because I want the disabled products only for a specific function:

Mage::app()->getStore()->setConfig('catalog/frontend/flat_catalog_product', 0);

You have to set this before the first time you call:

Mage::getModel('catalog/product')->getCollection()

I noticed you can't set it back to 1 during the same request, probably because the collection model store keep this flag in memory.

This way, the config in my database wasn't changed.

0
votes

With flat product enabled, you'd better use

$collection = Mage::getResourceModel('catalog/product_collection')

You will be able to retrieve disabled and enabled products.

See this answer for a better explanation: https://magento.stackexchange.com/a/40001/32179

0
votes

Once you get your collection you can join table using ->joinTable('cayaloginventory/stock_item','product_id=entity_id',array('stock_status'=>'is_in_stock'))

This will load the table that you need with products that are in stock, after just add ->addAttributeToFilter('status', array('eq' => 1) )... this will load only AVAILABLE.. if you want to load DISABLE change status to 2

To get Disabled Collection of ALL PRODUCTS

        $collection = Mage::getModel('catalog/product')
                    ->getCollection()
                    ->joinTable( 'cataloginventory/stock_item', 'product_id=entity_id', array("stock_status" => "is_in_stock") )        
                    ->addAttributeToFilter('status', array('eq' => 2 ) );
-5
votes

This should load your collection filtered by disabled products.

    $products = Mage::getModel('catalog/product')->getCollection()
        ->setStoreId(Mage::app()->getStore()->getId())
        ->addAttributeToFilter('status', '0');