PHP Magento Tips

Powered by Blogger.

Add Magento Default Captcha on Product Review Page in Magento

Rate this posting:

In this article I’m going to describe how you can add Magento native captcha in product review form. In Magento, a default captcha is available for only some forms like login, registration, checkout as guest only. But if you want to use the same captcha for product review form then follow below steps.

Step 1: Create Module Initialization file. Create file in app/etc/modules/Mycompany_Reviewcaptcha.xml and add below code in it.

<?xml version="1.0"?>

Step 2: Create Module configuration file. Create file in app/code/local/Mycompany/Reviewcaptcha/etc/config.xml and add below code in it.

<?xml version="1.0"?>
     <!-- Required for the in built Magento Captcha -->
                    <review><!-- take frontend router node -->
                        <label>Product Review Page</label>

Step 3: Create Observer to check captcha. Create file in app/code/local/Mycompany/Reviewcaptcha/Model/Observer.php and add below code in it.


class Mycompany_Reviewcaptcha_Model_Observer
    public function checkReviewCaptcha (Varien_Event_Observer $observer){
        $formId = 'review';
        $captchaModel = Mage::helper('captcha')->getCaptcha($formId);
        if ($captchaModel->isRequired()) {
            $controller = $observer->getControllerAction();
            $word = $this->_getCaptchaString($controller->getRequest(), $formId);
            if (!$captchaModel->isCorrect($word)) {
                Mage::getSingleton('customer/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
                $controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
                if ($redirectUrl = Mage::getSingleton('review/session')->getRedirectUrl(true)) {
                $referrerUrl = Mage::helper('core/http')->getHttpReferer() ? Mage::helper('core/http')->getHttpReferer()  : Mage::getUrl();
        return $this;
     * Get Captcha String
     * @param Varien_Object $request
     * @param string $formId
     * @return string
    protected function _getCaptchaString($request, $formId)
        $captchaParams = $request->getPost(Mage_Captcha_Helper_Data::INPUT_NAME_FIELD_VALUE);
        return $captchaParams[$formId];

Step 4: Create frontend layout file. Create file in app/design/frontend/rwd/default/layout/mycompany_reviewcaptcha.xml and add below code in it.

<?xml version="1.0"?>
<layout version="0.1.0">
  <reference name="head">
   <action method="addJs">
  <reference name="">
   <!-- Add the Magento Captcha -->
   <block type="core/text_list" name="">
    <block type="captcha/captcha" name="captcha">
     <action method="setFormId"><formId>review</formId></action>
     <action method="setImgWidth"><width>230</width></action>
     <action method="setImgHeight"><height>50</height></action>

Step 5: To display captcha in your product review form open the file


and add below code just above your submit button.

    <?php echo $this->getChildHtml(''); ?>

Now Go to your Magento Admin and under

System > Configuration > Customers > Customer Configuration > CAPTCHA

Select "Product Review Page" to Enable captcha.

You will find captcha as per below screenshot.

Tag : Magento
0 Comments On "Add Magento Default Captcha on Product Review Page in Magento"

Back To Top