Google+

Pages

Sunday, July 22, 2012

Magento: Disable category from top menu if list is empty for that category

Hi friends,

Many times we face a problem that we add categories to our magento store, but for one or more categories, we don't have products to show customer at that time.

In that case, we may not want to show that category in top menu, as it gives a bad impression to customers that there is no product in category.

You can hide that particular category from Top Menu, if it has no product. To do this, go to:

app/code/core/Mage/Catalog/Block Folder and copy Navigation.php Override Navigation.php 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;
}

Save file and enjoy.. :)

2 comments:

Augusta Infosol said...

Hi,

Very useful, but here you have forgot to filter collection by in stock filter. That's why if a category has products, but all of them are not in stock, then category will still be displayed in menu.

Anonymous said...

can you help me ..
How to display all "subcategory" of a particular category upon clicking on that category.
please guide me t solve it out