2014-04-20 45 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