Magento - Overwrite core 'Review' module to add custom 'Email' field

ivn 11/19/2018 at 13:19. 1 answers, 0 views
php zend-framework magento

I need to create a custom module which over writes the existing 'Review' Module to add an extra field 'Email Id'

I believe I need to over write core Model files 'app/code/core/Mage/Review/Model/Resource/Review.php' ,'app/code/core/Mage/Review/Model/Resource/Review/Product/Collection.php' and Block file 'app/code/core/Mage/Adminhtml/Block/Review/Edit/Form.php'

Also I don't know what setup script and with what version should I include to upgrade the existing 'review_detail' table with this extra field.

This is what I have done so far code wise and it doesn't work. For now I have manually added 'email' field in review_detail table and added email input field in base/default/template/review/form.phtml

CM
 Review
   Block
     Review
       Edit
        Form.php
   etc
     config.xml
   Helper
     Data.php
   Model
     Resource
       Review.php
       Review
          Product
             Collection.php

This is my config.xml file

<?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>

I have copy pasted the content of original Block and Model files with addition of 'email' field. But still can't get to save any data. However directly editing the original files does the trick but not through my custom module.

Is there anything obvious that I am missing out on? Any pointers for help will do. Thanks.


I am getting no error but also the field is neither getting saved to the database nor its getting added through install script code. Hence I commented the resources section from my code and added the field manually. This is my code,

config.xml

<?xml version="1.0"?>
<config>
<modules>
  <CM_Reviewmail>
    <version>0.0.1</version>
  </CM_Reviewmail>
</modules>

<frontend>
  <routers>
    <reviewmail>
       <use>standard</use>
       <args>
          <module>CM_Reviewmail</module>
          <frontName>cmreviewmail</frontName>
       </args>
    </reviewmail>
  </routers>
</frontend>     
<global>

 <blocks>
   <adminhtml>
      <rewrite>
        <review_edit_form>CM_Reviewmail_Block_Review_Edit_Form</review_edit_form>
      </rewrite>
  </adminhtml>
</blocks>

<helpers>
  <reviewmail>
     <class>CM_Reviewmail_Helper</class>
   </reviewmail>
</helpers> 

<models>
  <reviewmail>
     <rewrite>
        <resource_review>CM_Reviewmail_Model_Resource_Review</resource_review>
     </rewrite>
  </reviewmail>
</models>

<!-- <resources>
    <reviewmail_setup>
        <setup>
            <module>CM_Reviewmail</module>
            <class>Mage_Sales_Model_Mysql4_Setup</class>
        </setup>
        <connection>
            <use>core_setup</use>
        </connection>
    </reviewmail_setup>
    <reviewmail_setup_write>
        <connection>
            <use>core_write</use>
        </connection>
    </reviewmail_setup_write>
    <reviewmail_setup_read>
        <connection>
            <use>core_read</use>
        </connection>
    </reviewmail_setup_read>
</resources>-->

</global>
</config>

This is my Reviewmail/Block/Review/Edit/Form.php code

 <?php
