`
kirenenko04
  • 浏览: 145983 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

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>

 

分享到:
评论

相关推荐

    MediaGo的安装包

    Where applicable, Media Go will also automatically download and/or convert certain content (e.g. a podcast) into a suitable format. Media Go can also purchase, backup, and restore.

    vga显示卡控制器开源项目 vhdl源代码

    bank-switching function, which reduces flicker and cluttered images by automatically switching between video-memory pages and/or color lookup tables on each vertical retrace. The core can interrupt ...

    ddx_sdk4

    With the DDX SDK V4, you can write filters that can:· Perform in-place filtering· Process IEEE 32-bit floating point and/or 16-bit integer PCM data.· Process mono and/or stereo data. · Have up to...

    PIC16F877头文件

    // This header file was automatically generated by: // // inc2h.pl V1.6 // // Copyright (c) 2002, Kevin L. Pauba, All Rights Reserved // // SDCC is licensed under the GNU Public license (GPL) v2. Note...

    VGA/LCD Core v2.0 Specifications

    video-memory pages and/or color lookup tables on each vertical retrace. The optional hardware cursors provide additional flexibility through two 32x32 16bpp or 64x64 4bpp hardware generated cursors. ...

    Crystal Impact Match!2.1.3 试用延长

    As reference database, you can apply the included free-of-charge COD database and/or ICSD/Retrieve (if you have a valid licence), use any ICDD PDF product, and/or create a user database based on your ...

    Ginger automatically corrects spelling and grammar

    能够对word里的汉语和英文进行语法检测,并能够提出修改意见,对于写论文有很大的帮助,快速比较简单。

    mvc grid工具

    Many of the LINQ samples require XML files and/or the version of the Northwind sample database that is located in the Data directory. By default the Data directory appears under the same parent folder...

    Google Nexus 4 ToolKit v1.3.0

    * Backup/Restore a single package or all apps, user data and Internal Storage * Backup your /data/media (virtual SD Card) to your PC for a Full Safe backup of data * Unlock/Re-Lock your Bootloader * ...

    智能天线smart antenna system

    A combines multiple antenna elements with a signalprocessing capability to optimize its radiation and/or reception pattern automatically in response to the signal environment.

    TRU64 UNIX MME304

    can play back AVI files with JPEG, Intel's Indeo, or YUV data and MPEG-1 audio/video files. o Audio Control -- a utility to control volume and port selection for all audio devices. Multimedia ...

    gm8136数据手册

    estimation calculation task for 16x16 or 8x8 block. MPEG4/JPEG codec will also automatically perform the DCT/quantization, IDCT/inverse quantization, AC/DC prediction, zigzag scan, and VLC/VLD ...

    windows下命令行发送邮件

    Blat is useful for creating s cripts where mail has to be sent automatically (CGI, backups, etc.), or just as a quick way to send a file or message quickly from the command line. It will store ...

    注册表吸尘器

    If you don't understand the complexities of RegVac and/or the Registry but you still want a clean Registry, the Novice Mode is for you. The Novice Mode has only two controls on it, a "Clean ...

    Bounds Checker6.01 for Delphi

    BoundsChecker automatically pinpoints static, stack and heap memory errors, and resource leaks. Unlike ordinary memory-checking tools, BoundsChecker validates the latest Windows APIs including ...

    Universal-USB-Installer

    either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you...

    TMS FlexCel Studio v6.3.0.0 Full Source

    ApiMate tool for automatically showing needed Delphi/C++ code for generating specific .XLS/.XLSX file cells with Flexcel Templates can be embedded inside your exe. No additional files to distribute ...

    phpmaker610官方安装版

    Server-side validation and/or client-side JavaScript validation Optional search features (Quick, Extended Quick and Advanced) with search result highlight Optional User ID and User Level Advanced ...

    php解决Deprecated Automatically populating is deprecated and will be removed_will后面跟什么

    php解决Deprecated Automatically populating is deprecated and will be removed

    delphi 7 gif控件

    // Image.Parent.Invalidate or Image.Parent.Refresh to restore the background. // // This change was made as a result of a email correspondance with // // Tineke Kosmis (http://www.classe.nl/) which ...

Global site tag (gtag.js) - Google Analytics