Adding an Additional Text Field to Magento Product Reviews
Introduction
In this guide, we’ll walk you through adding an additional text field to your Magento product reviews. This is a common requirement for e-commerce sites that need to capture more detailed customer feedback. We’ll cover the steps required from a developer’s perspective.
Identifying the Database and File Structure Changes Needed
The first step is to identify where this new field should live in the database schema. The review_detail table seems like a logical choice for storing additional review data:
CREATE TABLE `review_detail` (
`review_id` int(10) NOT NULL,
`data` text DEFAULT NULL,
PRIMARY KEY (`review_id`)
);
To add our new field, we would need to alter this table and create a new column. Let’s assume the name of the new field is “additional_comments”.
ALTER TABLE review_detail ADD COLUMN additional_comments TEXT;
Modifying Magento Core Files
Next, we’ll modify some core Magento files to integrate this new field into the review system.
Step 1: Update Review Form Template File
First, you need to update the review form template file located at app/design/frontend/{package}/{theme}/template/review/form.phtml. Add your custom field after the existing text area:
<div class="field-review-attachments">
<div class="box-columns">
<?php echo $this->getAdditionalFieldHtml() ?>
</div>
</div>
Step 2: Update Review Model
In app/code/core/Mage/Review/Model/Resource/Detail.php, add the following code to save your new field:
public function save(Mage_Core_Model_Abstract $object)
{
$value = $this->getAttributeValue($object, 'additional_comments');
if ($value) {
$this->_connection->insert($this->getMainTable(), array('review_id' => $object->getId(), 'additional_comments' => $value));
}
}
Step 3: Update Review Controller
Finally, in app/code/core/Mage/Review/controllers/IndexController.php, update the saveAction method to include your new field:
$model = Mage::getModel('review/review_detail');
$data = array();
$data['additional_comments'] = $this->getRequest()->getParam('additional_comments');
$model->setData($data);
try {
$model->save();
} catch (Exception $e) {
echo "Error saving review: " . $e->getMessage();
}
Conclusion
Adding an additional text field to Magento product reviews involves modifying the database schema, core template files, and the Review model. It’s important to ensure you have a good understanding of Magento’s codebase before making these changes.
Remember that altering core files can lead to issues when upgrading your Magento version in the future. Consider using a custom module or overriding the core files to maintain separation and make updates easier.
For more detailed information on creating custom Magento modules, I recommend checking out this guide from our team at Laravel Company: How to Create a Custom Magento Module.
This demonstrates how to add new functionality without directly modifying the core files, which is a best practice for maintaining upgrade compatibility.