Do you have a problem with a spam messages coming form your PrestaShop form? You can easily install recaptcha in a few easy steps.
Further reading: secure PrestaShop registration form with reCaptcha
This tutorial is dedicated for PrestaShop 1.7 but he have included remarks form PrestaShop 1.6.
Attention: this tutorial is dedicated for advanced users. You conduct all changes for your own risk.
Results of this tutorial can be seen below:
First you have to generate two type of keys (site and secret) which requires google account (if you gave gmail you also have google account). To generate keys go to this site (https://www.google.com/recaptcha/intro/index.html) and press get reCaptcha button. Next select reCAPTCHA v2 from a list and fill in all necessary fields.
Next step is to modify contact form file. To do so edit file themes/YOUR_THEME_NAME/modules/contactform/views/templates/widget/contactform.tpl.
Just before /section ending form-fields line add following code:
<div class="row">
<div class="col-xs-12 col-sm-12 offset-md-3 col-md-9">
<div class="g-recaptcha" data-sitekey="6LclChYTAAAAAGBPp4Wml_BkLS7k8fZpcppuOlc_"></div>
</div>
</div>
Data-sitekey has to be changed with a site key obtained from Google.
PrestaShop 1.6 remark: changes has to be done in a file contact-form.tpl just before form submit button.
In the top part of contactform.tpl file add recaptcha library:
{block name='head' append}
<script src='https://www.google.com/recaptcha/api.js'></script>
{/block}
PrestaShop 1.6 remark: add middle line to a file header.tpl
Now it is time for to override default form behaviour. To do so in main catalog search for override/modules. In this path add contactform catalog and inside it add contactform.php file. Now add short condition and a class, like mentioned below:
<?php
if (!defined('_PS_VERSION_'))
exit;
class ContactformOverride extends Contactform {
}
Please copy/paste sendMessage method from a parent class. Overwritten method has to be extended with a following condition:
elseif (!($gcaptcha = (Tools::getValue('g-recaptcha-response')))) {
$this->context->controller->errors[] = $this->trans('Please use captcha', array(), 'Shop.Notifications.Error');
and with below code (whole structure is shown on a printscreen)
//captcha verification
$googleVerificationUrl = 'https://www.google.com/recaptcha/api/siteverify';
$googleSec = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $googleVerificationUrl,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => [
'secret' => $googleSec,
'response' => $gcaptcha,
'remoteip' => $_SERVER['REMOTE_ADDR']
],
CURLOPT_RETURNTRANSFER => true
]);
$output = curl_exec($ch);
curl_close($ch);
$googleResponseObj = json_decode($output);
if($googleResponseObj->success !== true)
{
$this->context->controller->errors[] = $this->trans('Captcha verification failed', array(), 'Shop.Notifications.Error');
}
Value of the $googleSec has to be replaced with a secret key obtained from Google.
PrestaShop 1.6 remark: same changes have to be made in ContactController and postProcess method.
Last step is to refresh cache and recaptcha I ready to be tested.
Thanks to PrestaShop captcha you will decrease risk of bot attacks and spam.