class CM_Reviewmail_Block_Review_Edit_Form extends Mage_Adminhtml_Block_Review_Edit_Form
{
 protected function _prepareForm()
 {
    $review = Mage::registry('review_data');
    $product = Mage::getModel('catalog/product')->load($review->getEntityPkValue());
    $customer = Mage::getModel('customer/customer')->load($review->getCustomerId());
    $statuses = Mage::getModel('review/review')
        ->getStatusCollection()
        ->load()
        ->toOptionArray();

    $form = new Varien_Data_Form(array(
        'id'        => 'edit_form',
        'action'    => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'), 'ret' => Mage::registry('ret'))),
        'method'    => 'post'
    ));

    $fieldset = $form->addFieldset('review_details', array('legend' => Mage::helper('review')->__('Review Details'), 'class' => 'fieldset-wide'));

    $fieldset->addField('product_name', 'note', array(
        'label'     => Mage::helper('review')->__('Product'),
        'text'      => '<a href="' . $this->getUrl('*/catalog_product/edit', array('id' => $product->getId())) . '" onclick="this.target=\'blank\'">' . $product->getName() . '</a>'
    ));

    if ($customer->getId()) {
        $customerText = Mage::helper('review')->__('<a href="%1$s" onclick="this.target=\'blank\'">%2$s %3$s</a> <a href="mailto:%4$s">(%4$s)</a>',
            $this->getUrl('*/customer/edit', array('id' => $customer->getId(), 'active_tab'=>'review')),
            $this->htmlEscape($customer->getFirstname()),
            $this->htmlEscape($customer->getLastname()),
            $this->htmlEscape($customer->getEmail()));
    } else {
        if (is_null($review->getCustomerId())) {
            $customerText = Mage::helper('review')->__('Guest');
        } elseif ($review->getCustomerId() == 0) {
            $customerText = Mage::helper('review')->__('Administrator');
        }
    }

    $fieldset->addField('customer', 'note', array(
        'label'     => Mage::helper('review')->__('Posted By'),
        'text'      => $customerText,
    ));

    $fieldset->addField('summary_rating', 'note', array(
        'label'     => Mage::helper('review')->__('Summary Rating'),
        'text'      => $this->getLayout()->createBlock('adminhtml/review_rating_summary')->toHtml(),
    ));

    $fieldset->addField('detailed_rating', 'note', array(
        'label'     => Mage::helper('review')->__('Detailed Rating'),
        'required'  => true,
        'text'      => '<div id="rating_detail">' . $this->getLayout()->createBlock('adminhtml/review_rating_detailed')->toHtml() . '</div>',
    ));

    $fieldset->addField('status_id', 'select', array(
        'label'     => Mage::helper('review')->__('Status'),
        'required'  => true,
        'name'      => 'status_id',
        'values'    => Mage::helper('review')->translateArray($statuses),
    ));

    /**
     * Check is single store mode
     */
    if (!Mage::app()->isSingleStoreMode()) {
        $fieldset->addField('select_stores', 'multiselect', array(
            'label'     => Mage::helper('review')->__('Visible In'),
            'required'  => true,
            'name'      => 'stores[]',
            'values'    => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm()
        ));
        $review->setSelectStores($review->getStores());
    }
    else {
        $fieldset->addField('select_stores', 'hidden', array(
            'name'      => 'stores[]',
            'value'     => Mage::app()->getStore(true)->getId()
        ));
        $review->setSelectStores(Mage::app()->getStore(true)->getId());
    }

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

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

    $fieldset->addField('title', 'text', array(
        'label'     => Mage::helper('review')->__('Summary of Review'),
        'required'  => true,
        'name'      => 'title',
    ));

    $fieldset->addField('detail', 'textarea', array(
        'label'     => Mage::helper('review')->__('Review'),
        'required'  => true,
        'name'      => 'detail',
        'style'     => 'height:24em;',
    ));

    $form->setUseContainer(true);
    $form->setValues($review->getData());
    $this->setForm($form);
    return parent::_prepareForm();
   }    
 }

This is my Reviewmail/Model/Resources/Review.php code

   <?php 
   class CM_Reviewmail_Model_Resource_Review extends Mage_Review_Model_Resource_Review
   {
protected function _afterSave(Mage_Core_Model_Abstract $object)
    {
     $adapter = $this->_getWriteAdapter();
     /**
     * save detail
     */
     $detail = array(
        'title'     => $object->getTitle(),
        'detail'    => $object->getDetail(),
        'nickname'  => $object->getNickname(),
        'email'     => $object->getEmail(),
    );
    $select = $adapter->select()
        ->from($this->_reviewDetailTable, 'detail_id')
        ->where('review_id = :review_id');
    $detailId = $adapter->fetchOne($select, array(':review_id' => $object->getId()));

    if ($detailId) {
        $condition = array("detail_id = ?" => $detailId);
        $adapter->update($this->_reviewDetailTable, $detail, $condition);
    } else {
        $detail['store_id']   = $object->getStoreId();
        $detail['customer_id']= $object->getCustomerId();
        $detail['review_id']  = $object->getId();
        $adapter->insert($this->_reviewDetailTable, $detail);
    }


    /**
     * save stores
     */
    $stores = $object->getStores();
    if (!empty($stores)) {
        $condition = array('review_id = ?' => $object->getId());
        $adapter->delete($this->_reviewStoreTable, $condition);

        $insertedStoreIds = array();
        foreach ($stores as $storeId) {
            if (in_array($storeId, $insertedStoreIds)) {
                continue;
            }

            $insertedStoreIds[] = $storeId;
            $storeInsert = array(
                'store_id' => $storeId,
                'review_id'=> $object->getId()
            );
            $adapter->insert($this->_reviewStoreTable, $storeInsert);
        }
    }

    // reaggregate ratings, that depend on this review
    $this->_aggregateRatings(
        $this->_loadVotedRatingIds($object->getId()),
        $object->getEntityPkValue()
    );

    return $this;
  } 
}

There is a Helper/Data.php

<?php
 class CM_Reviewmail_Helper_Data extends Mage_Core_Helper_Abstract {}

This is Model/Resource/Mysql4/Setup.php

<?php
class CM_Reviewmail_Model_Resource_Mysl4_Setup extends Mage_Core_Model_Resource_Setup {}

Can any one point out what have i done wrong? or what more needs to be done?

Please any help is appreciated. I have been banging my head against the wall and need to sort this problem asap. Thanks in advance.

1 Answers


Magento Guy 01/19/2012.

Your method for rewriting the class looks mostly right, but remember that you are rewriting adminhtml/review_edit_form to point to CM_Review_Block_Review_Edit_Form, so in app/code/local/CM/Review/Block/Review/Edit/Form.php you need to be declaring the class as such:

