How can I limit the age of my customers to people over 18?

Selling age-restricted products

There is no certain way to check on a visitor’s age, but you can check that the date of birth that they have given you would make them at least 18 (or any other age you choose).

To do this create an override file in your template for includes/modules/create_account.php.

Find the section of code that reads

  if (ACCOUNT_DOB == 'true') {
    // ... additional checks here 
    if (ENTRY_DOB_MIN_LENGTH > 0 or !empty($_POST['dob'])) {
      if (substr_count($dob,'/') > 2 || checkdate((int)substr(zen_date_raw($dob), 4, 2), (int)substr(zen_date_raw($dob), 6, 2), (int)substr(zen_date_raw($dob), 0, 4)) == false) {
        $error = true;
        $messageStack->add('create_account', ENTRY_DATE_OF_BIRTH_ERROR);
      }
    }
  }

and expand it to (setting the minimum age to whatever you wish)

  if (ACCOUNT_DOB == 'true') {
    // ... additional checks here 
    if (ENTRY_DOB_MIN_LENGTH > 0 or !empty($_POST['dob'])) {
      if (substr_count($dob,'/') > 2 || checkdate((int)substr(zen_date_raw($dob), 4, 2), (int)substr(zen_date_raw($dob), 6, 2), (int)substr(zen_date_raw($dob), 0, 4)) == false) {
        $error = true;
        $messageStack->add('create_account', ENTRY_DATE_OF_BIRTH_ERROR);
      }
      $minimum_age = 18;
      $acceptable_dob = (date('Y') - $minimum_age) . date('md');
      if (zen_date_raw($dob) > $acceptable_dob) {
        $error = true;
        $messageStack->add('create_account', ENTRY_DATE_OF_BIRTH_UNDERAGE);
      }               
    }
  }

Then create an override file for your includes/languages/english/create_account.php file and place the following line in there, changing the text to suit your style of feeding errors back to visitors

define('ENTRY_DATE_OF_BIRTH_UNDERAGE','Sorry, but you must be at least 18 to register to use this site');



Still have questions? Use the Search box in the upper right, or try the full list of FAQs. If you can't find it there, head over to the Zen Cart support forum and ask there in the appropriate subforum. In your post, please include your Zen Cart and PHP versions, and a link to your site.

Is there an error or omission on this page? Please post to General Questions on the support forum. Or, if you'd like to open a pull request, just review the guidelines and get started. You can even PR right here.
Last modified August 28, 2020 by Scott C Wilson (909e3a69).