How to get the collection of “instock” products through Resource Model in Magento

梦想的初衷 提交于 2019-12-23 05:32:05

问题


Is it possible to fetch the collection of "in stock" products through resource model in Magento ?

I have tried this code

$collection = Mage::getResourceModel('catalog/product_collection')
        ->addAttributeToSelect('*');
$collection->addAttributeToFilter('is_in_stock', 0);

But it is throwing Fatal error


回答1:


try below code

$productCollection = Mage::getResourceModel('catalog/product_collection');
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($productCollection);
echo "<pre>";print_r($productCollection->getData());

// use addInStockFilterToCollection()




回答2:


Just add at the end of product collection code

Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($productCollection);



来源:https://stackoverflow.com/questions/22276027/how-to-get-the-collection-of-instock-products-through-resource-model-in-magent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!