php - Get stock quantity of entire category in Magento

917

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...

729

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.

People are also looking for solutions to the problem: php - Access denied for user, using password 'NO' only outside of public_html

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.