I'm trying to get a list of all the products in the store using the product-collections but it doesn't respect the page and page-size params. I'm using the sample data and have 174 products in my store but irrespective of the page number and size supplied when querying the collection:
/** @var Store $store */
$store = $this->_storeManager->getStore(true);
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
$collection = $this->getCollection($store);
$collection = $collection->addAttributeToSelect('*');
$collection = $collection->setCurPage(100);
$collection = $collection->setPageSize(1000);
$collection = $collection->load();
var_dump($collection->getSize());
//var_dump($collection->count());
//var_dump(count($collection->getData()));
I've tried to output the size of the collection after querying it but the number of results are incorrect.
Best Answer
Take a look at the definition of
getSize
(it's in\Magento\Framework\Data\Collection\AbstractDb
). You'll see the following:The method
getSelectCountSql
does exist in the original class, but if you follow it down:To:
To:
You'll see that
getSize
will always reset the LIMIT and perform a MySQL query to give you the size of the full collection (assuming I didn't miss somewhere that sets_totalRecords
outside of that method). Given that you've already loaded the collection, callingcount($collection)
should result in the right number.