php - Get stock quantity of entire category in Magento
I've got the code to get stock quantity for a specific product, but I'm looking to get the total stock quantity of an entire category (to only list categories that have inventory).
so instead of
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);
I'd like something like
$stock = Mage::getModel('cataloginventory/stock_item')->loadByCategory($category_id);
(Which isn't a real thing of course.)
Or any other method to list only those categories containing items in stock?
We're on Magento v1.7.0.2.
Thanks...
Answer
Solution:
Checking for stocks of all products for a category to conclude whether a category should be displayed or not will be too resource intensive.
My suggestion would be to set a cron script that calculates category count and populates another table or a new EAV filed for category. Also using some observer you should listen to events and update this count when inventory changes.
Now during run time just check this specific EAV field value and conclude what needs to be displayed.