Displaying Recent Product Reviews in Magento Sidebar
As an experienced developer, I understand that showcasing customer reviews is a powerful way to build trust and drive sales on your e-commerce platform. In this post, we’ll guide you through displaying the most recent product reviews directly in the sidebar of the corresponding product page using Magento.
Understanding the Magento Review System
Magento allows customers to submit reviews for products after purchasing them or as guests via the “Write Your Own Review” form. These reviews are stored in the review table and associated with the relevant product using the entity_id field.
Displaying Reviews in the Sidebar
To show recent reviews in the sidebar, we’ll create a new block, add it to the layout, and then modify the template file to display the desired information. Here’s a step-by-step guide:
-
Create a New Block
Open your terminal (or command prompt) and navigate to your Magento root directory. Then run the following commands to create a new block called
reviews_sidebar:php bin/magento module:enable --all php bin/magento setup:upgrade php bin/magento module:status Magento_Review -
Add the Block to Layout
Open your preferred code editor and navigate to
app/design/frontend/[YourTheme]/[YourPackage]/Magento_Theme/layout/default.xml. Add the following XML code inside the<body>tag:<block class="Magento\Review\Block\Product\View" name="reviews_sidebar" template="Magento_Review::product/view/list.phtml"> <arguments> <argument name="view_model" xsi:type="object">Magento\Review\Model\ResourceModel\Review\Collection</argument> </arguments> </block> -
Update the Sidebar Template
Now, let’s modify the sidebar template to include our new block.
Navigate to
app/design/frontend/[YourTheme]/[YourPackage]/template/html/sidebar.phtmland add the following PHP code inside the<aside class="col-main">tag:<?php $reviews = $block->getReviews(); ?> <div class="block block-reviews"> <h2 class="block-title"><?= __('Recent Reviews') ?></h2> <ul class="products-list no-margin"> <?php foreach ($reviews as $review): ?> <li> <?= Mage::helper('core')->escapeHtml($review->getNickname()) ?> - <?= $review->getCreatedAt() ?> <div class="ratings"> <div style="width:<?= intval($review->getRatingSummary() * 20) ?>%"></div> </div> <p><?= Mage::helper('core')->escapeHtml(substr($review->getText(), 0, 15)) ?>...</p> <a href="<?= $this->getUrl('review/product/list') ?>"><?= __('See all reviews') ?></a> </li> <?php endforeach; ?> </ul> </div> -
Compile and Cache Files
After making these changes, run the following commands to compile and cache your Magento files:
php bin/magento setup:static-content:deploy php bin/magento c:f -
Test Your Changes
Go to a product page on the frontend of your website, and you should now see the “Recent Reviews” block in the sidebar, displaying the desired information.
Conclusion
By following this guide, you’ve successfully added a new block to display recent product reviews directly in the sidebar of each product page using Magento. This not only improves user experience but also builds credibility for your products by showcasing customer feedback.
Remember that customizing Magento requires a good understanding of PHP, XML, and template files. It’s essential to always backup your code before making changes and to test thoroughly after implementation.
For more advanced customizations and professional support, consider reaching out to our team at Laravel Company. We specialize in Magento development and can help you optimize your e-commerce platform for better performance and user engagement.