1

I am preparing the product collection as follows

$visibleProducts = $this->catalogProductVisibility->getVisibleInCatalogIds();
$collection = $this->productCollectionFactory->create();
$collection->addAttributeToFilter(
    'status',
    ['in' => $this->productStatus->getVisibleStatusIds()]
);
$collection->setVisibility($visibleProducts);
$collection->addAttributeToFilter('sku', ['in' => $skus]);
$collection->addStoreFilter();

$collection->addAttributeToSelect(['new', 'name'])
            ->addAttributeToFilter('new', 1)
            ->setPageSize($limit);

$collection->getSize() and count($collection->getItems()) is showing different numbers.

Why is this so?

  • which one is wrong $collection->getSize() or count($collection->getItems()) ? – Chirag Patel Jun 28 at 10:49
1
$collection->getSize()

this will return all the products count, it ignores pagination

whereas

count($collection->getItems())

this will show you the count on current page

  • 3
    What if I don't have pagination? – Anshu Mishra Jun 28 at 10:34
  • remove ->setPageSize($limit); from collection, and both will show same result – Shoaib Munir Jun 28 at 10:51
1

$collection->getSize() - getsize return all the products count, it ignores pagination

count($collection->getItems()) - will show you the count on current page

  • +1 on your explanation, I think this wil also clear concepts of getSize and count in different way :) – Shoaib Munir Aug 26 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.