我可以通过创建一个小模块来做到这一点,该模块将本机验证码模块添加到产品评论表单中。该模块仅包含几个文件:
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
将以下代码添加到包含您希望将验证码添加到的表单的模板 (.phtml) 文件中:
<?php echo $this->getLayout()->createBlock('captcha/captcha')
->setFormId('your_form_id')
->setImgWidht(230)
->setImgHeight(50)
->toHtml();
?>
将“your_form_id”更改为您想要的任何内容。
在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>
config.xml就是这样。
现在让我们添加我们的观察者。以下代码来自http://mustakarhu.com/blog/magento-captcha-extension-ajax/并且只进行了轻微更改,因此请大声喊出他们。
<?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;
}
}
?>
大部分工作都已经结束了。我将把MyCompany_MyCaptcha.xml留给您自己解决(这非常简单)。
在 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>
此布局 xml 将必要的 javascript 添加到产品页面的 head 部分。您需要将布局句柄 (catalog_product_view) 更改为表单所在的任何页面。
我希望我对所有内容都进行了足够详细的介绍,并且有人能够根据自己的需要进行调整。
关于这个主题的一些其他资源: