Add custom fields in review form

2019-02-15 22:57发布

问题:

I am looking forward to create a custom fields 'Email Id' & One drop-down in Review form .

I have tried this one but not saving the data, its hows the fields only

app\code\core\Mage\Review\Model\Mysql4\Review.php

protected function _afterSave(Mage_Core_Model_Abstract $object)
{
$detail = array(
'title' => $object->getTitle(),
'detail' => $object->getDetail(),
'nickname' => $object->getNickname(),
'email' => $object->getEmail(), // New field 1
'fname' => $object->getFname(), // New field 2
);

Now add email,fname in the review_detail table in the database also go to app\code\core\Mage\Adminhtml\Block\Review\Edit\Form.php also add :

$fieldset->addField('fname', 'text', array( // New field 2
'label' => Mage::helper('review')->__('First Name'),
'required' => true,
'name' => 'fname'
));

$fieldset->addField('email', 'text', array( // New field 1
'label' => Mage::helper('review')->__('Email'),
'required' => true,
'name' => 'email'
));

before to

$fieldset->addField('nickname', 'text', array(
'label' => Mage::helper('review')->__('Nickname'),
'required' => true,
'name' => 'nickname'
));

回答1:

Modify the Mage core class is a bit scary, which will be difficult to upgrade magento core class in the future. You can override the specific class by your own custom module (See module creator if you want to setup one)

Module's config.xml, add the models rewrite in as below:

<global>
    <models>
        <review_mysql4>
            <rewrite>
                <review>[[Your Company]]_[[Your Module]]_Model_Review</review>
            </rewrite>
        </review_mysql4>
    </models>
    ...
</global>

And the specified class will extend from the Magento core class you want to override:

class [[Your Company]]_[[Your Module]]_Model_Review
    extends Mage_Review_Model_Mysql4_Review
{
    protected function _afterSave(Mage_Core_Model_Abstract $object)
    {
     .... 
    }
}

Ps. to add the new field in magento review_detail table:

$installer = $this;
$installer->startSetup();
$installer->run("ALTER TABLE review_detail ADD COLUMN email VARCHAR(255) NULL");
$installer->endSetup();


回答2:

Finally i have solved it... Open app\code\core\Mage\Review\Model\Resource\Review.php

you will find this code in line about 150

protected function _afterSave(Mage_Core_Model_Abstract $object)
{
$detail = array(
'title' => $object->getTitle(),
'detail' => $object->getDetail(),
'nickname' => $object->getNickname(),
);

Add the new two fields you want to add.

protected function _afterSave(Mage_Core_Model_Abstract $object)
{
$detail = array(
'title' => $object->getTitle(),
'detail' => $object->getDetail(),
'nickname' => $object->getNickname(),
'email' => $object->getEmail(), // New field 1
'fname' => $object->getFname(), // New field 2
);

Thats it no more.... :) Happy coding