Manish Raghani
Manish Raghani

Reputation: 51

How to get stock status from product collection in magento

here is my product collection can anyone help to get me stock status from product collection.

$collection = Mage::getModel('catalog/category')
                ->load($categoryId)
                ->getProductCollection()
                ->addAttributeToSelect('*')
                ->addAttributeToFilter('visibility', array(
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG));
return $collection;

Upvotes: 3

Views: 8297

Answers (2)

sv3n
sv3n

Reputation: 347

Stock status (is_in_stock) is part of every product collection.

foreach ($collections as $product) {
    echo $product->getStockItem()->getIsInStock();
}

Returns 1 if in stock, else null.

If you need other stock information like min_qty or backorders you can add this to your collection:

$collection->setFlag('require_stock_items', true);

More details: https://magento.stackexchange.com/questions/106455/get-product-stock-quantity-in-magento/209510#209510

Upvotes: 6

Nirav Joshi
Nirav Joshi

Reputation: 2950

$collection = Mage::getModel('catalog/category')
                ->load($categoryId)
                ->getProductCollection()
                ->addAttributeToSelect('*')
                ->joinField('qty','cataloginventory/stock_item','qty','product_id=entity_id','{{table}}.stock_id=1','left')        
                ->addAttributeToFilter('visibility', array(
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG));
    return $collection;

Here you can do it like this

You have to add join Field for stock items ->joinField('qty','cataloginventory/stock_item','qty','product_id=entity_id','{{table}}.is_in_stock=1','left')

Upvotes: 3

Related Questions