Home » Magento21 December 2009

Magento: Get sub categories and product count

This article shows how to get sub categories of a particular category and the number of products (product count) present in the sub categories.

Suppose, you have a category named Furniture. The sub categories under Furniture are Living Room and Bedroom. Now, you want to show the sub categories under Funiture and the products associated with the sub categories (Living Room and Bedroom).

Furniture
– Living Room (4)
– Bedroom (2)

Thanks.

Magento

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • Hello Mukesh,
    I tried your code but
    $collection = Mage::getModel(‘catalog/category’)->getCategories($currCat->getEntityId());

    is not returning anything. Is there any other way to do this.

    Thanx

  • @Binod: You should be in any category page. You will not get anything if you are in homepage or any other CMS pages.

    Go to any category page like Furniture. You will definitely get result there.

  • gg

    Can u specify the file name, location of the file that need to put the code inside?
    Please do the same things for the other posts if u can.

  • Irvin

    Thanks a bunch for this info!

    By the way, I’d just like to add something. For some reasons, getCategories won’t return categories that have the attribute “Include in Navigation Menu

    ” set to NO. It took me about an hour and a few hair strands to realize that… :)

  • Weburn It

    its awesome! Thanks for your support Chapagain. I really love this blog so much.

  • Weburn It

    Inside Foreach should be like this in order to make sure that the product count without Out Stock

    $category = Mage::getModel(‘catalog/category’)->load($cat->getEntityId());

    $layer = Mage::getSingleton(‘catalog/layer’);

    $layer->setCurrentCategory($category);

    $prodCount = $layer->getProductCollection()->getSize();

  • Asdf

    A huge help once again. This blog has been such a huge help to me. You’re awesome.

  • Asdf

    A huge help once again. This blog has been such a huge help to me. You’re awesome.

  • Mai Thanh

    Is it possible to count products per website/store? I have 3 websites in one single installation. They’re sharing categories, but obviously, they have different product counts.

  • Rishad

    Hi Mukesh,

    I am a Magento developer looking for a help from your side. I have to show category filter in the form a tree structure in search result page. For example, If a user search with keyword test, then the left side category filter must show in following form

    Pants
    |
    —–Jeans(4)
    |
    —-Linen(4)
    |
    —–Slim(2)

    That is, I need all categories always visible in layered navigation. Is there any trick? please help.

    Thanks, Rishad

  • This should work:-

    $productCollection = Mage::getResourceModel('catalog/product_collection') ->setStoreId(Mage::app()->getStore()->getId()) ->addCategoryFilter($category);

  • This FREE Magento extension might be useful for you:-

    http://www.manadev.com/advanced-layered-navigation-ce

  • A huge help once again. Really very Very Helpful Blog!!!

  • Guest

    product count is showing inactive and disabled products in product count .i want to remove that.

    __(‘Browse By’) ?>

    getStoreCategories() ?>

    0): ?>

    getName() ?>

    load($_category->getId()) ?>
    getChildrenCategories() ?>
    0): ?>

    getName() ?> (getProductCount() ?>)

  • rakesh

    i want remove inactive and disabled products from product count
    $_category = Mage::getModel(‘catalog/category’)->load($_category->getId())

    $_subcategories = $_category->getChildrenCategories()
    if (count($_subcategories) > 0):

    foreach($_subcategories as $_subcategory):

    href=”echo $_helper->getCategoryUrl($_subcategory) ”
    echo $_subcategory->getName() (echo $_subcategory->getProductCount() )

  • Loek

    add Mage::getSingleton(‘cataloginventory/stock’)->addInStockFilterToCollection($collection);
    to Filter out of stock items in your collection

  • comicer

    thanks man, you saved my day!