1

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?

1 Answer 1

1

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

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.