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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Only accept certain emails in WooCommerce registration

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


    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: 6,283 ✭✭✭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,217 ✭✭✭✭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