2014-04-20 43 views
4

我在Magento网站创建了一个新的自定义联系表单,我试图在它的末尾添加验证码。Magento:添加验证码到自定义联系表格

我已经添加到应用程序/代码/核心/法师/验证码的/ etc/config.xml中

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

我添加到应用程序/设计/前端/碱/默认/布局/ 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> 

然后我启用验证码在配置>客户配置> CAPTCHA,我选择“自定义联系表”,然后“显示模式”为“Always”。

但我仍然没有得到我的自定义形式的验证码。

我希望我是清楚的

感谢

+0

的Khali,检查现在我的答案 –

+0

我做这个工作,我希望它适用于自定义联系人窗体,这是它:www.cabas-durables.fr/custom-contact/ – Khalil

+0

请设置管理 –

回答

3

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

在代码文件

DONOT写代码create an extension

步骤1:创建模块控制文件

路径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> 

第二步:

创建的zend and captcha controllersapp/code/local/Amit/Captchaown/etc/config.xml .Rewrite模型,并在此代码

<?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> 

第三步模块config.xml文件:

覆盖Mage_Captcha_Model_Zend file _isUserAuth()功能 创建Zend.php文件在app/code/local/Amit/Captchaown/Model/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();* 
*/ 
} 

} 

第四步: 然后加入contacts.xml路径一些代码的应用程序/设计/前端/我们的包/模板/布局

添加和修改代码

<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

第六步:

现在,你需要重写与我们联系控制器创建应用程序/代码/本地/艾米特/ Captchaown /控制器/ 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

+0

这可以工作自定义联系表格,谢谢 – Khalil

+0

它现在在管理员,我可以看到“自定义联系表格”已被添加到验证码列表,因为我命名它,但保存配置后,没有出现在页面中,我补充说在我的form.phtml文件中的行如下所示:'​​<?php echo $ this-> getChildHtml('form.additional.info'); ?> ',是否有什么需要添加更多,谢谢 – Khalil

+0

请尝试新鲜 –

1

我想出了阿米特·贝拉的答案的变化这是很容易实现:在“是什么人”书面问题和ANSW呃

Admin -> System -> Custom Variables创建两个变量:

enter image description here

编辑的联系表格模板文件(可能是在其他地方,如果你购买了模板)app/design/frontend/base/default/template/contacts/form.phtml

接近榜首,仅在标题下,我添加了这些行来检索自定义变量信息,然后在脚本标记中,将自定义验证函数添加到原型JS中声明的验证类。通过在模板文件中使用JavaScript,您可以通过php动态设置问题的答案变量。

<?php 
$confirm_people_question = Mage::getModel('core/variable') 
    ->setStoreId(Mage::app()->getStore()->getId()) 
    ->loadByCode('contact_people_question') 
    ->getValue('text'); 
$confirm_people_answer = Mage::getModel('core/variable') 
    ->setStoreId(Mage::app()->getStore()->getId()) 
    ->loadByCode('contact_people_answer') 
    ->getValue('text'); 
?> 
<script type="text/javascript"> 
Validation.add(
    'validate-people','Incorrect answers prevent automated emails' 
    ,function(v){ 
    var m="<?php echo $confirm_people_answer ?>"; 
    return Validation.get('IsEmpty').test(v) || (v==m); 
}); 
</script> 

再往下在同一template/contacts/form.phtml文件,在新的字段添加到窗体本身:在javascript函数

<li> 
    <label for="people" class="required"><em>*</em> 
    <?php echo $confirm_people_question ?></label> 
    <div class="input-box"> 
     <input name="people" id="people" 
       title="Please confirm you are people" value="" 
       class="required-entry input-text validate-people" type="text" /> 
    </div> 
</li> 

注意validate-people文本是<input class=""属性相同,以及我们以上添加。

最终结果: enter image description here