Displaying Reviews and Review Form in Tabs on Product Page
When you’re working with Magento, displaying both reviews and a review form within tabs on a product page is a common requirement for e-commerce sites. This allows customers to easily submit their thoughts while also browsing existing reviews.
Understanding the Default Setup
By default, Magento separates the reviews tab from the review submission form. The catalog.xml file includes two separate actions:
<action method="addTab" translate="title" module="catalog">
<alias>review</alias>
<title>Review</title>
<block>review/product_view_list</block>
<template>review/product/view/list.phtml</template>
</action>
<!-- For getting the review datails -->
<action method="addTab" translate="title" module="catalog">
<alias>review</alias>
<title>Review</title>
<block>review/form</block>
<template>review/form.phtml</template>
</action>
<!-- For displaying review form -->
The first action adds a tab with the alias “review” that displays existing reviews using product_view_list block and the list.phtml template. The second action adds another tab with the same alias, which displays the review submission form.
Combining Reviews and Review Form in One Tab
While this default setup is useful for separating concerns, it’s often more user-friendly to display both reviews and the review form within the same “Review” tab. To achieve this, you can modify your catalog.xml file as follows:
<action method="addTab" translate="title" module="catalog">
<alias>review</alias>
<title>Review</title>
<block>review/product_view_list</block>
<template>review/product/view/list.phtml</template>
<dependent_action>review/form</dependent_action>
</action>
<!-- Now both reviews and form are in the same tab -->
The key change is adding a dependent_action attribute with the value of “review/form”. This tells Magento that the review submission form should be added to the same tab as the existing reviews.
Implementing the Review Form
To ensure the review form displays correctly within the tab, you need to modify the form.phtml template located at app/design/frontend/<your_package>/<your_theme>/template/review/form.phtml.
<?php if($this->getReviewsCount() > 0): ?>
<div class="reviews">
<?php echo $this->getChildHtml('review_list') ?>
</div>
<?php endif; ?>
<?php if ($this->getAllowSubmit()): ?>
<form action="<?php echo $this->getFormActionUrl(); ?>" id="product-review-form" method="post">
<!-- Add your review fields here -->
<button type="submit" title="<?= /* @escapeNotVerified */ __('Save Review') ?>" id="review-form-submit-button" name="save_review" class="action primary"><?= /* @escapeNotVerified */ __('Save Review') ?></button>
</form>
<?php endif; ?>
In this modified template, we first check if there are any existing reviews ($this->getReviewsCount() > 0). If so, it displays the list of reviews using getChildHtml('review_list').
Then, it checks if submissions are allowed with $this->getAllowSubmit(). If true, it renders the review submission form.
Conclusion
Displaying both reviews and the review form within a single tab on your product page is a straightforward process that involves modifying the catalog.xml file and updating the form.phtml template. This approach provides a more seamless user experience for customers submitting reviews while browsing existing ones.
For more detailed information on customizing Magento’s review system, I recommend checking out our comprehensive guide at https://laravelcompany.com/magento-development/reviews-system-customization. It covers advanced topics like adding ratings to your reviews and implementing a rating system.