1

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

1 Answer 1

3

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();
1
  • yes, using collection is the right way to fix your problem.
    – dagfr
    Commented Jul 31, 2013 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.