Showing the Top Sellers from a Single Magento Category
Magento 1 already aggregates bestseller data, which you can see under Reports > Products > Bestsellers. Because the totals are pre-calculated, you can query them cheaply instead of adding up every order yourself, and you can choose a daily, monthly or yearly period.
The code
Set the number of products and the category, then run:
<?php
$productCount = 10; //insert how much prodect
$storeId = Mage::app()->getStore()->getId();
$collection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addOrderedQty()
->setStoreId($storeId)
->addStoreFilter($storeId)
->addCategoryFilter(Mage::registry('current_category'))
->setOrder('ordered_qty', 'desc')
->setPageSize($productCount);
Mage::getSingleton('catalog/product_status')
->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')
->addVisibleInCatalogFilterToCollection($collection);
?>
Refresh the bestseller statistics regularly (under Reports > Refresh Statistics, or with a cron job) so the list stays current.
Heads up: this guide applies to Magento 1, which stopped receiving official security updates in June 2020. If your store still runs it, plan a move to Magento 2 or Adobe Commerce soon. We can help with the migration.
Stuck on something similar? Our Magento development team is happy to take a look.