Magento category product count

Home / Magento / Magento category product count

Many times we need to show product count per category in magento. So here I am showing to how many products (Product count) in that categories. First of all I get all categories based on category position with ascending order and then get product count for that category.

It shows like below example:

Laptops (4)
Hard Drives (4)
Root Catalog (5)
Monitors (4)
Shirts (19)
Shoes (46)

Get category and product count in magento

<?php
$categories = Mage::getModel('catalog/category')->getCollection()
             ->addAttributeToSelect('name')
             ->addAttributeToSelect('url_key')
             ->addAttributeToSelect('my_attribute')
			 ->addAttributeToSelect('position')
			 ->addAttributeToSort('position', 'ASC')
             ->setLoadProductCount(true)
             ->addAttributeToFilter('is_active',array('eq'=>true))
             ->load();
?>

<?php foreach($categories as $key=>$category): ?>
<?php if($category->getName() != ''):?>
	<?php $prodCollection = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($category); // Magento product collection ?>
	<a href="<?php echo $category->getUrl() ?>"><?php echo $category->getName() ?></a> (<?php echo $prodCollection->count() ?>)<br/>
<?php endif;?>
<?php endforeach; ?>

Related Posts

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *