3

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; 
$productCount=5;
$storeId =1;

$allcategoryproduct = $cateinstance->create()->load($cateid)->getProductCollection()->addAttributeToSelect('*')
->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
->setOrder('ordered_qty', 'desc')
->setPageSize($productCount)->setStoreId($storeId)->addStoreFilter($storeId);
$allcategoryproduct_count=$allcategoryproduct->getSize();
$allcategoryproduct_data=$allcategoryproduct->getData();

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

0
1

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.