Magento2 – Why is Size of a Collection Greater Than the Number of Products

magento-2.1magento2

In toolbar.phtml there is $block->getCollection()->getSize() which returns 67.

However if I do count($block->getCollection()->getData()) the result is 12.

The layered navigation is also using the larger number.
The issue is on a page with a few bundle products on, so that may be a contributing factor.

Then when you go to the second page there are no results.

I am using the Wyomind_Elasticsearch and Wyomind_Core mode as the indexer for product.

Why is this happening?

Best Answer

I believe that when you call getData() you are getting the keys to the objects _data property, but in a collection, the collection "items" live in _items, so to count the items, call getItems() and count that instead:

count($block->getCollection()->getItems())

Edit: if you chase down the getSize() function, you will see a call to getSelectCountSql() in vendor/magento/framework/Data/Collection/AbstractDb.php which will return the total number of results, not just the actual set of results in the current collection.