Retrieve top rated products in Magento

Here you learn how to display top rated products using an helper function and a snippet of a .phtml block.

This is the function you need to put in your helper ( mine is named ‘heart’, you’ll see that later ):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php

public function getTopRatedProducts($limit) {

        $limit = (int) $limit;

        // retrieve all the products
        $products = Mage::getModel('catalog/product')->getCollection();

        $rated = array();

        foreach($products as $product) {

            $_product = Mage::getModel('catalog/product')
                        ->load($product->getid());

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

            // retrieve reviews data related to the current products
            // of the iteration
            $summaryData = Mage::getModel('review/review_summary')
            ->setStoreId($storeId)
            ->load($_product->getId());

            // put a subarray containing name, url and rating of the
            // product in our array containing the products
            $rated[] = array(
                         'rating' => $summaryData['rating_summary'],
                         'name' => $_product->getName(),
                         'url' => $_product->getUrlPath()
                        );
        }

        // tell that the product's array must be ordered by rating DESC
        arsort($rated);

        // return the array with the amount of products defined by $limit
        return array_slice($rated, 0, $limit);
    }

So now that you are able to retrieve the products you need to put them in your Magento pages:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php

//create the array of top rated products calling our function
$products = Mage::helper('heart')->getTopRatedProducts(5);

    foreach($products as $product){

        echo "
            <li>
                <a href='{$product['url']}' />
                    {$product['name']}
                </a>
            </li>
        ";
    }

That’s it!


In the mood for some more reading?

...or check the archives.