1

I want to get the random 10 products from whole product collection in magento2.

$productCollection = $this
        ->productModel
        ->getProductCollection()
        ->addAttributeToSelect('*');

$productCollection->getSelect()->orderRand();

$productCollection->setPagesize(10);

How to get the products without loading the whole product collection.

Thanks.

0

Try This :-

// Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $_collection
$collection = $this->_collection->create();
        $collection->addAttributeToSelect('*')
           ->getSelect()->orderRand()->limit(10);

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.