Magento - getting the wrong number of the method count()

Magento - getting the wrong number of the method count()

August 13, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - getting the wrong number of the method count()

Understanding Magento Product Listing Count: A Comprehensive Guide

Magento is a powerful e-commerce platform that offers a wide range of features for managing your online store. One common issue faced by many developers is the discrepancy between the actual number of products assigned to a category and the number displayed in a CMS page using <?php echo $this->getLoadedProductCollection()->count(); ?>. Let me help you understand why this happens and how to fix it.

The Problem: Product Listing Count vs. Assigned Products

When you assign 12 products to a category (let’s say Phone with ID 12), you would expect that any page displaying these products should show the full count of 12. However, if you use the code snippet provided in your CMS page template (list2.phtml) and echo the collection count, you might get an unexpected result, like 9 instead of 12.

This discrepancy is due to Magento’s built-in pagination system. By default, Magento limits the number of products displayed on a single page to prevent overwhelming users with too many options at once.

The Culprit: Default Products per Page Setting

In your backend configuration under System > Configuration > Catalog > Frontend > Product Listing Options, you’ll find “Products per Page on Grid Default Value”. The value set here (9 in your case) determines how many products will be shown on each page of the category listing.

When you use <?php echo $this->getLoadedProductCollection()->count(); ?> in a template, Magento applies this pagination and returns only the number of items visible on that specific page. It doesn’t return the total count of assigned products.

Solutions: How to Get the Correct Product Count

  1. Disable Pagination: If you want to display all 12 products without any pagination, you can temporarily disable it in your template. Set <?php $this->getCollection()->setPageSize(null); ?> before calling count(). This tells Magento not to apply pagination to this collection.
<?php $this->getLoadedProductCollection()->setPageSize(null);
echo $this->getLoadedProductCollection()->count();
?>
  1. Use the Total Count Helper: Magento provides a helper method called $this->helper('catalog/output')->getTotalItemCount($_productCollection) that returns the total number of items in the collection, including those beyond the current page limit.
<?php echo $this->helper('catalog/output')->getTotalItemCount($this->getLoadedProductCollection()) ?>
  1. Customize the Products per Page: If you want to permanently change the “Products per Page” setting for this specific category, you can create a custom layout update XML in your theme or module.
<layout>
  <catalog_category_view id="category_products">
    <reference name="product_list">
      <action method="setCategoryProductLimit"><limit>12</limit></action>
    </reference>
  </catalog_category_view>
</layout>

Conclusion

Magento’s pagination system is a crucial feature that improves user experience by limiting the number of products displayed on each page. However, it can sometimes lead to confusion when trying to retrieve the total product count.

By understanding how Magento applies pagination and using the appropriate methods (like setting setPageSize(null) or using the helper method), you can accurately display the total number of assigned products in your templates.

Remember, for more advanced customizations and detailed documentation on Magento development, consider exploring the resources available at Laravel Company. They offer a wealth of information on e-commerce platforms like Magento to help you build robust and efficient online stores.

Catalog Themes Admin Configuration

Our network