How to add Google reCaptcha on contacts Page in Prestashop


Prestashop is one of the most used ecommerce CMS after Magento. But the default contact form in Prestashop CMS has no protection again the spam.

Here is a step by step for prestashop 1.6.

1 Setup your Google reCAPTCHA if you don’t have it

https://www.google.com/recaptcha/intro/index.html

Note the key and the secret key

You have to configure it and put your domain name.

2  Change the contact-form.tpl

First you have to change the contact form (client side) contact-form.tpl should be in your theme. Open an editor and search the submit botton

For me it is like that

<div class="submit">

 <input type="text" name="url" value="" class="hidden" />

 <input type="hidden" name="contactKey" value="{$contactKey}" />

 <button type="submit" name="submitMessage" id="submitMessage" class="button btn btn-default button-medium"><span>{l s='Send'}<i class="icon-chevron-right right"></i></span></button>

</div>


Just above add few lines of code. Replace key by your key



3 Change ContactControler.php

If you don’t have this source in override/controllers/front copy it from controllers/front

Then you have to add few lines to check if the captcha is ok or not

search the postProcess() function it should be at the begining (2nd function) and add

public function postProcess()

    {

        if (Tools::isSubmit('submitMessage')) {

            $saveContactKey = $this->context->cookie->contactFormKey;

            $extension = array('.txt', '.rtf', '.doc', '.docx', '.pdf', '.zip', '.png', '.jpeg', '.gif', '.jpg');

            $file_attachment = Tools::fileAttachment('fileUpload');

            $message = Tools::getValue('message'); // Html entities is not usefull, iscleanHtml check there is no bad html tags.

            $url = Tools::getValue('url');

            if (!($from = trim(Tools::getValue('from'))) || !Validate::isEmail($from)) {

                $this->errors[] = Tools::displayError('Invalid email address.');

            } elseif (!$message) {

                $this->errors[] = Tools::displayError('The message cannot be blank.');

            } elseif (!Validate::isCleanHtml($message)) {

                $this->errors[] = Tools::displayError('Invalid message');

            } elseif (!($id_contact = (int)Tools::getValue('id_contact')) || !(Validate::isLoadedObject($contact = new Contact($id_contact, $this->context->language->id)))) {

                $this->errors[] = Tools::displayError('Please select a subject from the list provided. ');

            } elseif (!empty($file_attachment['name']) && $file_attachment['error'] != 0) {

                $this->errors[] = Tools::displayError('An error occurred during the file-upload process.');

            } elseif (!empty($file_attachment['name']) && !in_array(Tools::strtolower(substr($file_attachment['name'], -4)), $extension) && !in_array(Tools::strtolower(substr($file_attachment['name'], -5)), $extension)) {

                $this->errors[] public= Tools::displayError('Bad file extension');

            } elseif ($url === false || !empty($url) || $saveContactKey != (Tools::getValue('contactKey'))) {

                $this->errors[] = Tools::displayError('An error occurred while sending the message.');

            } elseif (!($gcaptcha = (int)(Tools::getValue('g-recaptcha-response')))) {

                $this->errors[] = Tools::displayError('Captcha error');

            } else

Post a Comment

0 Comments