Reputation: 247
I would like to have Magento display the total count of the products including the products in subcategories. Eg. if the Main category has two products and a subcategory has five products.
I.e.:
How can I do this?
Upvotes: 0
Views: 1406
Reputation: 1261
Or else you can use this function.
public function getProductCount($category)
{
$prodCollection = Mage::getResourceModel(’catalog/product_collection’)->addCategoryFilter($category);
Mage::getSingleton(’catalog/product_status’)->addVisibleFilterToCollection($prodCollection);
Mage::getSingleton(’catalog/product_visibility’)->addVisibleInCatalogFilterToCollection($prodCollection);
return $prodCollection->count();
}
Upvotes: 0
Reputation: 1261
Try with this code.
<ul>
<?php
// This is category id
$id = 42;
$cat = Mage::getModel('catalog/category')->load($id);
$subcats = $cat->getChildren();
foreach(explode(',',$subcats) as $subCatid):
$_category = Mage::getModel('catalog/category')->load($subCatid);
if($_category->getIsActive()):
$productCount = Mage::getModel('catalog/category')->load($_category->getId())->getProductCount();?>
<li><a href="<?php echo $_category->getURL();?>"><span><?php echo $_category->getName();?>
<?php echo '('.$productCount.')'?></span></a></li>
<?php endif;
endforeach;?>
</ul>
Upvotes: 1