<?php
    class CM_Review_Block_Review_Edit_Form extends Mage_Adminhtml_Block_Review_Edit_Form
    {

And then only override the functions that you need to.

Don't know if you are doing that already. The same process applies for any models that you rewrite. You shouldn't really need to rewrite the collection for this either, but that's up to you depending on what you need to do with it.

As for the install stuff:

in app/code/local/CM/Review/etc/config.xml

<config>
    ...
    <resources>
        <review_setup>
            <setup>
                <module>CM_Review</module>
                <class>Mage_Sales_Model_Mysql4_Setup</class>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </review_setup>
    </resources>
    ...
</config>

In app/code/local/CM/Review/sql/review_setup/mysql4-install-0.0.1.php

<?php
    $installer = $this;
    $installer->startSetup();
    $installer->run("ALTER TABLE review_detail ADD COLUMN email_id INT NULL");
    $installer->endSetup();

If this doesn't take the first time (it should, unless you've tried something similar already), go into the database, and remove the "review_setup" row from the table "core_resource". Then try again. After adding columns, always remember to flush the magento cache in System > Cache Management

Don't forget that you also need to create a file called CM_Review.xml in app/etc/modules. It should look like this:

<config>
    <modules>
        <CM_Review>
            <active>true</active>
            <codePool>local</codePool>
        </CM_Review>
    </modules>
</config>

Hope this helps.


HighResolutionMusic.com - Download Hi-Res Songs

1 The Chainsmokers

Beach House flac

The Chainsmokers. 2018. Writer: Andrew Taggart.
2 (G)I-DLE

POP/STARS flac

(G)I-DLE. 2018. Writer: Riot Music Team;Harloe.
3 Anne-Marie

Rewrite The Stars flac

Anne-Marie. 2018. Writer: Benj Pasek;Justin Paul.
4 Ariana Grande

​Thank U, Next flac

Ariana Grande. 2018. Writer: Crazy Mike;Scootie;Victoria Monét;Tayla Parx;TBHits;Ariana Grande.
5 Diplo

Close To Me flac

Diplo. 2018. Writer: Ellie Goulding;Savan Kotecha;Peter Svensson;Ilya;Swae Lee;Diplo.
6 BTS

Waste It On Me flac

BTS. 2018. Writer: Steve Aoki;Jeff Halavacs;Ryan Ogren;Michael Gazzo;Nate Cyphert;Sean Foreman;RM.
7 Clean Bandit

Baby flac

Clean Bandit. 2018. Writer: Jack Patterson;Kamille;Jason Evigan;Matthew Knott;Marina;Luis Fonsi.
8 Imagine Dragons

Bad Liar flac

Imagine Dragons. 2018. Writer: Jorgen Odegard;Daniel Platzman;Ben McKee;Wayne Sermon;Aja Volkman;Dan Reynolds.
9 BlackPink

Kiss And Make Up flac

BlackPink. 2018. Writer: Soke;Kny Factory;Billboard;Chelcee Grimes;Teddy Park;Marc Vincent;Dua Lipa.
10 Nicki Minaj

No Candle No Light flac

Nicki Minaj. 2018. Writer: Denisia “Blu June” Andrews;Kathryn Ostenberg;Brittany "Chi" Coney;Brian Lee;TJ Routon;Tushar Apte;ZAYN;Nicki Minaj.
11 Rita Ora

Cashmere flac

Rita Ora. 2018. Writer: Sean Douglas;Lindy Robbins.
12 Backstreet Boys

Chances flac

Backstreet Boys. 2018.
13 Brooks

Limbo flac

Brooks. 2018.
14 Rita Ora

Velvet Rope flac

Rita Ora. 2018.
15 Fitz And The Tantrums

HandClap flac

Fitz And The Tantrums. 2017. Writer: Fitz And The Tantrums;Eric Frederic;Sam Hollander.
16 Little Mix

Woman Like Me flac

Little Mix. 2018. Writer: Nicki Minaj;Steve Mac;Ed Sheeran;Jess Glynne.
17 Cher Lloyd

None Of My Business flac

Cher Lloyd. 2018. Writer: ​iamBADDLUCK;Alexsej Vlasenko;Kate Morgan;Henrik Meinke;Jonas Kalisch;Jeremy Chacon.
18 Billie Eilish

When The Party's Over flac

Billie Eilish. 2018. Writer: Billie Eilish;FINNEAS.
19 Kelly Clarkson

Never Enough flac

Kelly Clarkson. 2018. Writer: Benj Pasek;Justin Paul.
20 Lil Pump

Arms Around You flac

Lil Pump. 2018. Writer: Rio Santana;Lil Pump;Edgar Barrera;Mally Mall;Jon Fx;Skrillex;Maluma;Swae Lee;XXXTENTACION.

Language

Popular Tags