Surya prakash Patel
Surya prakash Patel

Reputation: 1522

Set product reviews auto approve in magento

When any customer submit review on a product, review must be automatically approved. No need to approved by admin.

Upvotes: 3

Views: 3574

Answers (2)

Roman Snitko
Roman Snitko

Reputation: 3655

You can try this approach.

UPDATE: the link is no longer valid, so I posted the one from web archive.

Upvotes: 3

Russell Albin
Russell Albin

Reputation: 51

Create a new module is the best approach, its simple and easy to do. Step 1: create a module declaration file in app/etc/modules called Dpc_Review.xml

<?xml version="1.0"?>
    <config>
    <modules>
        <Dpc_Review>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Review/>
            </depends>
        </Dpc_Review>
    </modules>
</config>

Step 2: In app/etc/local create a folder called Dpc Step 3: Inside app/etc/local/Dpc/ create a new folder Review Step 3: inside app/etc/local/Dpc/Review create 3 folders controllers, etc and Helper Step 4: inside app/etc/local/Dpc/Review/etc/ create a file called config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Dpc_Review>
            <version>0.0.1</version>
        </Dpc_Review>
    </modules>
    <frontend>
        <routers>
            <review>
                <args>
                    <modules>
                        <Dpc_Review before="Mage_Review">Dpc_Review</Dpc_Review>
                    </modules>
                </args>
            </review>
        </routers>
    </frontend>
    <global>
        <helpers>
            <dpc_review>
                <class>Dpc_Review_Helper</class>
            </dpc_review>
            <review>
                <rewrite>
                    <data>Dpc_Review_Helper_Data</data>
                </rewrite>
            </review>
        </helpers>
    </global>
</config>

Step 5: inside app/code/local/Dpc/Review/Helper create a file called Data.php

<?php

/**
 * Class Dpc_Review_Helper_Data
 */
class Dpc_Review_Helper_Data extends Mage_Review_Helper_Data
{

}

Step 6: inside app/code/local/Dpc/Review/controllers/ create a file called ProductController.php

<?php
require_once 'Mage' . DS . 'Review' . DS . 'controllers' . DS . 'ProductController.php';
/**
 * Class Dpc_Review_ProductController
 */
class Dpc_Review_ProductController extends Mage_Review_ProductController
{
    /**
     * Submit new review action
     *
     */
    public function postAction()
    {
        if (!$this->_validateFormKey()) {
            // returns to the product item page
            $this->_redirectReferer();
            return;
        }

        if ($data = Mage::getSingleton('review/session')->getFormData(true)) {
            $rating = array();
            if (isset($data['ratings']) && is_array($data['ratings'])) {
                $rating = $data['ratings'];
            }
        } else {
            $data   = $this->getRequest()->getPost();
            $rating = $this->getRequest()->getParam('ratings', array());
        }

        if (($product = $this->_initProduct()) && !empty($data)) {
            $session    = Mage::getSingleton('core/session');
            /* @var $session Mage_Core_Model_Session */
            $review     = Mage::getModel('review/review')->setData($data);
            /* @var $review Mage_Review_Model_Review */

            $validate = $review->validate();
            if ($validate === true) {
                try {
/****** This is the spot where we are now setting the value to STATUS_APPROVED *******/                        

$review->setEntityId($review->getEntityIdByCode(Mage_Review_Model_Review::ENTITY_PRODUCT_CODE))
                        ->setEntityPkValue($product->getId())
                        ->setStatusId(Mage_Review_Model_Review::STATUS_APPROVED)
                        ->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId())
                        ->setStoreId(Mage::app()->getStore()->getId())
                        ->setStores(array(Mage::app()->getStore()->getId()))
                        ->save();

                    foreach ($rating as $ratingId => $optionId) {
                        Mage::getModel('rating/rating')
                            ->setRatingId($ratingId)
                            ->setReviewId($review->getId())
                            ->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId())
                            ->addOptionVote($optionId, $product->getId());
                    }

                    $review->aggregate();
                    $session->addSuccess($this->__('Your review has been accepted'));
                }
                catch (Exception $e) {
                    $session->setFormData($data);
                    $session->addError($this->__('Unable to post the review.'));
                }
            }
            else {
                $session->setFormData($data);
                if (is_array($validate)) {
                    foreach ($validate as $errorMessage) {
                        $session->addError($errorMessage);
                    }
                }
                else {
                    $session->addError($this->__('Unable to post the review.'));
                }
            }
        }
        // this is my own custom need, feel free to do whatever you want here
        $product_url = $product->getUrlPath();
        if ($product_url) {
            $this->_redirect($product_url);
            return;
        }
        $this->_redirectReferer();
    }

}

Upvotes: 1

Related Questions