DISEÑO WEB BARCELONA
Tel. 937 932 061 - Mov. 653 610 980

Cómo poner un Captcha en Formulario de Contacto de Magento1


magento1 captcha formulario contacto

Si necesitas mostrar el captcha predeterminado de magento1 en la página de contacto, hay que crear un módulo personalizado o instalar una extensión para esto.

A continuación te mostramos el código para la implementación como un módulo personalizado:

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>

Borrar la cache de magento1

Después de crear el módulo, inicie sesión en el panel de control de Magento1 y vaya a Sistema> Configuración> Clientes> Configuración de cliente> Captcha y seleccione Página de contactos de entre las opciones dadas.

magento1 captcha formulario contacto admin

Borrar la cache de magento1 y actualizar la página de contacto de tu web /contacts.

Puedes descargar los archivos del módulo aquí.

Responder