Wednesday, June 6, 2012

Create review like module in magento


I have created a module by which we can marked product review helpful or not
Create a table
CREATE TABLE IF NOT EXISTS `review_like` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `product_id` int(11) NOT NULL,
  `review_id` int(11) NOT NULL,
  `customer_id` int(11) NOT NULL,
  `like` int(11) NOT NULL,
  `notlike` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `product_id` (`product_id`,`review_id`,`customer_id`)
)

Modify review_detail  add two field like, notlike
Add the below code in the app\code\core\Mage\Review\etc\config.xml
<review_like>
    <table>review_like</table>
 </review_like>

Add the following code in your review page after the review description
app\design\frontend\base\default\template\review\product\view\list.phtml

<?php
      $like_url = $this->getUrl('review/product/list').'id/'.$_review->getEntityPkValue().'/rev/'.$_review->getReviewId().'/lik/';                                                                     
 ?>
               <p class="rev_h"> Was this review helpful?
               <?php if(!Mage::getSingleton('customer/session')->isLoggedIn()):?> <a href="<?php echo $this->getSkinUrl(''); ?>customer/account/login/"><?php else: ?>             
               <a href="<?php echo $like_url; ?>1"><?php endif?>Yes</a>(<?php echo $this->htmlEscape($_review->getLike()) ?>) /
               <?php if(!Mage::getSingleton('customer/session')->isLoggedIn()):?> <a href="<?php echo $this->getSkinUrl(''); ?>customer/account/login/"><?php else: ?>
               <a href="<?php echo $like_url; ?>2"><?php endif?>No</a>(<?php echo $this->htmlEscape($_review->getNotlike()) ?>)  </p>    
After that go to app\code\core\Mage\Review\controllers\ProductController.php
Add   
$customer_id = Mage::getSingleton('customer/session')->getCustomerId();
                                                $rev_id = Mage::app()->getRequest()->getParam('rev');                                                             
                                                $lik = Mage::app()->getRequest()->getParam('lik');                                        
                                                if($lik>0){                                                                            
                                                                Mage::getModel('review/resource_review')->saveLikereview($product->getId(),$rev_id,$customer_id,$lik);     
                                                }

After
public function listAction()    {                    
                                if ($product = $this->_initProduct()) {
            Mage::register('productId', $product->getId());
Now open  app\code\core\Mage\Review\Model\Resource\Review.php  and past this
public function saveLikereview($product_id,$rev_id,$customer_id,$lik)
                {
                                $adapter = $this->_getWriteAdapter();
                                if($lik==1){ $like=1; $notlike=0; }
                                elseif($lik==2){ $like=0; $notlike=1; }
                                $data = array(
                                                    'product_id'  =>  $product_id,
                                                     'review_id'        =>  $rev_id,
                                                     'customer_id'  =>           $customer_id,
                                                   'like'                     =>           $like,
                                                    'notlike'                              =>           $notlike
                                      );
                                try{
                                                $res = $adapter->insert($this->_reviewLikeTable, $data);
                                                if($res)
                                                {
                                                                $adapter1 = $this->_getReadAdapter();
                                                                $select = $adapter->select()
                                                                                ->from($this->_reviewDetailTable, array('like','notlike'))
                                                                                ->where('review_id = :review_id');
                                                                $no_rev = $adapter1->fetchAll($select, array(':review_id' => $rev_id));
                                                               
                                                                                $condition = "review_id =".$rev_id;
                                                                                $data = array(                                                                                                   
                                                                                                                  'like'                     =>           ($like + $no_rev[0]['like']),
                                                                                                                  'notlike'                              =>           ($notlike + $no_rev[0]['notlike'])
                                                                                                                  );
                    $adapter->update($this->_reviewDetailTable, $data, $condition);      
                                                }
                                }
                                catch(Exception $e){      }             
Also past  $this->_reviewLikeTable   = $this->getTable('review/review_like');  under protected function _construct()
Now open app\code\core\Mage\Review\Model\Resource\Review\Collection.php
And add $this->_reviewLikeTable     = $this->getTable('review/review_like'); under  protected function _construct()
And change array('detail_id', 'title', 'detail', 'nickname', 'customer_id')); in to
array('detail_id', 'title', 'detail', 'nickname', 'customer_id','like','notlike'));             


5 comments:

  1. article good!
    hey you? you give me, a solution, now i have created new module to admin product designed. but i save that products designed in a new table. I also want to review ,rating, tag on that product, I will customize in review, rating, tag module of magento, or i will create new same same with review, rating, tag module of magento?..

    ReplyDelete
  2. I am getting error while performing following chagnes.

    "There has been an error processing your request
    Exception printing is disabled by default for security reasons.
    Error log record number: 1074376077"

    Changes are following.
    "Also past $this->_reviewLikeTable = $this->getTable('review/review_like'); under protected function _construct()
    Now open app\code\core\Mage\Review\Model\Resource\Review\Collection.php
    And add $this->_reviewLikeTable = $this->getTable('review/review_like'); under protected function _construct()
    And change array('detail_id', 'title', 'detail', 'nickname', 'customer_id')); in to
    array('detail_id', 'title', 'detail', 'nickname', 'customer_id','like','notlike')); "

    Thanks

    ReplyDelete
    Replies
    1. Yes.I also get a error for the like ,notlike column.The column is not present in the details table.Thats why you need to join .Code given below -
      ->joinLeft(array('like' => $this->_reviewLikeTable),
      'main_table.review_id = like.review_id',
      //array('detail_id', 'title', 'detail', 'nickname', 'customer_id'));
      array('like','notlike')) ;

      This code should be written in the _initSelect().

      Delete
  3. Great posting about Create review like module in magento ..........


    best magento hosting uk

    ReplyDelete