I want to get Top 5 ordered products details of specific Categories in Magento 2. How Can I do that?

Here is My Code:

$cateinstance = $objectManager->create('Magento\Catalog\Model\CategoryFactory');
$cateid = 3; 
$storeId =1;

$allcategoryproduct = $cateinstance->create()->load($cateid)->getProductCollection()->addAttributeToSelect('*')
->setOrder('ordered_qty', 'desc')

Here I'm Getting lot of information about products. But I want only the Product IDs in a array.


Since you only want the product ID there is no need to select all the attributes, which you are doing with ->addAttributeToSelect('*') so you can remove that call. Also, all collections extend \Magento\Framework\Data\Collection so you can use the getAllIds() function instead of getData().

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.