Jump to content

can this age restriction override be modified to work with recent versions?


Truffelstunter

Recommended Posts

found this and treid to get it work

probably for an old version of prestashop. But it does excatly what i need, so if anybody kknow how to modify it so it should work on recent prestashopversions i would be very grateful and i think more people will also!

 

In this tutorial, we show a code to restrict the user the age of 18 as the minimum age.

To do this, the validation class (class / Validation.php) that birthday check is used. But we use the override to avoid modifying core files of PrestaShop.

First, create a new php file called Validation.php, and put this content:

class Validate extends ValidateCore {
public static function isBirthDate($date)
{
if (empty($date) || $date == '0000-00-00')
return false;
if (preg_match('/^([0-9]{4})-((?:0?[1-9])|(?:1[0-2]))-((?:0?[1-9])|(?:[1-2][0-9])|(?:3[01]))([0-9]{2}:[0-9]{2}:[0-9]{2})?$/', $date, $birth_date))
{
if ((floor((time() - strtotime($date))/31556926))<18)
return false; 
if ($birth_date[1] > date('Y') && $birth_date[2] > date('m') && $birth_date[3] > date('d'))
return false;
return true;
}
return false;
}
}

 

and copy this file to overrides/classes directory of PrestaShop. After that, delete the file cache/class_index.php to load the new override and thats all

Link to comment
Share on other sites

I just tested this on my PrestaShop v1.6.1.7 test site and it works fine. It did have a syntax error that disappeared after I reformatted the code though. Here's what I have in override/classes/Validate.php:

<?php

class Validate extends ValidateCore
{
    public static function isBirthDate($date)
    {
        if (empty($date) || $date == '0000-00-00')
            return false;

        if (preg_match('/^([0-9]{4})-((?:0?[1-9])|(?:1[0-2]))-((?:0?[1-9])|(?:[1-2][0-9])|(?:3[01]))([0-9]{2}:[0-9]{2}:[0-9]{2})?$/', $date, $birth_date)) {
            if ((floor((time() - strtotime($date)) / 31556926)) < 18)
                return false;
                
            if ($birth_date[1] > date('Y') && $birth_date[2] > date('m') && $birth_date[3] > date('d'))
                return false;

            return true;
        }

        return false;
    }
}

I then went and tried to register a new account with a birth date that made me under 18 years. At first I thought it wasn't working, but then I realised that the "Invalid date of birth" message doesn't appear unless every other field is correct.

 

You can go to Localization > Translations > Error message translations to change the error message to something more friendly like "You must be over 18 years old to register.".

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...