Magento Product Review Captcha
In this article I’m going to describe how you add 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 also. In this article I’m gonna show you how to use Magento’s native captcha for product reviews. Magento has a powerful Captcha module, but unfortunately there is no way of using this Captcha for the product review form out of the box. In this tutorial you will learn how to make a few small changes to the Magento file system in order to enable Magento’s native captcha for product reviews.
By default Product Reviews are open to all visitors of your website. I personally think this is the best option, but this also enables automated spam bots to fill out and submit the product review form. Another powerful feature of Magento is that is has Captcha build in. The Captcha form will prevent automated spammers from flooding your store with junk messages. Magento’s native Captcha can be used for both the admin and customers. For the stores frontend you can configure the native Magento Customer Captcha in
System > Configuration > Customers > Customer Configuration > CAPTCHA
After enabling the Captcha on frontend you will see that there are several forms which Captcha can be added to. However, the Magento Product Review form is not included in this list. So in order to add Magento’s native captcha to the Product Review form you will have to make a few changes.
This tutorial is based on Magento CE 1.9.1.0 and I will be using the Default package and theme. To try and add the Magento Product Review Captcha to a custom theme simply replace /DEFAULT/default/ with /YOURPACKAGE/default/
Follow below steps:-
1. Copy the file
app/design/frontend/base/default/template/review/form.phtml
to
app/design/frontend/default/default/template/review/
(or your package template folder)
2. Open up this form.phtml and around line 87-88 add the following code:
<li><?php echo $this->getChildHtml('form.additional.info'); ?></li>
3. Copy the file
app/design/frontend/base/default/layout/review.xml
to
app/design/frontend/default/default/layout/
(or your package layout folder)
4. Open up this review.xml and around line 101-102 add the following code:
<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>250</width></action> <action method="setImgHeight"><width>80</width></action> </block> </block>
5. Create the file
app/code/community/Prashant/ReviewCaptcha/etc/config.xml
6. Open up this newly created config.xml file and add the following code:
<?xml version="1.0"?> <!-- /** * @category Prashant * @package Prashant_ReviewCaptcha * @author Prashant Kumar * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) */ --> <config> <modules> <Prashant_ReviewCaptcha> <version>0.1.0</version> </Prashant_ReviewCaptcha> </modules> <global> <models> <reviewcaptcha> <class>Prashant_ReviewCaptcha_Model</class> </reviewcaptcha> </models> <helpers> <reviewcaptcha> <class>Prashant_ReviewCaptcha_Helper</class> </reviewcaptcha> </helpers> <events> <controller_action_predispatch_review_product_post> <observers> <reviewcaptcha> <class>reviewcaptcha/observer</class> <method>checkProductReview</method> </reviewcaptcha> </observers> </controller_action_predispatch_review_product_post> </events> </global> <default> <captcha translate="label"> <frontend> <areas> <product_review> <label>Product Review</label> </product_review> </areas> </frontend> </captcha> </default> </config>
7. Create the file
app/code/community/Prashant/ReviewCaptcha/Helper/Data.php
8. Open up this newly created Data.php file and add the following code:
<?php /** * Prashant_ReviewCaptcha * * NOTICE OF LICENSE * * This source file is subject to the Open Software License (OSL 3.0) * http://opensource.org/licenses/OSL-3.0 * * @package Prashant_ReviewCaptcha * @author Prashant Kumar * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ /** * Class Prashant_ExtraDescription_Helper_Data */ class Prashant_ReviewCaptcha_Helper_Data extends Mage_Core_Helper_Abstract { }
9. Create the file
app/code/community/Prashant/ReviewCaptcha/Model/Observer.php
10. Open up this newly created Observer.php file and add the following code:
<?php /** * ReviewCaptcha Observer * * @category Prashant * @package Prashant_ReviewCaptcha * @author Prashant Kumar */ class Prashant_ReviewCaptcha_Model_Observer extends Mage_Captcha_Model_Observer { /** * Check Captcha On Product Review Page * * @param Varien_Event_Observer $observer * @return Mage_Captcha_Model_Observer */ public function checkProductReview($observer) { $productId = $observer->getControllerAction()->getRequest()->getParam('id'); $formId = 'product_review'; $captchaModel = Mage::helper('captcha')->getCaptcha($formId); if ($captchaModel->isRequired()) { $controller = $observer->getControllerAction(); if (!$captchaModel->isCorrect($this->_getCaptchaString($controller->getRequest(), $formId))) { Mage::getSingleton('core/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.')); $controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true); $controller->getResponse()->setRedirect(Mage::getUrl('review/product/list', array('id'=> $productId))); } } return $this; } }
11. To register this new module create the file
app/etc/modules/Prashant_ReviewCaptcha.xml
12. Open up this newly created Prashant_ReviewCaptcha.xml file and add the following code:
<?xml version="1.0"?> <config> <modules> <Prashant_ReviewCaptcha> <active>true</active> <codePool>community</codePool> <depends> <Mage_Captcha/> </depends> </Prashant_ReviewCaptcha> </modules> </config>
Now custom product review captcha module has been created successfully.
Now first go to your Magento admin panel and flush all cache. Log out and back in to activate the new Magento Product Review Captcha extension.
Now go to System > Configuration > Advanced > Advanced and check if the new module is there. If it’s not then check all previous steps.
Then go to System > Configuration > Customers > Customer Configuration > CAPTCHA. You will see that the Forms multi-select field now contains the Product Review option at the bottom. Select this option (along with all other forms that you wish to use captcha for) and hit the “Save Config” button.
I hope you will enjoy this tutorial. If you have any questions, feel free to leave a message at the comments below.
hello your post is nice but can you explain in how to add magento2.1.1 product review catch how to add.