PHP Magento Tips

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"?>
<config>
    <modules>
        <Mycompany_Reviewcaptcha>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Review/>
            </depends>
        </Mycompany_Reviewcaptcha>
    </modules>
</config>

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"?>
<config>
    <modules>
        <Mycompany_Reviewcaptcha>
            <version>1.0.0</version>
        </Mycompany_Reviewcaptcha>
    </modules>
 <frontend>
        <layout>
            <updates>
                <mycompany_reviewcaptcha>
                    <file>mycompany_reviewcaptcha.xml</file>
                </mycompany_reviewcaptcha>
            </updates>
        </layout>
    </frontend>
    <global>
        <models>
            <mycompany_reviewcaptcha>
                <class>Mycompany_Reviewcaptcha_Model</class>
            </mycompany_reviewcaptcha>
        </models>
        <events>
            <controller_action_predispatch_review_product_post>
                <observers>
                    <addcaptcha>
                        <class>mycompany_reviewcaptcha/observer</class>
                        <method>checkReviewCaptcha</method>
                    </addcaptcha>
                </observers>
            </controller_action_predispatch_review_product_post>
        </events>
    </global>
    <default>
     <!-- Required for the in built Magento Captcha -->
        <captcha>
            <frontend>
                <areas>
                    <review><!-- take frontend router node -->
                        <label>Product Review Page</label>
                    </review>
                </areas>
            </frontend>
        </captcha>
        <customer>
            <captcha>
                <always_for>
                    <review>1</review>
                </always_for>
            </captcha>
        </customer>
    </default>
</config>

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

<?php

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)) {
                    $controller->getResponse()->setRedirect($redirectUrl);
                    return;
                }
                $referrerUrl = Mage::helper('core/http')->getHttpReferer() ? Mage::helper('core/http')->getHttpReferer()  : Mage::getUrl();
                $controller->getResponse()->setRedirect($referrerUrl);
            }
        }
        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">
 <review_product_list>
  <reference name="head">
   <action method="addJs">
    <file>mage/captcha.js</file>
   </action>
  </reference>
  
  <reference name="product.review.form">
   <!-- Add the Magento Captcha -->
   <block type="core/text_list" name="form.additional.info">
    <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>
    </block>
   </block>
  </reference>
 </review_product_list>
</layout>

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

app/design/frontend/rwd/default/template/review/form.phtml

and add below code just above your submit button.

<li>
    <?php echo $this->getChildHtml('form.additional.info'); ?>
</li>

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