Accessing product rating summaries
13 Oct 2009, by Peter Spiller
2 Comments · Posted in Magento

    Product ratings are an important tool for gaining credibility and driving sales in any online store, and are yet another powerful feature built-in to the Magento core. Ratings can be a valuable criteria to make visible to people browsing the site, so rather than hide them away on product pages only, it can be desirable to display rating summary data elsewhere, or to use the data in developing other custom functionality. Review summaries work a little differently to other product data, so this post will demonstrate how to access them.

    First of all, load the product using the standard method of loading a model:

    $productId = 1234;
    $product = Mage::getModel('catalog/product')->load($productId);

    Next, we have to populate the product object with the review data as this is not automatically loaded as part of the above step. This is because the review information is a completely separate set of information, which needs to be loaded independently for a particular product.

    $storeId = Mage::app()->getStore()->getId();
    Mage::getModel('review/review')->getEntitySummary($product, $storeId);

    Now our product object contains review summary data, which can be accessed in a similar way to other product data:

    $ratingSummary = $product->getRatingSummary();

    The code above will output something like:

        [primary_id] => 27
        [entity_pk_value] => 16
        [entity_type] => 1
        [reviews_count] => 3
        [rating_summary] => 51
        [store_id] => 1

    The most useful fields here are reviews_count and rating_summary. reviews_count is the number of approved reviews; pending reviews aren't counted. rating_summary is the average score for all of the star ratings for all of the product's approved reviews. This is shown as a number out of 100, and the scores from pending reviews aren't counted.

    To access these fields directly you can call their get methods:


    One thing to remember is that the summary data is scoped to the store ID you specify. Reviews can be associated with one or more stores, and reviews that aren't associated with the store you specify won't be included when the summary is calculated.

    Using the above methods, you should be able to make use of review summaries anywhere you like. For example, you may want to show a star rating on a featured product in a sidebar, or include the data in a customised product export. If you have reviews on a lot of products, also consider adding an option to sort categories by product ratings. By displaying a quick overview of the reviews for a product you can increase interest in that product and encourage further customer participation.