Magento load product collection including disabled products
Asked Answered
V

6

7

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.

Vouch answered 26/2, 2013 at 17:1 Comment(0)
E
8

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.

Epithelium answered 26/2, 2013 at 17:32 Comment(2)
This got me halfway there. It stopped using the flat table, but it is still joining to the table mage_catalog_category_product_index instead of mage_catalog_category_product, so I still can't get the disabled products.Verso
I got it working by applying ->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID). This would be all you needed to do, but unless you turn off Use Flat Catalog Product, Magento will try to use it when it's not supposed to and throw an error. So I had to use them both togetherVerso
S
6

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.

Suellensuelo answered 10/6, 2015 at 8:17 Comment(0)
P
0

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 ) );
Paprika answered 20/10, 2016 at 15:33 Comment(0)
C
0

When flat table is enabled, getting product collection in the frontend excludes disabled products. For ver 1.9.4 and above, we can disable flat table in the product collection with the helper:

Mage::helper('catalog/product_flat')->disableFlatCollection();
$collection = Mage::getResourceModel('catalog/product_collection');
// $collection includes both enabled and disabled products.
// To filter disabled product:
$collection
    ->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
Compassion answered 8/8, 2023 at 5:28 Comment(0)
L
-1

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

Lucillelucina answered 20/10, 2016 at 15:13 Comment(1)
This is incorrect, because when flat table is enabled, getting product collection in the frontend will exclude disabled products.Compassion
C
-5

This should load your collection filtered by disabled products.

    $products = Mage::getModel('catalog/product')->getCollection()
        ->setStoreId(Mage::app()->getStore()->getId())
        ->addAttributeToFilter('status', '0');
Contemptuous answered 26/2, 2013 at 20:24 Comment(2)
First of all, disabled status is 2. Second, the disabled products aren't in the collection to begin with, so filtering doesn't help.Verso
Disabled products stauts is "2" and not '0'Stratigraphy

© 2022 - 2025 — McMap. All rights reserved.