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

magento

Hi
I have assign 12 product to category Phone(id=12), and

{{block type="catalog/product_list" category_id="12" template="catalog/product/list2.phtml"}}

in a CMS page, I have placed

<?php
$_productCollection=$this->getLoadedProductCollection();
echo $_productCollection->count()
?>

to list2.phtml

Why it get 9 instead of 12?

I set "Products per Page on Grid Default Value=9" in backend, is this affecting the result above, and how to fix it?

Thanks you

Best Answer

Instead of...

echo $_productCollection->count()

...try this method:

echo $_productCollection->getSize()

Internally getSize() uses getSelectCountSql() which takes the original select statement, strips off page limitations and queries the database directly. So it returns the correct answer even after the shorter, paginated, list has been loaded. Whereas count() returns the actual number of items in the loaded list.

Related Topic