Magento: Display Only If There Are Products In The Category

Magento Icon

This is a more difficult post to title – but I think that should do it. Here was my need today: We’ve got some navigational links in the header, and there are two categories “Sale Items” and “Internet Only” that should show up as links ONLY if there are products in those categories – otherwise, they shouldn’t be there (no need to have a link to a category with no products in it).

The Solution

There’s a quick’n’easy code line that you can pass in a category id to, and it will return how many products are in that category:


So – I got the category IDs from the admin and I created the following code to check the count – and display the link only if there are products in that category:

    $sale_count = Mage::getModel('catalog/category')->load(198)->getProductCount();
    if ($sale_count > 0) : ?>
        <li class="sale"><a href="/sale/">Sale Items</a></li>
    $internet_count = Mage::getModel('catalog/category')->load(200)->getProductCount();
    if ($internet_count > 0) : ?>
        <li class="internet"><a href="/internet-only/">Internet Only</a></li>

Hopefully you can find that useful if you need it!

This entry was posted in Magento. Bookmark the permalink.

6 Responses to Magento: Display Only If There Are Products In The Category

  1. Alex says:

    Loading every category in navigation is not really good idea. I won’t recommend so if you worry about performance

  2. marlize says:

    Where do I post this code pleasE?

  3. Flaxton says:


    I have been working on listing categories with their images within certain category pages by creating a new template file and inserting it into a CMS block. Everything seems to be working perfectly, but the category listing I created will display categories even if there are no products in them. I would like for the category+image not to display if there are not any product in that category. Could you offer some advice, any help is much appreciated.

    __('Browse Products') ?>

    getIsActive()): ?>
    load($_category->getId()); ?>

    setCurrentCategory($cur_category); ?>
    <a href="getCategoryUrl($_category) ?>" title="htmlEscape($_category->getName()) ?>">
    <img src="" width="auto" alt="htmlEscape($_category->getName()) ?>" />

    htmlEscape($_category->getName()) ?>


    setCurrentCategory($_maincategorylisting); ?>

  4. Vangilsweb says:

    We use the Easy Catalog Images extension. Which file do we have to edit with this code to automatically hide empty categories on anchor pages?

  5. Dan says:

    Thanks for the tip mate, very useful for my deals website I am building where I needed to count the current deals,


  6. Bimal says:

    Thank you..
    You don’t need the categoryID.. It’s also dynamic :