Jump to content

PHP age restriction on creating account PrestaShop 1.7.6.2


AleShey

Recommended Posts

Hi everyone, I'm finding some difficulties on making a restricted registration for user under 18 on an ecommerce. Problem is that PS 1.7's architecture has changed from 1.6 so all the solutions I've found are not working. For example I tried to replace  the function isBirthDate with the following (found in a topic on this forum): 

public static function isBirthDate($date, $format = 'Y-m-d')
{
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 also tried to integrate it with the existing code but, even tho apparently it was working , I can't understand if it really checks the age now but apparently it completely breaks the format and doesn't recognize it anymore, cause even if I put the right converted format DD/MM/YYYY or any kind of format it gives me an error which states that the format is wrong.

Now this is the "stock" code of the function:

public static function isBirthDate($date, $format = 'Y-m-d')
    {
        if (empty($date) || $date == '0000-00-00') {
            return true;
        }

        $d = DateTime::createFromFormat($format, $date);
        if (!empty(DateTime::getLastErrors()['warning_count']) || false === $d) {
            return false;
        }

        return $d->getTimestamp() <= time();
    }

I'd also like to know if that "return true" is right or not. Cause I think it should return false for that condition.

TL;DR: I can't find a way to make age restricted registration in a prestashop ecommerce 1.7.6.2 via php ( so no modules/addon to buy). I tried old solutions but obviously they won't work.

Thanks for your attention.

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

  • 1 month later...
  • 1 year later...
  • 6 months later...

just make your override for classes/Validate.php with the next content...

<?php

class Validate extends ValidateCore
{

public static function isBirthDate($date, $format = 'Y-m-d')
    {
        if (empty($date) || $date == '0000-00-00') {
            return true;
        }

        $d = DateTime::createFromFormat($format, $date);
        if (!empty(DateTime::getLastErrors()['warning_count']) || false === $d) {
            return false;
        }
        
        $d->add(new DateInterval("P18Y"));
        return $d->setTime(0, 0, 0)->getTimestamp() <= time();
    }
}

 

you could see a new line in 1.7 function..

$d->add(new DateInterval("P18Y"));

remember to clear cache.. and now will work fine your +18 registration restriction for prestashop 1.7..

other thing will be change or add a related new alert..

PD: not my work, original reply at https://stackoverflow.com/questions/68268953/validate-a-prestashop-user-registration-field

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