How to get products by stock qty?
You need to create a join with the cataloginventory/stock_item table to get the stock qty for a product, you could use:
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->joinField('qty',
'cataloginventory/stock_item',
'qty',
'product_id=entity_id',
'{{table}}.stock_id=1',
'left'
)->addAttributeToFilter('qty', array('gt' => 0));
ONE WAY:
$stockIds = Mage::getModel('cataloginventory/stock_item')
->getCollection()
->addQtyFilter('=', 30) //can be ->addQtyFilter('>=', 30), depending on requirement
->getAllIds();
$products = Mage::getModel('catalog/product')
->getCollection()
->addIdFilter($stockIds)
->setPageSize(10);
SECOND WAY:
$oCollection = Mage::getModel('catalog/product')
->getCollection()
->joinField(
'qty',
'cataloginventory/stock_item',
'qty',
'product_id=entity_id',
'{{table}}.stock_id=1',
'left'
)
->addAttributeToFilter('qty', array('eq' => 30));
The 'FIRST WAY' in @TBI Infotech's answer wont work, as the ->getAllIds()
method returns the stock id, not the product ID. Instead you need to add this;
$stockIds = Mage::getModel('cataloginventory/stock_item')
->getCollection()
->addQtyFilter('>=', 30);
//->getAllIds();
foreach($stockIds as $stock) {
$idarray[] = $stock->getProductId();
}
$products = Mage::getModel('catalog/product')
->getCollection()
->addIdFilter($idarray)
->setPageSize(10);