How to add product qty to your product collection

March 25, 2013 15:14

Sometimes you have to add the product qty to your product collection which is not included in default. Normally people will get the collection first, then run the below code per product:

$qtyStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();

If you have more than 10000 records, it spends too much time by additional SQL query and increase server loading. Actually you can join the inventory table when you are getting the collection as below codes:

$productCollection = Mage::getModel('catalog/product')->getCollection();
$productCollection->joinField('qty', 'cataloginventory/stock_item','qty','product_id=entity_id', '{{table}}.stock_id=1','left');

foreach ($productCollection as $product) {
    echo $product->getQty();

More efficient, right?

Happy Magento’ing ~


2 Responses to “How to add product qty to your product collection”

  1. I am interested in hiring a PHP/Joomla developer to work with us to improve our current web site. We are a new start-up firm in Hong Kong, and at this point, prefer someone who can work on freelance basis.

    Please let me know if you are available, and how we can discuss your timing, cost and our project.


  2. Thanks for your message. Sorry that I'm very busy recently on my new born baby. However, if you are still looking for the freelance programmer for Joomla, could you please send the project detail to my email websitecomment at this domain and I can then refer to my other partners.