Magento 1.7 Captcha模块

时间:2012-09-25 15:31:22

标签: magento captcha magento-1.7

在新的Magento版本中,“系统 - >配置 - >客户配置 - > Captcha”中的验证码选项我创建了一个名为“Signmeup”的新表单......但它似乎不起作用。我无法将其显示出来:

<?php echo Mage::getSingleton('core/layout')
->createBlock('captcha/captcha_zend')
->setFormId('signmeup')
->setImgWidth(230)
->setImgHeight(50)
->setTemplate('captcha/zend.phtml')
->toHtml();?>

现在该页面上没有显示该块。 (不是动态页面......带有核心Mage Bootup的静态页面) 这是我正在谈论的屏幕截图:

2 个答案:

答案 0 :(得分:2)

在线找到captcha.xml代码后(由于某种原因不在我的企业中),我创建了captcha.xml并将该代码粘贴到布局文件夹中。

接下来,我确保在admin > Config > Customer config..

中打开了所有这些内容

我确实要添加我希望Captcha显示的phtml文件:

echo $this->getChildHtml('form.additional.info');  (in php brackets.)

一旦我这样做,一切都显示出来并且功能正常。

答案 1 :(得分:0)

我能够通过创建一个将本机验证码模块添加到产品评论表单的小模块来实现这一目的。该模块只包含几个文件:

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)更改为您的表单所在的任何页面 我希望我能够详细介绍所有内容,并且有人能够根据自己的需要进行调整。

关于这个主题的其他一些资源: