Question

In my system flat setting has been enable for products and as per as magento system magento exclude disable products from product collection whenever flat table is creating. That why we can not get disable product collection on frontend

Now i want to get product collection with disable product at frontend.

Was try below code with set store id as admin for store but it does not works.

Please help me

trying

$coutPro=Mage::getModel('catalog/product')->getCollection()-
    ->addAttributeToFilter('amit_status', array(
    'nin' => array(8),
    ))->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID);
Was it helpful?

Solution

take a look at this method Mage_Catalog_Model_Resource_Product_Collection::_construct

protected function _construct()
{
    if ($this->isEnabledFlat()) {
        $this->_init('catalog/product', 'catalog/product_flat');
    }
    else {
        $this->_init('catalog/product');
    }
    $this->_initTables();
}

this means that the collection will use flat tables if the result of isEnabledFlat is true.
If you look at the isEnabledFlat you will see that it returns false for the admin otherwise it returns the status of the flat flag for each store. Actually it's more than that. even if the flag is enabled it may return false if the the flat index is not up to date.

So what you can do is add this at the top of your code:

Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));

This will make the collection use the admin store.

[EDIT]
If you use this on frontend you will get issues, because you will see the values from admin and you don't want that.

My second idea is to rewrite the product collection model and change the behavior of the method isEnabledFlat and make it look like this:

public function isEnabledFlat()
{  
    //add this
    if (Mage::registry('use_product_eav')) {
        return false;
    }
    return parent::isEnabledFlat();
}

then all you have to do is add in your code this Mage::register('use_product_eav', true) before instantiating your collection.
Make sure you call Mage::unregister('use_product_eav') after instantiating it if you don't want the rest of the page to use eav tables.

OTHER TIPS

Since I only need to get the count of some disabled products, I have taken Marius' first answer and modified it as follows:

// Temporarily set the store to the admin one, otherwise the collection below will omit disabled products
$oldStoreId = Mage::app()->getStore()->getId();
$newStoreId = Mage_Core_Model_App::ADMIN_STORE_ID;
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load($newStoreId));

// Search for disabled products
$disabledProducts = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_DISABLED));

// Reset the store to what it was before
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load($oldStoreId));
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top