Magento – Category active product count on category list

categorymagento-1.7php-5.4product

I am trying to display number of products in in each category..
and using this code to display all subcategory of category id:3
It is showing but it is counting not visible and disable product also,

<?php
    $cats = Mage::getModel('catalog/category')->load(3)->getChildrenCategories();
?>
<ul>
    <?php foreach($cats as $category): ?>
    <li>
         <a href="<?php echo $category->getUrl() ?>"><?php echo $category->getName() ?>(<?php echo $category->getProductCount(); ?>)</a>
    </li>
    <?php endforeach; ?>
</ul>

Is there any good solution so that i can get exact count thats are visible-catalog search and active.

thanks

Best Answer

Follow that one:

$collection=$category->getProductCollection();
$collection->setVisibility(array(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH, Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG));     
$collection->addFieldToFilter('status',Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
echo $collection->count();
Related Topic