(Apologies for posting this here, and if people think this should be deleted, please do.

I posted this question on stackoverflow, having completely forgotten this forum, which of course is more appropriate for a Magento question. I'd prefer to move it here but... how?!

In order to keep only one version, I've posted the link to it here.


Btw, I found an answer although I'm sure it can be improved.)

I want my main menu to not include any categories that are empty. I've done this for the layered navigation very easily in the relevant phtml file by using


However, for the navigation menu, I'm finding it impossible to do this as easily (I have seen the Prattski example but it does seem rather OTT).

The main menu seems to be built in Mage_Page_Block_Html_Topmenu.php, specifically in the function _getHtml. This gets all the children in the menu and if I try something like $child->getId(), I get something like category-node-36.

It doesn't seem like I'm too far from being able to use getProductCount() and so test if it's more than zero.

Is it possible to do this? Can somebody point me to how?

If I can, I'll extend the class with my version.

1 Answer 1



If you ask me, filtering the categories on the menu is bad for the performance and deactivating them is a better idea.

So my suggestion is to implement a cronjob that checks all categories and child categories for products and if there are no, set the status to disabled.

And implement an observer which does the same for only one category on the event catalog_category_save_before

while loading collection

You can implement an observer which listens on catalog_category_collection_load_before and add a setLoadProductCount(true) to the collection, then you can use $category->getProductCount() in the menu template... Ah, btw when you filter the categories, the counts do mismatch, maybe this is a problem somewhere, e.g. while paging (what you don't do) this makes problems if you show fewer items than you selected.

I think you have to change the block for the top menu. I would change the reference in the layout.xml instead of rewriting the block, but this is up to you, then change _getHtml and immediately in the foreach just check the count and continue; when 0

  • I don't have many categories so I don't think the performance overhead is significant. Moreover, a cron job risks being out of date.
    – PedroKTFC
    Apr 10, 2014 at 8:51
  • Categories doesn't matter, you have to count the products ON EVERY MENU CREATION. if you don't cache this, this is really bad. But I updated my answer. Apr 10, 2014 at 9:38
  • Is it really so bad? I would hope that Magento keeps the count of products per category as you add and remove products. So all I'm doing is looping over a few categories and looking at a count attribute. Not really that heavy. So far I don't see a perceptible performance hit.
    – PedroKTFC
    Apr 24, 2014 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.