Question

I am making a Mage::getModel call to try and get all products in a certain category that is in stock.

$stockIds = Mage::getModel('cataloginventory/stock_item')
    ->getCollection()
    ->addQtyFilter('<=',1)
    ->addFieldToFilter('is_in_stock', 1)
    ->getAllIds();

$_productCollection=Mage::getModel('catalog/category')
    ->load(51)
    ->getProductCollection()
    ->addIdFilter($stockIds)
    ->addAttributeToSelect('*')
    ->setPageSize(10)->setCurPage(1);

Everything works apart from the ->addFieldToFilter('is_in_stock', 1) line. Without it all products in the category show up. With it, it's filtering some (apparently random) products out, but not based on whether it is 'In stock'. Some out-of-stock items still appears, and some in-stock items do not appear.

I have checked that all the products are enabled and all quantities are at least 10.

How do I get all products in a certain category that is in stock?

Was it helpful?

Solution

first,$stockIds don't return the product ids ,it give the stock id,basically stock table primary key. If you want to filter product collection by saleable then you need use below code:

Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($_productCollection)
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top