Automatically approving ratings and/or reviews

本文介绍了一个Magento扩展,该扩展能够自动审批产品评价和评分。通过重写原生控制器,实现无需管理员手动操作即可完成评价发布的过程。适用于希望简化评价管理流程的Magento商店。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值