user1958112
user1958112

Reputation: 65

Magento: count products using stock quantity

I'm new in magento. I'm wondering how I can count all products using stock quantity. For example, I have

The result of the sum of all products should be 25

Actually, I'm using

  <?php
$prods =  Mage::getModel('catalog/product')->getCollection();
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($prods);
$count = number_format(count($prods));
echo $count;
?> 

but this counts the products without stock quantity.

Thanks for your help.

Upvotes: 2

Views: 1725

Answers (2)

Fabian Blechschmidt
Fabian Blechschmidt

Reputation: 4141

This should work, too. The reports collection joins together all the quote_items. But I'm not sure wether any order status is considered

$collection = Mage::getResourceModel('reports/product_sold_collection');
$collection->addOrderedQty();
// EDIT reading the question is all
$sum = 0;
foreach($collection as $product) {
    $sum += $product->getOrderedQty();
}
echo $sum;

Upvotes: 0

Drew Hunter
Drew Hunter

Reputation: 10114

Untested but this should get you what you need…

$stockItemCollection = Mage::getModel('cataloginventory/stock_item')
    ->getCollection();
$stockTotal = array_sum($stockItemCollection->getColumnValues('qty'));

Upvotes: 2

Related Questions