Magento – Getting Products Count and Products Per Page

ce-1.8.1.0pagination

I'm using this code getting products. And also using some addAttributeToFilter method for filter.

$collection = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addStoreFilter()
    ->setOrder('created_at', 'desc')
    ->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());

Also I'm using $collection->count() code to get products count.

I'm trying to write my own pagination. (My products page is really complicated.)

Then I use this code setPage($pageNumber, $productsPerPage) to paginate.

But if I'm gonna paginate $collection->count() function return $productsPerPage number.

For example:

setPage(1, 6) // 6 product per page
$collection->count() // return 6 (but I have 16 products!)

If I use only $collection->count() code, it return 16!

What I'm missing?

I hope you guys understand me.

Best Answer

$collection->count() loads the collection and returns the number of products matching the select associated to the collection.
I think you are looking for $collection->getSize(). This will get you the total number of products matching the select, but ignores the limit.
Also getSize does not load the collection, so it's faster.