Display product’s rating beyond the review page

Display product’s rating beyond the review page

Reviews are another one of numerous excellent features in Magento. Usage of reviews can increase visitors’ trust in your brand, which, in return boosts your sales by a significant amount.

Sometimes there is a need for showing star ratings outside the products’ review page. That’s what I’ll be addressing with this article.

The following code snippet works just about anywhere you have a product loaded (or if you know it’s ID) – cart being one of the examples.

<?php
$_product = $_item->getProduct(); //get the product in cart
$storeId = Mage::app()->getStore()->getId();
$summaryData = Mage::getModel('review/review_summary')
->setStoreId($storeId)
->load($_product->getId());
 
if ($summaryData['rating_summary']):?>
<div class="ratings">
<div class="rating-box">
<div class="rating" style="width:<?php echo $summaryData['rating_summary']; ?>%"></div>
</div>
</div>
<?php endif; ?>

If we visit our cart page, we can see that rating stars are shown for every product customers have rated:

product-rating-example

The array of keys for values we can access in $summaryData object are as follows:

//Entity id of a summary review
["primary_id"] => string(3) "100"
//
//Product id
["entity_pk_value"] => string(3) "119"
//
//Entity type id: 1-Product; 2-Customer; 3-Category
["entity_type"] => string(1) "1"
//
//Qty of reviews
["reviews_count"] => string(1) "2"
//
//Summarized rating: the percentage which represents number of stars, each 20% step fills up one star
["rating_summary"] => string(2) "80"
//
//Store id
["store_id"] => string(1) "1"

Keep in mind that the “Magento way” of showing this would be creating a block or a helper with a method that accepts two parameters – product ID and store ID, and echoing it’s return value from the view file.

However, you could also insert this code snippet anywhere in the view file for quick fetch of star ratings for your product.

Be careful to echo the rating value following the HTML markup (lines 7-11) above, otherwise, the stars won’t be shown.

As always, you can view my code example on GitHub

Note: This is a revamp of the article originally written in January 2009.

You made it all the way down here so you must have enjoyed this post! You may also like:

External database connection in Magento Damir Serfezi
Damir Serfezi, | 4

External database connection in Magento

Enabling Multi-part MIME Emails in Magento Tomislav Nikcevski
Tomislav Nikcevski, | 3

Enabling Multi-part MIME Emails in Magento

Pimcore Portlets Zoran Salamun
Zoran Salamun, | 0

Pimcore Portlets

11 comments

  1. Hey there, thanks for the post.
    One question: is there a way to add a review via wysiwyg? Or could i call a php file from wysiwyg maybe, which contains your script?

  2. It’s also noting that where the product is loaded, such as in ‘catalog/product/view.phtml’ you can just use:

    $_product->getRatingSummary();

    Which is usefull for displaying a star rating on a section of the product page outside of the reviews:

    $ratingSummary = $_product->getRatingSummary();
    echo $ratingSummary->getReviewsCount();
    echo $ratingSummary->getRatingSummary();

  3. Does this take into consideration the Approved and then Disapproved reviews? I have a product with a review but then disapproved the review. It is still saying the review exists in the reviews_count and the rating_summary. Any ideas would be greatly appreciated.

  4. Hi, Sheldon, vivek.
    In this example I edited the checkout template file.
    app/design/frontend/base/default/template/checkout/cart/item/default.phtml

    @vivek By the sound of it, what you need are reviews, i.e. Magento_Review_Model_Review model, which is not a subject of this article. Hope this helps you in your search 🙂

  5. Hi,
    Thanks for the code.

    I also want to show the rating title and the comment add by customer.
    Please help

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.