Magento: add captcha to custom contact form

I created a new custom contact form on the Magento website, I'm trying to add captcha at the end of it.

I added in app / code / core / Mage / Captcha / etc / config.xml

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

And I added to 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>

And I added this code to my form:

<script type="text/javascript">
//<![CDATA[
$('form-validate-captcha').captcha.refresh($('catpcha-reload'));
//]]>

And I added this code to show Captcha:

<div><?php echo $this->getChildHtml('captcha'); ?></div>

Then I turned Captcha on in Configuration> Client Configuration> CAPTCHA, and I select "Custom Contact Form" and then "Display Mode" to "Always."

But I have not received Captcha in my user form yet.

I hope I was clean.

thank

+4
source share
2

magento own captcha to contact us form, .....

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>

2:

mod.xml app/code/local/Amit/Captchaown/etc/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>

3:

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();*
*/
}

}

4: contacts.xml app/design/frontend/ / /

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

5:

<?php echo $this->getChildHtml('form.additional.info'); ?> form.phtml path app/design/frontend/our package/your template/tempate/contacts.

6:

. /code/local/Amit/Captchaown/controller/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];
}

}

captcha contact us form sytem->configuration->customer configuration->Captcha

+3

, : " " .

Admin -> System -> Custom Variables :

enter image description here

( , - , ) app/design/frontend/base/default/template/contacts/form.phtml

, , , script Validation, 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 :

<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="", javascript, .

: enter image description here

+1

Source: https://habr.com/ru/post/1537269/


All Articles