Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
397 views
Welcome To Ask or Share your Answers For Others

1 Answer

To do this, go to:

app/code/core/Mage/Catalog/Block Folder and copy Navigation.php and override it in your local package.

Open Navigation.php of your package and paste below code in this file:

if ($category->getIsActive()) {
    $cat = Mage::getModel('catalog/category')->load($category->getId());
    $products = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($cat);
    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
    Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($products);
    if(count($products)==0)
       return;
}

I hope my code will help you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...