How to count number of product in magento product listing?

How to count number of product in magento product listing?

September 3, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to count number of product in magento product listing?

Counting Total Products Per Category in Magento

As a new developer working with Magento, you may find yourself needing to display the total number of products within each category. By default, Magento’s product listing shows only the products on a single page, which can be misleading for users who want to know how many items are available in total.

Let me walk you through the steps to achieve this:

Understanding the Current Code

Your current code snippet is a good starting point but it doesn’t provide the desired result. The line echo $_productCollection->count(); only returns the number of products on the currently loaded page, not the total count for the category.

<?php
$_productCollection = $this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
echo $_productCollection->count();
?>

Fetching All Products in a Category

To get the total number of products in a specific category, you need to fetch all products for that category without any pagination. Here’s how you can modify your code:

<?php
$categoryId = 123; // Replace with your desired category ID
$_productCollection = Mage::getModel('catalog/category')->load($categoryId)
    ->getProductCollection()
    ->addAttributeToSelect('*')
    ->setOrder('position', 'asc')
;
echo $_productCollection->getSize();
?>

In this updated code:

  1. Replace 123 with the ID of your desired category.
  2. We use Mage::getModel('catalog/category')->load($categoryId) to load the specific category by its ID.
  3. The line ->getProductCollection() gets all products assigned to that category.
  4. ->addAttributeToSelect('*') ensures all product attributes are selected for further operations.
  5. ->setOrder('position', 'asc') orders the products based on their position attribute in ascending order, which is a common practice.
  6. Finally, echo $_productCollection->getSize(); outputs the total number of products in that category.

Conclusion

Remember to clear your cache after making these changes for them to take effect.

This approach provides an accurate count of all products in a specific category, regardless of pagination or current page limitations. It’s a fundamental feature for enhancing user experience and helping customers understand the product range within each category.

For more advanced Magento customization techniques, consider checking out Laravel Company’s comprehensive guides on Magento Development. They provide in-depth tutorials and best practices from experienced developers.

Happy coding! Let me know if you have any other questions.

Catalog Orders

Our network