Magento: Display Individual Rating Summaries
Handling individual rating summaries in Magento can be challenging since the platform is built around displaying aggregate ratings by default. In this post, we’ll walk through a practical solution to show summary votes for each specific rating attribute on your product page.
Understanding Your Scenario
You’ve set up five review attributes: Value, Looks, Usability, Quality, and Portability. For each product, you want to display the average percentage score for these individual attributes based on all reviews submitted. Currently, Magento only provides an overall summary of all ratings combined.
The Approach: Customizing Reviews Block
To achieve this, we’ll need to modify the core reviews block that handles product reviews and ratings. Here’s a step-by-step guide:
- Create a new module
First, create a custom module (e.g., YourCompany_ReviewSummaries) following standard Magento setup.
- Override the reviews block
In your module’s etc/di.xml file, use the <preference> tag to override the core reviews block:
<preference for="Magento\Review\Block\Product\View" type="YourCompany\ReviewSummaries\Block\Product\View"/>
- Create the custom View block
In your module’s etc/frontend/di.xml, add a block definition for the new View block:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Review\Block\Product\View">
<arguments>
<argument name="template" xsi:type="string">YourCompany_ReviewSummaries::product/view.phtml</argument>
</arguments>
</type>
</config>
- Modify the view template
Create a new view.phtml file in your module’s theme directory (e.g., app/design/frontend/Vendor/theme/Magento_Theme/templates/magento_reviews/product/view.phtml).
In this template, use Magento’s data object methods to retrieve and display the individual rating summaries:
<?php foreach ($block->getReviewsCollection() as $_review): ?>
<?php if ($_review->canDisplayRatingSummary() && count($_review->getRatings()) > 0): ?>
<div class="rating-summary">
<?php
$summary = [];
foreach ($_review->getRatings() as $_ratingCode => $_ratingData) {
$average = number_format($_ratingData['summary']['ratings_sum'] / $_ratingData['summary']['count'], 2);
$summary[$_ratingCode] = $average;
}
?>
<?php foreach ($summary as $_code => $_avg): ?>
<div class="rating-summary-item">
<strong><?php echo $block->escapeHtml($_code) ?>:</strong> <?php echo $_avg ?>%
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php endforeach; ?>
Conclusion
This approach allows you to display individual rating summaries for each review attribute on your product page. By overriding the core reviews block and modifying the template, we can extract and format the required data from Magento’s internal reviews collection.
Remember to clear your Magento cache after implementing these changes to ensure they take effect. If you’re looking for professional support or custom development services, visit Laravel Company for expert assistance with your eCommerce projects.