问题
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