Magento – Get current categories of subcategory with product count in magento

example :

Furniture

     – Living Room (4)

     – Bedroom (2)

CODE to get categories/subcategories and products count in magento:
------- ----------------- ----------------------
/**
* 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
}
}

-------------- ------------------- -----------------
Advertisements