Reputation: 367
In the new Magento release the captcha options in "System->Configurations->Customer Configuration->Captcha" I created a new form called "Signmeup"... but it does not seem to be working. I am having trouble getting it to show:
<?php echo Mage::getSingleton('core/layout')
->createBlock('captcha/captcha_zend')
->setFormId('signmeup')
->setImgWidth(230)
->setImgHeight(50)
->setTemplate('captcha/zend.phtml')
->toHtml();?>
Right now that block is not being displayed on the page. (Not a dynamic page... static page with core Mage Bootup) Here is a screen shot of what I am talking about:
Upvotes: 3
Views: 14672
Reputation: 123
I was able to do this by creating a small module that adds the native captcha module to the product review form. The module consists of just a few files:
app/code/local/MyCompany/MyCaptcha/etc/config.xml
app/code/local/MyCompany/MyCaptcha/Model/Observer.php
app/etc/modules/MyCompany_MyCaptcha.xml
app/design/frontend/default/default/layout/mycaptcha.xml
Add the following code to the template (.phtml) file containing the form you wish to add the captcha to:
<?php echo $this->getLayout()->createBlock('captcha/captcha')
->setFormId('your_form_id')
->setImgWidht(230)
->setImgHeight(50)
->toHtml();
?>
change 'your_form_id' to whatever you'd like.
In config.xml:
<config>
<modules>
<MyCompany_MyCaptcha>
<version>1.0.0</version>
</MyCompany_MyCaptcha>
</modules>
<frontend>
<layout>
<updates>
<mycaptcha> <!-- should be some unique name -->
<file>mycaptcha.xml</file>
</mycaptcha>
</updates>
</layout>
</frontend>
<!-- Now we need to add our observer. I attached mine to the
controller_action_predispatch_review_product_post event because
I needed to intercept product review post event. The event you
attach your observer to will be different depending on what you're
trying to do. -->
<global>
<events>
<controller_action_predispatch_review_product_post>
<observers>
<mycaptcha> <!-- these need to match -->
<class>MyCompany_MyCaptcha_Model_Observer</class>
<method>myMethod</method>
</mycaptcha>
</observers>
</controller_action_predispatch_review_product_post>
</events>
</global>
<!-- Now we add our form label that will show in configuration and allow
us to turn the captcha on or off. -->
<default>
<captcha>
<frontend>
<areas>
<mycaptcha> <!-- these need to match -->
<label>My Captcha</label>
</mycaptcha>
</areas>
</frontend>
</captcha>
</default>
</config>
That's it for config.xml
Now let's add our observer. The following code is from http://mustakarhu.com/blog/magento-captcha-extension-ajax/ and has only been slightly altered so shout out to them.
<?php
/**
* Break the execution in case of incorrect CAPTCHA
*
* @param Varien_Event_Observer $observer
* @return Cbad_Captcha_Model_Observer
*/
class MyModule_MyCaptcha_Model_Observer extends Mage_Captcha_Model_Observer
{
public function myMethod($observer) { // called in config.xml
$formId = 'your_form_id'; // you will change this value
$captchaModel = Mage::helper('captcha')->getCaptcha($formId);
$controller = $observer->getControllerAction();
$request = $controller->getRequest();
if ($captchaModel->isRequired()) {
$request->getPost(Mage_Captcha_Helper_Data::INPUT_NAME_FIELD_VALUE);
if (!$captchaModel->isCorrect($this->_getCaptchaString($request, $formId))) {
if((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')) {
// Is ajax
$action = $request->getActionName();
Mage::app()->getFrontController()->getAction()->setFlag(
$action, Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
$controller->getResponse()->setHttpResponseCode(200);
$controller->getResponse()->setHeader('Content-type', 'application/json');
$controller->getResponse()->setBody(json_encode(
array(
"msg" => Mage::helper('captcha')->__('Incorrect CAPTCHA.')
)
));
} else {
// Is form submit
Mage::getSingleton('customer/session')
->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
$controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
Mage::getSingleton('customer/session')
->setCustomerFormData($controller->getRequest()->getPost());
$controller->getResponse()->setRedirect(Mage::getUrl('*/*'));
}
}
}
return $this;
}
}
?>
Most of the work is out of the way. I'll leave MyCompany_MyCaptcha.xml for you to figure out on your own (it's incredibly simple).
Onto mycaptcha.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>
</layout>
This layout xml adds the necessary javascript to the head section on a product page. You will need to change the layout handle (catalog_product_view) to whatever page your form will be on.
I hope I covered everything in enough detail and someone will be able to adapt this to their own needs.
Some other resources on this subject:
Upvotes: 0
Reputation: 350
After finding the captcha.xml code online (which for some reason was not in my enterprise), I created captcha.xml
and pasted that code in the layout folder.
Next, I made sure every this was turned on in the admin > Config > Customer config..
I did have to add the the phtml files which I wanted the Captcha to show:
echo $this->getChildHtml('form.additional.info'); (in php brackets.)
Once i did that everything was shown and functional.
Upvotes: 2