On magento product view page, i'm trying to order upsell products by best selling / more rating

On a search i found that this code will work to get order product collection by best seling,

$storeId    = Mage::app()->getStore()->getId();

$products = Mage::getResourceModel('reports/product_collection')
            ->addAttributeToSelect(array('name', 'price', 'small_image')) //edit to suit tastes
            ->setOrder('ordered_qty', 'desc'); //best sellers on top



I have two issues where to add this code & how about adding set order by rating too ?

Any help is much appreciated.

1 Answer 1


You shouldn't implement your ordering this way. The problem is, that this counts all the order_items. No index on this and with the time they increase a lot in count, therefore you can't check what the performance will be in a year or two.

I suggest to create a new attribute on the product, a static one, put an index on it and order by this. Then you can implement (an) observer(s) who updates this attribute (however you want to order it), after someone added a rating or a order is placed.

  • you're idea is really good one, but here the logic is different, here every single product is an upsell of more than one product so attribute filter wont work, that's the reason i'm going with dynamic ordering of products by bestselling / no of ratings. Mar 14, 2014 at 9:21
  • Maybe I'm wrong but the number of sells and ratings are a global value, so why not writing them/"cache" them in an attribute? Mar 14, 2014 at 12:03
  • I'm a beginner to magento, can you be bit clear ? Mar 15, 2014 at 8:06
  • The number of one selled products is the sum of all the selles of this one product, so there is only one way to calculate this value. This solution can now be saved, either into the cache or into an attribute to the product: magentocommerce.com/knowledge-base/entry/… Mar 17, 2014 at 12:45

Your Answer

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

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