How to implement Magento Captcha in Contact- Us form?
-
13-12-2020 - |
Domanda
I need to show default magento captcha in contact-us page.
Soluzione
Created a new module for this.
Step 1:
app/code/local/Contact/Captcha/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Contact_Captcha>
<version>0.1.0</version>
</Contact_Captcha>
</modules>
<default>
<captcha>
<frontend>
<areas>
<contact_page_captcha>
<label>Contact Page</label>
</contact_page_captcha>
</areas>
</frontend>
</captcha>
</default>
<global>
<models>
<contact_captcha>
<class>Contact_Captcha_Model</class>
</contact_captcha>
</models>
<events>
<controller_action_predispatch_contacts_index_post>
<observers>
<captcha>
<type>model</type>
<class>contact_captcha/observer</class>
<method>checkContactPage</method>
</captcha>
</observers>
</controller_action_predispatch_contacts_index_post>
</events>
</global>
</config>
Step 2:
app/etc/modules/Contact_Captcha.xml
<?xml version="1.0"?>
<config>
<modules>
<Contact_Captcha>
<active>true</active>
<codePool>local</codePool>
</Contact_Captcha>
</modules>
</config>
Step 3:
app/code/local/Contact/Captcha/Model/Observer.php
<?php
class Contact_Captcha_Model_Observer extends Mage_Captcha_Model_Observer
{
public function checkContactPage($observer)
{
$formId = 'contact_page_captcha';
$captchaModel = Mage::helper('captcha')->getCaptcha($formId);
if ($captchaModel->isRequired()) {
$controller = $observer->getControllerAction();
if (!$captchaModel->isCorrect($this->_getCaptchaString($controller->getRequest(), $formId))) {
Mage::getSingleton('customer/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
$controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
$controller->getResponse()->setRedirect(Mage::getUrl('*/*/'));
}
}
return $this;
}
}
Step 4:
Create a local.xml in theme path.
app/design/frontend/theme/default/layout/local.xml
<?xml version="1.0"?>
<layout version="0.1.0">
<contacts_index_index translate="label">
<reference name="contactForm">
<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_page_captcha</formId></action>
<action method="setImgWidth"><width>230</width></action>
<action method="setImgHeight"><width>50</width></action>
</block>
</block>
</reference>
</contacts_index_index>
</layout>
Step 5:
app/design/frontend/theme/default/template/contacts/form.phtml
Add below code:
<?php echo $this->getChildHtml('form.additional.info'); ?>
Step 6:
Admin Settings:
Dashboard - > Customers - > Customer Configuration - > CAPTCHA - > Forms - > Select Contact Page
Step 7:
Clear cache. Refresh contact us page.
Altri suggerimenti
You need to either create a custom module or install an extension for this.
Below is the code for implementation as a custom module:
app/etc/modules/Mohit_Contactcaptcha.xml
<?xml version="1.0"?>
<config>
<modules>
<Mohit_Contactcaptcha>
<active>true</active>
<codePool>local</codePool>
</Mohit_Contactcaptcha>
</modules>
</config>
app/code/local/Mohit/Contactcaptcha/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Mohit_Contactcaptcha>
<version>0.1.0</version>
</Mohit_Contactcaptcha>
</modules>
<frontend>
<layout>
<updates>
<contactcaptcha>
<file>contactcaptcha.xml</file>
</contactcaptcha>
</updates>
</layout>
</frontend>
<global>
<models>
<contactcaptcha>
<class>Mohit_Contactcaptcha_Model</class>
</contactcaptcha>
</models>
<events>
<controller_action_predispatch_contacts_index_post>
<observers>
<contactcaptcha>
<class>contactcaptcha/observer</class>
<method>checkContacts</method>
</contactcaptcha>
</observers>
</controller_action_predispatch_contacts_index_post>
</events>
</global>
<default>
<captcha>
<frontend>
<areas>
<contacts>
<label>Contacts Page</label>
</contacts>
</areas>
</frontend>
</captcha>
<customer>
<captcha>
<always_for>
<contacts>1</contacts>
</always_for>
</captcha>
</customer>
</default>
</config>
app/code/local/Mohit/Contactcaptcha/Model/Observer.php
<?php
class Mohit_Contactcaptcha_Model_Observer
{
public function checkContacts($observer){
$formId = 'contacts';
$captchaModel = Mage::helper('captcha')->getCaptcha($formId);
if ($captchaModel->isRequired()){
$controller = $observer->getControllerAction();
$word = $this->_getCaptchaString($controller->getRequest(), $formId);
if (!$captchaModel->isCorrect($word)) {
Mage::getSingleton('customer/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
$controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
$url = Mage::getUrl('contacts');
$controller->getResponse()->setRedirect($url);
}
}
return $this;
}
/**
* Get Captcha String
*
* @param Varien_Object $request
* @param string $formId
* @return string
*/
protected function _getCaptchaString($request, $formId)
{
$captchaParams = $request->getPost(Mage_Captcha_Helper_Data::INPUT_NAME_FIELD_VALUE);
return $captchaParams[$formId];
}
}
app/design/frontend/default/default/layout/contactcaptcha.xml
<?xml version="1.0"?>
<layout version="0.1.0">
<contacts_index_index>
<reference name="contactForm">
<action method="setTemplate"><template>contactcaptcha/contacts/form.phtml</template></action>
<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>contacts</formId></action>
<action method="setImgWidth"><width>230</width></action>
<action method="setImgHeight"><width>50</width></action>
</block>
</block>
</reference>
</contacts_index_index>
</layout>
app/design/frontend/default/default/template/contactcaptcha/contacts/form.phtml
<?php
/**
* Magento
*
* @category design
* @package base_default
* @copyright Copyright (c) 2013 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
?>
<div id="messages_product_view"><?php echo $this->getMessagesBlock()->getGroupedHtml() ?></div>
<div class="page-title">
<h1><?php echo Mage::helper('contacts')->__('Contact Us') ?></h1>
</div>
<form action="<?php echo $this->getFormAction(); ?>" id="contactForm" method="post">
<div class="fieldset">
<h2 class="legend"><?php echo Mage::helper('contacts')->__('Contact Information') ?></h2>
<ul class="form-list">
<li class="fields">
<div class="field">
<label for="name" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Name') ?></label>
<div class="input-box">
<input name="name" id="name" title="<?php echo Mage::helper('contacts')->__('Name') ?>" value="<?php echo $this->escapeHtml($this->helper('contacts')->getUserName()) ?>" class="input-text required-entry" type="text" />
</div>
</div>
<div class="field">
<label for="email" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Email') ?></label>
<div class="input-box">
<input name="email" id="email" title="<?php echo Mage::helper('contacts')->__('Email') ?>" value="<?php echo $this->escapeHtml($this->helper('contacts')->getUserEmail()) ?>" class="input-text required-entry validate-email" type="text" />
</div>
</div>
</li>
<li>
<label for="telephone"><?php echo Mage::helper('contacts')->__('Telephone') ?></label>
<div class="input-box">
<input name="telephone" id="telephone" title="<?php echo Mage::helper('contacts')->__('Telephone') ?>" value="" class="input-text" type="text" />
</div>
</li>
<li class="wide">
<label for="comment" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Comment') ?></label>
<div class="input-box">
<textarea name="comment" id="comment" title="<?php echo Mage::helper('contacts')->__('Comment') ?>" class="required-entry input-text" cols="5" rows="3"></textarea>
</div>
</li>
<?php echo $this->getChildHtml('form.additional.info'); ?>
</ul>
</div>
<div class="buttons-set">
<p class="required"><?php echo Mage::helper('contacts')->__('* Required Fields') ?></p>
<input type="text" name="hideit" id="hideit" value="" style="display:none !important;" />
<button type="submit" title="<?php echo Mage::helper('contacts')->__('Submit') ?>" class="button"><span><span><?php echo Mage::helper('contacts')->__('Submit') ?></span></span></button>
</div>
</form>
<script type="text/javascript">
//<![CDATA[
var contactForm = new VarienForm('contactForm', true);
//]]>
</script>
After creating the module, login to admin panel and navigate to Store > Configuration > Customers > Customers Configuration > Captcha and select Contacts Page from given options
Please let me know if you find any problem.
how to fix Contact-us page captcha not showing in magento 1?