Jump to content

How do I integrate reCaptcha v2 into Prestashop?


Walrus

Recommended Posts

Hi,

I added reCaptcha using a free 'eicaptcha' module. However, spammers are still sending messages from the contact form.

I have read you also need to add reCaptcha to the ContactController.php template, so tried doing it manually as module cannot do it.

However, the Google instructions say to add snippets before the closing </head> tag on your HTML template but I cannot find a head tag in contact-form.php, or in ContactController.php

Can someone please show by example how I may add the snippets to both of the above templates.

 

Thanks.

Link to comment
Share on other sites

1 hour ago, Rho_Bur said:

Easy peasy:

Get this free module (Thank the author) https://www.prestashop.com/forums/topic/442633-add-recaptcha-to-prestashop-version-16014-for-free/

Add your V2 keys in it's configuration and you're good to go.

This is the same module with that? https://github.com/nenes25/eicaptcha

Because this version does not need any manual editing in files

Edited by Prestan0ob (see edit history)
Link to comment
Share on other sites

13 hours ago, Rho_Bur said:

yes; no manual editing, just add your keys into the module's configuration page.

In Step 5 it says about coding...

Step 5) Open "contact.tpl" and find:

<div style="text-align:right;" class="submit">
<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>

For me it was on Line 141

 

Directly above this div place the following code: 

{if $captcha}
<p>
<script src='https://www.google.com/recaptcha/api.js'></script>
<div class="g-recaptcha" data-sitekey="YOUR_PUBLIC_KEY"></div>
</p>
{/if}
Link to comment
Share on other sites

As I said at the very top, I already had the free module but spammers can bypass the reCaptcha it adds. I found a tutorial to also modify ContactController but kept getting errors when I followed it. I was given help by a kind soul. I share it here and I have attached it for good measure.

 

HOW TO ADD GOOGLE RECAPTCHA TO PRESTASHOP

Paste following snippet before the closing </head> tag on your HTML template:

<script src='https://www.google.com/recaptcha/api.js'></script>

Prestashop version 1.7

In file: /public_html/themes/YOUR_THEME_NAME/modules/contactform/views/templates/widget/contactform.tpl

Right before: 

<footer class="form-footer text-xs-right">
      <input class="btn btn-primary" type="submit" name="submitMessage" value="{l s='Send' d='Shop.Theme.Actions'}">
    </footer>

Add following and your site/public key:
<div class="g-recaptcha" data-sitekey="YOUR_SITE_PUBLIC_KEY"></div>

—————————————————

In file: /public_html/modules/contactform

Right after:

        $notifications = false;
        if (Tools::isSubmit('submitMessage')) {

Add this:

if (($gcaptcha = (int)(Tools::getValue('g-recaptcha-response'))) == 0) {
                        $this->errors[] = $this->trans('Please complete reCaptcha', array(), 'Admin.Login.Notification');
                echo '<div class="col-xs-12 alert alert-danger">Please compete the reCaptcha</div>';
                        }

—————————————————

Prestashop version 1.6

In file: /public_html/controllers/front/ContactController.php

Lines 36—47 should read:

    public function postProcess()
    {
        if (Tools::isSubmit('submitMessage'))
        {
            $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.
            if (!($from = trim(Tools::getValue('from'))) || !Validate::isEmail($from))
                $this->errors[] = Tools::displayError('Invalid email address.');
            elseif (!($gcaptcha = (int)(Tools::getValue('g-recaptcha-response'))))
                $this->errors[] = Tools::displayError('Please use reCaptcha to verify you are not a robot.');
            elseif (!$message)

In file: /public_html/themes/YOUR_THEME_NAME/contact-form.tpl

Find towards the bottom of page:
        
<div class="submit" style="margin-top: 15px;">

Add above it and your site/public key:

        <div>
 <script src='https://www.google.com/recaptcha/api.js'></script>
 <div class="g-recaptcha" data-sitekey=“YOUR_SITE_PUBLIC_KEY”></div>
</div>
 

 

 

 

Add reCaptcha to 1.6 and 1.7.rtf

Edited by Walrus (see edit history)
Link to comment
Share on other sites

  • 4 weeks later...

Could You help me? I can't find this "

Cytat

 

Paste following snippet before the closing </head> tag on your HTML template:

<script src='https://www.google.com/recaptcha/api.js'></script>

 

"

I've prestashop 1.7 and I can't find my HTML template with </head> tag. Where should I look for? I was looking for it in "public_html/themes/my_template/templates" there I have index.php, index.tpl, page.tpl files but in none of them I can find the </head> tag. I' ve also been in "public_html/themes/my_template/templates/layouts but in any of the files that are there I found </head> tag. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...