gpt4 book ai didi

Magento:将验证码添加到自定义联系表单

转载 作者:行者123 更新时间:2023-12-01 01:01:16 25 4
gpt4 key购买 nike

我在 Magento 网站上创建了一个新的自定义联系表单,我正在尝试在其末尾添加验证码。

我已添加到 app/code/core/Mage/Captcha/etc/config.xml

                <customcontacts>
<label>Custom Contact Form</label>
</customcontacts>

我添加到 app/design/frontend/base/default/layout/captcha.xml
    <customcontacts>
<reference name="customcontacts">
<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>customcontacts</formId></action>
<action method="setImgWidth"><width>230</width></action>
<action method="setImgHeight"><width>50</width></action>
</block>
</block>
</reference>
</customcontacts>

我在我的表单中添加了这段代码:
<script type="text/javascript">
//<![CDATA[
$('form-validate-captcha').captcha.refresh($('catpcha-reload'));
//]]>

我添加了这段代码来显示验证码:
<div><?php echo $this->getChildHtml('captcha'); ?></div>

然后我在配置>客户配置>验证码中启用了验证码,然后选择“自定义联系表”,然后将“显示模式”设置为“始终”。

但我仍然没有在我的自定义表单中获得验证码。

我希望我很清楚

谢谢

最佳答案

如果,你想添加magento own captcha to contact us form ,然后按照以下步骤......

不要在代码文件中写代码create an extension
Step1:创建模块控制文件

路径 app/etc/modules/Amit_Captchaown.xml添加以下代码

<?xml version="1.0" ?>
<config>
<modules>
<Amit_Captchaown>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Captcha/>
</depends>
</Amit_Captchaown>
</modules>
</config>

第2步:

app/code/local/Amit/Captchaown/etc/config.xml 中创建模块 config.xml 文件.重写 zend and captcha controllers的模型和这里的代码
 <?xml version="1.0"?>
<config>
<modules>
<Amit_Captchaown>
<version>1.0.0</version>
</Amit_Captchaown>
</modules>
<global>
<models>
<captcha>
<rewrite>
<zend>Amit_Captchaown_Model_Zend</zend>
</rewrite>
</captcha>
</models>
</global>
<default>
<captcha translate="label">
<frontend>
<areas>
<contact_us>
<label>Contact us Form</label>
</contact_us>
</areas>
</frontend>
</captcha>

<customer>
<captcha>
<always_for>
<contact_us>1</contact_us>
</always_for>
</captcha>
</customer>
</default>
<frontend>
<routers>
<contacts>
<args>
<modules>
<amit_captchaown before="Mage_Contacts">Amit_Captchaown</amit_captchaown>
</modules>
</args>
</contacts>
</routers>
</frontend>
</config>

第三步:

覆盖 Mage_Captcha_Model_Zend file _isUserAuth()功能
app/code/local/Amit/Captchaown/Model/Zend.php 中创建 Zend.php 文件.在这个函数中你只需要注释函数代码。
<?php
class Amit_Captchaown_Model_Zend extends Mage_Captcha_Model_Zend
{

protected function _isUserAuth()
{
/* return Mage::app()->getStore()->isAdmin()
? Mage::getSingleton(‘admin/session’)->isLoggedIn()
: Mage::getSingleton(‘customer/session’)->isLoggedIn();*
*/
}

}

第4步:
然后在contacts.xml路径app/design/frontend/our package/your template/layout中添加一些代码

添加和修改代码是
    <contacts_index_index translate="label">
<label>Contact Us Form</label>
<reference name="head">
<action method="setTitle" translate="title" module="contacts"><title>Contact Us</title></action>
</reference>
<reference name="root">
<action method="setTemplate"><template>page/2columns-right.phtml</template></action>
<action method="setHeaderTitle" translate="title" module="contacts"><title>Contact Us</title></action>
</reference>
<reference name="content">
<block type="core/template" name="contactForm" template="contacts/form.phtml">

<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>contact_us</formId></action>
<action method="setImgWidth"><width>230</width></action>
<action method="setImgHeight"><width>50</width></action>
</block>
</block>

</block>
</reference>
</contacts_index_index>

第五步:

然后添加代码 <?php echo $this->getChildHtml('form.additional.info'); ?>form.phtml路径 app/design/frontend/our package/your template/tempate/contacts .

第六步:

现在您需要覆盖联系我们 Controller 在 app/code/local/Amit/Captchaown/controllers/IndexController.php... 中创建文件。
<?php
require_once(Mage::getModuleDir('controllers','Mage_Contacts').DS.'IndexController.php');
class Amit_Captchaown_IndexController extends Mage_Contacts_IndexController
{

public function postAction()
{
$post = $this->getRequest()->getPost();
if ( $post ) {
$translate = Mage::getSingleton('core/translate');
/* @var $translate Mage_Core_Model_Translate */
$translate->setTranslateInline(false);
try {
$postObject = new Varien_Object();
$postObject->setData($post);

$error = false;

if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
$error = true;
}

if (!Zend_Validate::is(trim($post['comment']) , 'NotEmpty')) {
$error = true;
}

if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
$error = true;
}

if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
$error = true;
}

$formId ='contact_us';
$captchaModel = Mage::helper('captcha')->getCaptcha($formId);
if ($captchaModel->isRequired()) {
if (!$captchaModel->isCorrect($this->_getCaptchaString($this->getRequest(), $formId))) {
Mage::getSingleton('customer/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
$this->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);

Mage::getSingleton('customer/session')->setCustomerFormData($this->getRequest()->getPost());
$this->getResponse()->setRedirect(Mage::getUrl('*/*/'));
return;
}
}

if ($error) {
throw new Exception();
}
$mailTemplate = Mage::getModel('core/email_template');
/* @var $mailTemplate Mage_Core_Model_Email_Template */
$mailTemplate->setDesignConfig(array('area' => 'frontend'))
->setReplyTo($post['email'])
->sendTransactional(
Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
null,
array('data' => $postObject)
);

if (!$mailTemplate->getSentSuccess()) {
throw new Exception();
}

$translate->setTranslateInline(true);

Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
$this->_redirect('*/*/');

return;
} catch (Exception $e) {
$translate->setTranslateInline(true);

Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
$this->_redirect('*/*/');
return;
}

} else {
$this->_redirect('*/*/');
}
}

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

}

现在只需启用 captchacontact us form转到您的管理面板设置 sytem->configuration->customer configuration->Captcha

关于Magento:将验证码添加到自定义联系表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23181997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com