Updating the Number of Reviews on Your Product Page in Magento
Hello fellow developers! Today we’re going to tackle a common issue: displaying the number of reviews for each product right on your Magento product pages.
The Current Challenge
You’re seeing an empty Reviews tab titled “Productâs Reviews”, but you want it to display as “Reviews(0)” or any other number that reflects the actual review count. You tried using PHP code like this:
<?php echo $this->__('%d Review(s)', $this->getReviewsCount()) ?>
But alas, it didn’t work.
The Root Cause
The issue lies in how Magento handles reviews out of the box. By default, it doesn’t connect product reviews to specific products directly via a relationship model. This leads to the $this->getReviewsCount() method simply returning zero for each individual product page.
Our Solution Approach
To resolve this, we’ll need to create an association between the review and the product using a foreign key. We’ll modify the database schema and update the Magento codebase accordingly.
Step-by-Step Guide
1. Modify the Database Schema
First, let’s add a new column to the review_entity table that links each review to its corresponding product:
ALTER TABLE `catalog_product_entity_review`
ADD COLUMN `product_id` INT(10) UNSIGNED NULL DEFAULT NULL;
This adds a new field called product_id, which will store the ID of the associated product.
2. Update the Review Table
Next, we need to populate this new field with actual data:
UPDATE catalog_product_entity_review AS r
JOIN review AS rv ON rv.entity_pk_value = r.review_id
SET r.product_id = (SELECT e.entity_id FROM catalog_product_entity AS e WHERE e.sku = rv.entity_code);
This SQL command joins the review and catalog_product_entity tables on the product SKU, then populates the new product_id field in the catalog_product_entity_review table.
3. Update Magento’s Core Files
Now we need to modify some core files to ensure that reviews are properly associated with products:
app/code/core/Mage/Review/etc/config.xml
Add this line inside the <global> section:
<config>
<modules>
<Mage_Review>
<version>1.0</version>
</Mage_Review>
</modules>
<!-- Add the following line -->
<product_review>
<reference name="product.review_form">
<action method="setAttributeSetIdForProduct" />
</reference>
</product_review>
</config>
app/code/core/Mage/Review/controllers/ProductController.php
Add this function:
public function setAttributeSetIdForProduct()
{
$product = Mage::registry('product');
if ($product) {
Mage::getSingleton('review/session')->setProductId($product->getId());
}
}
4. Update the Review Count Function
Finally, let’s update the review count function to reflect the actual number of reviews for each product:
app/code/core/Mage/Review/Model/Mysql4/Review.php
Replace this method with the following code:
public function getProductReviews($productId)
{
$collection = $this->_getResource()->getCollection()
->addFieldToFilter('status_id', 1)
->addFieldToFilter('product_id', $productId);
return $collection;
}
Conclusion
By following these steps, you should now be able to display the number of reviews for each product on your Magento product pages. This customization requires modifying the database schema and updating a few core files.
Remember, always backup your files before making any changes in your production environment!
If you’re looking for more advanced techniques or have any questions about this process, feel free to reach out at [https://laravelcompany.com]. We’d be happy to help you optimize your e-commerce platform further!