Magento - Add a Custom Field in Review Form for Guest Customers
Adding a custom field like an ‘Email Id’ to the review form in Magento is a common requirement for many e-commerce stores. In this guide, we will walk you through the process of creating a new module that adds this field specifically for guest customers while keeping it optional for registered users.
1. Understanding the Core Files
Before we begin, let’s understand which core files need to be rewritten or extended:
/code/core/Mage/Review/Model/Mysql4/Review/Product/Collection.php: This file is responsible for fetching review data from the database./code/core/Mage/Review/Model/Mysql4/Review.php: This model handles basic CRUD operations on thereviewtable.app/code/core/Mage/Review/Block/Review/Edit/Form.php: The form block that renders the review edit form.
2. Creating a New Module
First, create a new module structure in your local Magento installation:
app/code/local/CM/Review/
This will include the following subfolders:
CM
Review
Block
Review
Edit
Form.php
etc
config.xml
Helper
Data.php
Model
Resource
Review.php
Review
Product
Collection.php
The config.xml file is the configuration file for your module. Let’s break down its contents:
<?xml version="1.0"?>
<config>
<modules>
<CM_Review>
<version>0.0.1</version>
</CM_Review>
</modules>
<frontend>
<routers>
<review>
<use>standard</use>
<args>
<module>CM_Review</module>
<frontName>Review</frontName>
</args>
</review>
</routers>
</frontend>
<global>
<blocks>
<adminhtml>
<rewrite>
<review_edit_form>CM_Review_Block_Review_Edit_Form</review_edit_form>
</rewrite>
</adminhtml>
</blocks>
<helpers>
<review>
<rewrite>
<class>CM_Review_Helper</class>
</rewrite>
</review>
</helpers>
<models>
<review>
<rewrite>
<resource_review>CM_Review_Model_Resource_Review</resource_review>
<resource_review_product_collection>
CM_Review_Model_Resource_Review_Product_Collection
</resource_review_product_collection>
</rewrite>
</review>
</models>
</global>
</config>
3. Adding the Custom Email Field
3.1 Database Schema Change
To add a new email field to the review table, you can use an SQL script like this:
ALTER TABLE `review` ADD `email` VARCHAR(255) NULL COMMENT 'Customer email';
Make sure to backup your database before running any schema changes.
3.2 Extending the Review Form Block
Open the file app/code/local/CM/Review/Block/Review/Edit/Form.php. This is where you will add the custom field:
class CM_Review_Block_Review_Edit_Form extends Mage_Core_Block_Template
{
public function __construct()
{
parent::__construct();
$this->setTemplate('review/form.phtml');
}
public function getReviewFormHtml()
{
$form = new Varien_Data_Form();
$form->setAction($this->getUrl('*/*/post'));
$form->setId('review_form');
$form->setName('review_form');
$form->setMethod('POST');
$form->setUseContainer(true);
// Add the custom email field
$email = $form->addField(
'email',
'text',
[
'name' => 'email',
'label' => Mage::helper('review')->__('Your Email'),
'required' => ($this->getCustomerType() == Mage_Customer_Model_Group::NOT_LOGGED_IN ? true : false),
'class' => 'input-text validate-email'
]
);
// Add the rest of your form fields here...
return $form->toHtml();
}
public function getCustomerType()
{
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
return Mage_Customer_Model_Group::REGISTERED;
} else {
return Mage_Customer_Model_Group::NOT_LOGGED_IN;
}
}
}
This code extends the core Form.php block and adds a new text field named ‘email’. The required attribute is set based on whether the customer is logged in or not.
3.3 Updating the Review Form.phtml File
Next, open the file app/design/frontend/default/default/template/review/form.phtml. Add your custom email field after the existing fields:
<div class="field">
<label for="email" class="required"><em>*</em><?php echo $this->__('Your Email') ?></label>
<div class="input-box">
<?php echo $this->getFieldHtml('email'); ?>
</div>
</div>
The <?php echo $this->getFieldHtml('email'); ?> line will render the email field you added in the Form.php file.
4. Extending Core Models
4.1 Review Model Resource
Open the file app/code/local/CM/Review/Model/Resource/Review.php. This is where we’ll handle saving the additional data:
class CM_Review_Model_Resource_Review extends Mage_Core_Model_Mysql4_Abstract
{
protected function _construct()
{
$this->_init('review/review', 'review_id');
}
protected function _beforeSave(Mage_Core_Model_Abstract $object)
{
if (!$this->getUseIndexer()) {
$this->_saveReviewAttributes($object);
}
return parent::_beforeSave($object);
}
private function _saveReviewAttributes($review)
{
// Save the email attribute
$email = $review->getEmail();
if ($email) {
$review->setData('email', trim($email));
} else {
$review->setData('email', null);
}
}
}
The _beforeSave method is called before each save operation. We are using it to save the email attribute from the review object.
4.2 Review Product Collection
Open the file app/code/local/CM/Review/Model/Resource/Review/Product/Collection.php. This model handles fetching reviews for a specific product:
class CM_Review_Model_Resource_Review_Product_Collection extends Mage_Core_Model_Mysql4_Abstract
{
protected function _construct()
{
$this->_init('review/review', 'review_id');
$this->setItemObjectClass('model');
// Set the main table name and columns to select from
$this->_map['fields']['main_table'] = '`review`';
$this->_map['fields']['main_table'][] = '`review_id`';
$this->_map['fields']['main_table'][] = '`review_id` AS `id`';
// Add the new email field to the select statement
$this->_map['fields']['main_table'][] = '`email`';
// Set other tables and fields...
}
protected function _renderFiltersBefore()
{
parent::_renderFiltersBefore();
if ($this->_filterAvailable) {
foreach ($this->getSelect()->getPart(Zend_Db_Select::FROM) as $v) {
if (isset($v['main_table'])) {
// Add the new email field to the select statement
$this->addFilterToCollection($v, 'email');
}
}
}
return $this;
}
}
In the _construct method, we are adding the email field to the main table definition and selecting it. In the _renderFiltersBefore method, we add a filter for the email field.
5. Conclusion
Adding a custom field like an ‘Email Id’ to the review form in Magento is a straightforward process once you understand how the core files work. By creating a new module and extending the appropriate blocks and models, you can easily add this functionality without touching any core files directly.
Remember to clear your cache after making these changes for them to take effect. If you’re still having issues, double-check that you’ve followed all the steps correctly and that there are no typos in your code.
For more advanced customizations or if you need help with other Magento development tasks, feel free to reach out to our team at Laravel Company. We specialize in providing high-quality Magento development services tailored to meet the unique needs of your business.