Display product’s rating beyond the review page – Magento
Sometimes there is a need for showing star ratings outside the products 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.
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; ?>
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.