0

I've created a module and added a new frontend value to magento in order to display the Captcha on the product review page.

I've posted this into my form.phtml:

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

This is the node in my captcha.xml:

<review_product_list>
    <reference name="review_form">
        <block type="core/text_list" name="form.additional.info">
            <block type="captcha/captcha" name="captcha">
                <reference name="head">
                    <action method="addJs"><file>mage/captcha.js</file></action>
                </reference>
                <action method="setFormId"><formId>product_review</formId></action>
                <action method="setImgWidth"><width>230</width></action>
                <action method="setImgHeight"><width>50</width></action>
            </block>
        </block>
    </reference>
</review_product_list>

On the frontend I can verifyt that mage/captcha.js is loaded (when removing the XML, its not => I assume its working so far).

However, there is no captcha. What am I doing wrong?

1 Answer 1

2

I assume that you have forgotten to define form.additional.info in the layout file. It is hard to guess, which part you have missed.

You can follow below approach to apply captcha in the product review form:

Create app/etc/modules/Stack_Reviewcaptcha.xml and put the following code:

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

Next define module's configuration in the file app/code/local/Stack/Reviewcaptcha/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Stack_Reviewcaptcha>
            <version>0.1.0</version>
        </Stack_Reviewcaptcha>
    </modules>
    <global>
        <events>
            <controller_action_predispatch_review_product_post>
                    <observers>
                            <reviewcaptcha>
                                    <class>Stack_Reviewcaptcha_Model_Observer</class>
                                    <method>checkProductReview</method>
                            </reviewcaptcha>
                    </observers>
            </controller_action_predispatch_review_product_post>
            <core_layout_block_create_after>
                    <observers>
                            <form_captcha>
                                    <class>Stack_Reviewcaptcha_Model_Observer</class>
                                    <method>insertCaptcha</method>
                            </form_captcha>
                    </observers>
            </core_layout_block_create_after>
        </events>
    </global>
    <frontend>
        <layout>
            <updates>
                <reviewcaptcha>
                    <file>reviewcaptcha.xml</file>
                </reviewcaptcha>
            </updates>
        </layout>
    </frontend>
    <default>
        <captcha>
            <frontend>
                <areas>
                    <reviewform>
                        <label>Product Review Page</label>
                    </reviewform>
                </areas>
            </frontend>
        </captcha>
        <customer>
            <captcha>
                <always_for>
                    <reviewform>1</reviewform>
                </always_for>
            </captcha>
        </customer>
    </default>
</config>

Here we have defined two observer functions, one for inserting captcha and other for validating captcha.

Next, we will define the Observer.php file and write the functions we have defined above.

Create file app/code/local/Stack/Reviewcaptcha/Model/Observer.php and write the following code:

<?php
class Stack_Reviewcaptcha_Model_Observer
{
    public function checkProductReview($observer){
        $productId = $observer->getControllerAction()->getRequest()->getParam('id');
        $returnUrl =  Mage::getUrl('review/product/list', array('id'=> $productId));
        $formId = 'reviewform';
        $captchaModel = Mage::helper('captcha')->getCaptcha($formId);
        $controller = $observer->getControllerAction();
        if (!$captchaModel->isCorrect($this->_getCaptchaString($controller->getRequest(), $formId))){
            Mage::getSingleton('core/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
            Mage::app()->getResponse()->setRedirect($returnUrl)->sendResponse();
            exit;
        }
    }

    public function insertCaptcha($observer){
        $block = $observer->getEvent()->getBlock();
        if ($block instanceof Mage_Review_Block_Form){
            if (!$block->getChild('review_form_captcha')){
                $captcha = Mage::getBlockSingleton('captcha/captcha')->setFormId('reviewform')->setImageWidth(230)->setImageHeight(50);
                $block->setChild('review_form_captcha', $captcha);
            }
        }
    }

    protected function _getCaptchaString($request, $formId)
    {
        $captchaParams = $request->getPost(Mage_Captcha_Helper_Data::INPUT_NAME_FIELD_VALUE);
        return $captchaParams[$formId];
    }
}

Next, define the layout in app/design/frontend/[theme-package]/[theme]/layout/reviewcaptcha.xml

<?xml version="1.0"?>
<layout version="0.1.0">
    <catalog_product_view>
        <reference name="head">
            <action method="addJs"><file>mage/captcha.js</file></action>
        </reference>
    </catalog_product_view>
    <review_product_list>
        <reference name="head">
            <action method="addJs"><file>mage/captcha.js</file></action>
        </reference>
    </review_product_list>
</layout>

Final step: In the app/design/frontend/[theme-package]/[theme]/template/review/form.phtml, add the following code just above the tag:

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

Refresh the cache and check.

2
  • Great answer! I added the observers code to my existing observer and changed the name of the Child (which was defined in the XML above...). Thanks!
    – Max
    Commented May 14, 2018 at 11:51
  • Great to know that I could help :) Commented May 14, 2018 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.