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?

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

2 Answers 2

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

2
  • 3
    What if I don't have pagination? Jun 28, 2019 at 10:34
  • remove ->setPageSize($limit); from collection, and both will show same result Jun 28, 2019 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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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