0

I override Mage/Catalog/Block/Product/List.php block and I want to display all products out of stock and if qty is 0 as well as in stock products progrmmatically.

protected function _getProductCollection()
    {   
        parent::_getProductCollection();

        $this->_productCollection->addAttributeToSelect('*')
        ->addAttributeToFilter(
        array(
            array('attribute'=> 'is_in_stock','eq' => 0),
            array('attribute'=> 'is_in_stock','eq' => 1)
        )
        );

     echo "<pre>";
     print_r($this->_productCollection->getData());
     die();

        return $this->_productCollection;
    }
10
  • Why don't you just configure Magento to show out of stock products too? Mar 6, 2016 at 19:45
  • You are right I respect your answer but I want to do with this way.Could you help me ? Mar 6, 2016 at 19:47
  • Just get the collection without "addAttributeToFilter" and you will get both in stock and out of stock products. Do you need this? Mar 6, 2016 at 19:48
  • I want out of stock and in stock products collection.I tried this but I am getting collection in stock products. Mar 6, 2016 at 19:50
  • Are you using flat catalog option in your Magento? Mar 6, 2016 at 19:51

1 Answer 1

1

Your problem comes from Mage_Catalog_Model_Resource_Product_Collection::isEnabledFlat since you are probably using the flat catalog option and you asked to hide out of stock products. So it is just not adding them to flat tables.

Just change your code removing the filter, you do NOT need it, it is useless:

->addAttributeToFilter(
        array(
            array('attribute'=> 'is_in_stock','eq' => 0),
            array('attribute'=> 'is_in_stock','eq' => 1)
        )
        ); // This is completely useless!

As you can see from the code Mage_Catalog_Model_Resource_Product_Collection constructor it uses a different resource model depending on your config:

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

Now, if you see isEnabledFlat you will notice it checks for the admin store ID:

public function isEnabledFlat()
    {
        // Flat Data can be used only on frontend
        if (Mage::app()->getStore()->isAdmin()) {
            return false;
        }
        $storeId = $this->getStoreId();
        if (!isset($this->_flatEnabled[$storeId])) {
            $flatHelper = $this->getFlatHelper();
            $this->_flatEnabled[$storeId] = $flatHelper->isAvailable() && $flatHelper->isBuilt($storeId);
        }
        return $this->_flatEnabled[$storeId];
    }

What you can do is to fake the admin store:

You have to rewrite Mage_Catalog_Model_Layer::getProductCollection by adding this code at the very first lines:

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

Otherwise:

You could just rewrite Mage_Catalog_Model_Resource_Product_Collection and change the isEnabledFlat as follows:

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

If it oes not work

Just check if you are getting filters from parent class.

10
  • I removed filter and follow your code changed isenabledflat but still not display out of stock products Mar 6, 2016 at 20:08
  • So check you parent function, you probably have a filter there Mar 6, 2016 at 20:09
  • In parent core class it default filter by category. Mar 6, 2016 at 20:11
  • That's weird, you should see them just getting products collection. Mar 6, 2016 at 20:14
  • P.S: use a foreach instead of "->getData()", I didn't see it before. Mar 6, 2016 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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