Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Only accept certain emails in WooCommerce registration

  • 04-02-2018 02:02AM
    #1
    Registered Users, Registered Users 2 Posts: 11,257 ✭✭✭✭


    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, Registered Users 2 Posts: 7,120 ✭✭✭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, Registered Users 2 Posts: 11,257 ✭✭✭✭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