Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Only accept certain emails in WooCommerce registration

Options
  • 04-02-2018 2:02am
    #1
    Registered Users Posts: 11,181 ✭✭✭✭


    Hi guys,
    Wondering if anyone can point me in the right direction to solve this issue. Long story short I need to allow users to signup only using a certain email address (let's say @yourcompany.com email address) in Woocommerce / registration page . if anyone else was to try and signup it would throw an error saying please use your correct email etc.

    Any ideas of how to do this?
    I was hoping there would be a plugin out there but cannot find one sadly.


    cheers.


Comments

  • Registered Users Posts: 6,079 ✭✭✭Talisman


    Here's a code snippet to perform the task in WordPress.
    function whitelist_email_check($errors, $sanitized_user_login, $user_email) {
      $whitelisted_domains = array('mydomain.com'); // whitelist email domains
      $valid_email = false;
      foreach ($whitelisted_domains as $valid_domain) {
        $length = strlen($valid_domain);
        $email_domain = strtolower(substr($user_email, -($length), $length));
        if ($email_domain == strtolower($valid_domain)) {
          $valid_email = true;
          break;
        }
      }
      if ($valid_email == false) {
        $errors->add('user_email_error', __('<strong>ERROR</strong>: The email address is not whitelisted.', 'some_textdomain'));
      }
      return $errors;
    }
    
    add_filter('registration_errors', 'whitelist_email_check', 10, 3);
    

    WooCommerce provides the woocommerce_register_post hook which takes different parameters ($username, $email, $errors) so you would have to tweak the code slightly.
    function wc_whitelist_email_check($username, $user_email, $errors) {
      $whitelisted_domains = array('mydomain.com'); // whitelist email domains
      $valid_email = false;
      foreach ($whitelisted_domains as $valid_domain) {
        $length = strlen($valid_domain);
        $email_domain = strtolower(substr($user_email, -($length), $length));
        if ($email_domain == strtolower($valid_domain)) {
          $valid_email = true;
          break;
        }
      }
      if ($valid_email == false) {
        $errors->add('email_error', __('<strong>ERROR</strong>: The email address is not whitelisted.', 'some_textdomain'));
      }
    }
    
    add_filter('woocommerce_register_post', 'wc_whitelist_email_check', 10, 3);
    

    Update the theme's functions.php file with the code snippet. Modify the whitelisted_domains array to check for the required email domains and you should be good to go.


  • Registered Users Posts: 11,181 ✭✭✭✭B.A._Baracus


    Thank you ever so much.
    You saved me so much stress of trying to get this to work. I owe you a beer! :)


Advertisement