How to get stock status from product collection in magento

How to get stock status from product collection in magento

July 6, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to get stock status from product collection in magento

Retrieving Stock Status from a Magento Product Collection

As an experienced developer, I understand that retrieving specific product attributes like stock status can be crucial for your e-commerce operations in Magento. Let’s dive into how to achieve this using your existing product collection.

Understanding Your Current Collection

Your current code snippet is already quite comprehensive and demonstrates best practices for loading a category and its associated products:

$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));

This will return a collection of all visible products assigned to the specified category.

Retrieving Stock Status

Now, let’s modify this snippet to include the stock status attribute:

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

Notice the addition of 'stock_status' to the addAttributeToSelect method. This tells Magento to load the stock status attribute along with all other attributes.

Accessing Stock Status Data

Once you have your collection, you can iterate over it and access the stock status:

foreach ($collection as $product) {
    $stockStatus = $product->getStockStatus();

    if ($stockStatus == 1 && Mage::helper('cataloginventory')->isSaleable($product)) {
        // Product is in stock and saleable
    } else {
        // Product is not in stock or not saleable
    }
}

The getStockStatus method returns the current stock status of each product. It can have two values:

  • 0: Out of Stock
  • 1: In Stock

Filtering by Stock Status

If you want to filter your collection based on stock status, use the following code:

$collection->addAttributeToFilter('stock_status', array(
    'eq' => 1 // Only products with stock status 1 (in stock)
));

This will ensure that only products in stock are included in the result set.

Conclusion

Retrieving stock status from a Magento product collection is straightforward once you understand how to select and filter attributes. By using the addAttributeToSelect method, you can efficiently load multiple attributes including stock status.

Remember, always consider performance when selecting many attributes at once. For large collections or complex queries, consider limiting the number of selected attributes or optimizing your database for better performance.

If you’re looking to optimize your e-commerce experience further, check out LaravelCompany’s guide on Magento 2 optimization.

Catalog

Our network