Install Google Recaptcha v3 on the contact form prestashop 1.7.x.

Installing Google Captcha on the contact form prestashop 1.7.x.
Let’s consider the example of installing Google Recaptcha v3 on the prestashop contact form page. For example, let’s take the currently recent version of prestashop 1.7.6.5 with the standard classic theme. For convenience, you will also need the contactform module, which comes in the basic (boxed) version of prestashop.
 


So, let’s get started. To begin with, let’s take the necessary parameters of the captcha. You must have a Google account. Go to the https://www.google.com/recaptcha/intro/v3.html and click Admin console:

Captcha parameters in prestashop

Fill in the domain information. Copy the Site Key and Secret Key. You will only need these two parameters:

Captcha parameters for prestashop 1.7.x

For the convenience of implementing the functionality, we will use the contactform module, which comes in the basic version of prestashop:

contactform module in prestashop 1.7.x

This module processes requests from the contact form using the widget already installed.

So, find themesclassicmodulescontactformviewstemplateswidgetcontactform.tpl module template, which displays the contact form of the standard theme for the front office.

According to the instructions for installing the captcha, you need to insert the following lines into the template:

Insert them at the very beginning of the contactform.tpl file, before the section tag and slightly change, substituting the data (Site Key), which was obtained from the captcha settings. We will also specify action and input to transfer the token. The result is the following code:

To pass the token, let’s write input in the body of the form (after line 139):

So, the form is ready, the server processing remains. The request will be processed through modules/contactform/contactform.php. Let’s find the sendMessage function in it and change it by inserting the following code at the very beginning of the function:

Such code, which receives the token value from the submission form, sends it to the Google server for verification. After receiving a response from the server, an incorrect captcha message is displayed and the function processing is interrupted:

Wrong captcha in prestashop

Or continues in case of successful result:

Successful captcha in prestashop

Archive, with two files can be downloaded here. The contactform module version used = 4.1.1:

 

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Leave a comment

Your email address will not be published.