2

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

https://stackoverflow.com/questions/22822997/remove-empty-categories-from-magento-menu

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

$_category->getProductCount(). 

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.

2

cronjob

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

So my suggestion is to implement a cronjob which checks all categories and child categories for products and if there are no, set 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 less items than you selected.

I think you have to change the block for the topmenu. I would change the reference in the layout.xml instead of rewriting the block, but this is up to you, then change _getHtml and immediatly 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 '14 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. – Fabian Blechschmidt Apr 10 '14 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 '14 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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