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

4 Comments

  • Magento developers many times get confused for designing a website in a unique way. After visiting this article, one can easily get the overview of designing a particular Ecommerce website. As the matter of fact, various platforms require different kinds of designs for better engagement of a product. So after visiting here, one can easily get the best overview of designing a website.

  • Heya! I just wanted to ask if you ever have any problems with hackers?
    My last blog (wordpress) was hacked and I ended up losing many months of hard
    work due to no data backup. Do you have any solutions to
    stop hackers?

  • Hello! I know this is kinda off topic but I was wondering which blog platform are you using
    for this website? I’m getting fed up of WordPress because I’ve had issues with hackers and I’m looking at options for another platform.
    I would be awesome if you could point me in the direction of a good platform.

Leave a Reply

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