Home » Magento

Magento: Get sub categories and product count

21 December 2009 9,786 views Popularity: 20% Share/Bookmark

email

Here, I will be showing you the code to get sub categories of a particular category and the number of products (product count) present in the sub categories.

Like, I have a category named Furniture. The sub categories under Furniture are Living Room and Bedroom. Now, I 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)


/**
 * get current category
 */
$currCat = Mage::registry('current_category');

/**
 * get sub categories of current category
 */
$collection = Mage::getModel('catalog/category')->getCategories($currCat->getEntityId());

/**
 * looping through sub categories
 * only showing active sub categories ($cat->getIsActive())
 */
foreach($collection as $cat) {
	if($cat->getIsActive()) {
		$category = Mage::getModel('catalog/category')->load($cat->getEntityId());

		/**
		 * getting product collection for a particular category
		 * applying status and visibility filter to the product collection
  		 * i.e. only fetching visible and enabled products
  		 */
		$prodCollection = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($category);
		Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($prodCollection);
		Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($prodCollection);

		?>

		<a href="<?php echo $category->getUrl() ?>"><?php echo $category->getName() ?></a> (<?php echo $prodCollection->count() ?>)<br/>

		<?php
	}
}

Related posts:

  1. Magento: Get list of all Categories
  2. Magento: Get category name and url from product
  3. Magento: How to filter product collection using 2 or more category filters?
  4. Magento: Get parent id of simple product associated to configurable product
  5. Magento: How to get all associated children product of a configurable product?
  6. Magento: Get Product Collection by Type
  7. Magento: Load store specific product
  8. Magento: Product Edit Warning: Invalid argument supplied for foreach()
  9. Magento: Sort latest product by ‘created date’ and ‘new from date’
  10. Magento: Describing Flat Catalog
  • http://binod-luitel.com.np Binod

    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

  • http://blog.chapagain.com.np Mukesh

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

  • http://1lagu.com 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.