4

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);
13

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.

  • @Marius..Already saw Mage_Catalog_Model_Resource_Product_Collection that why i was asking this quesion .if i add this code to frontend then is it create issue in frontend Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));? – Amit Bera May 29 '15 at 7:42
  • yep. That's true. if you use it on fronted you will get issues. I have an other idea. I will update the answer in a few minutes. – Marius May 29 '15 at 7:46
  • @AmitBera. I updated the answer. – Marius May 29 '15 at 7:53
  • It just awasome... accept +1upvote.Salute marius – Amit Bera May 29 '15 at 8:34
  • awsome trick +1 from me @Marius – Keyur Shah May 30 '15 at 8:14
0

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));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.