2
votes

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

  • category 1
  • product one - stock 10
  • product two - stock 5
  • category 2
  • product three - stock 10

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.

2

2 Answers

2
votes

Untested but this should get you what you need…

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

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;