Only active product count on Magento Category

Only active product count on Magento Category

July 7, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Only active product count on Magento Category

Displaying Only Active Product Count in Magento Category

Are you trying to display the number of active products in each category on your Magento site? If so, you might have noticed that by default, Magento includes disabled or invisible products in the product count for a given category. This can lead to inaccurate and confusing information for your customers.

In this post, we’ll explore how to modify the default behavior to display only the active product count for each category. We’ll provide clear instructions and relevant code examples to help you achieve this.

The Challenge: Including Disabled Products in Count

The provided code snippet loads all child categories of a given parent category (in this case, ID 3) and displays their names along with their respective product counts:

<?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>

However, as you’ve noticed, this code includes disabled and invisible products in the product count. Let’s dive into how to fix this issue.

Solution: Filtering by Enabled Status

To display only active products, we need to filter the products based on their enabled status before counting them. Here’s an updated version of the code that accomplishes this:

<?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() ?></a> (<?php
                $products = Mage::getModel('catalog/category')->load($category->getId())
                    ->getProductCollection()
                    ->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED))
                    ->count();
            echo $products; ?>)
        </li>
    <?php endforeach; ?>
</ul>

In this updated code, we’re retrieving the product collection for each category and applying a filter to include only products with a status of enabled. The addAttributeToFilter method ensures that only products in an enabled state are considered when calculating the count.

Additional Best Practices

While updating your code, consider the following best practices:

  1. Use Product Visibility: Instead of filtering by status, you can also filter by visibility to ensure that only visible products are included in the count. Use Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE for invisible products.

  2. Optimize Performance: If your site has a large number of categories or products, consider optimizing this code by using more efficient queries or caching mechanisms to improve performance.

Conclusion: Displaying Accurate Product Counts

By understanding the need to filter active products and implementing the correct filtering methods in your Magento category display, you can provide an accurate product count for each category. This not only improves the user experience but also ensures that your site presents up-to-date and reliable information.

Remember, small details like this can significantly enhance the overall shopping experience for your customers. Don’t hesitate to explore more customizations and optimizations on Laravel Company to further refine your e-commerce platform.

Happy coding!

Catalog Customers Errors

Our network