Automatically approving ratings and/or reviews

This one kept me up all night, and would like to offer these code snippets to save someone else the trouble of automatically approving ratings and reviews. Using this page as a guide: 
http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/how_to_overload_a_controller

I created the following items: 
1) /app/code/local/Mynamespace/Review/controllers/ProductController.php 
2) /app/code/local/Mynamespace/Review/etc/config.xml 
3) /app/etc/modules/Mynamespace_Review.xml

Here’s the contents of each file: 
1) 

<?php 
/* this is the working version! hoorah */
require_once("Mage/Review/controllers/ProductController.php");
class Mynamespace_Review_ProductController extends Mage_Review_ProductController {
    
    public function postAction() {
        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 {
                    $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 rating has been approved.'));
                }
                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.'));
                }
            }
        }

        if ($redirectUrl = Mage::getSingleton('review/session')->getRedirectUrl(true)) {
            $this->_redirectUrl($redirectUrl);
            return;
        }
        $this->_redirectReferer();
    }
}
?>

 

2) 

<?xml version="1.0"?>
<config>
  <modules>
    <Mynamespace_Review>
        <version>0.0.1</version>
    </Mynamespace_Review>
  </modules>
  
  <frontend>
    <routers>
        <review>
            <args>
                <modules>
                    <Mynamespace_Review before="Mage_Review">Mynamespace_Review</Mynamespace_Review>
                </modules>
            </args>
        </review>
    </routers>
  </frontend>
</config>

 3)

<?xml version="1.0"?>
<config>
  <modules>
    <Mynamespace_Review>
      <active>true</active>
      <codePool>local</codePool>
    </Mynamespace_Review>
  </modules>
</config>

 

你可能感兴趣的:(view)