Public Ticket #3789718

group belongings

Closed
Comments
33 Tickets 14
Johan started the conversation 2 years ago on December 25, 2024 at 10:11am

Hi

Do you have a way to auto set group based on email address? 

Like if user has email using domain aaa.com or bbb.com, the set group to private1. And if user has domain ttt.com set group to private2 and so on? Do you think it is possible? Like this could be managed on registration, but still needs approval? Unfortunatelly I cannot let customers see options of groups at the registration page. 

2,762
KingsPlugins replied 2 years ago on December 27, 2024 at 1:21pm

Hello Johan,

I believe we can write a custom code snippet that checks the domain of the email, and then based on the domain assigns a specific group as you described.

I didn't fully understand what you are looking for regarding approval: do you still want to use manual approval? In that case would this only serve to pre-select a group before approval ? Or were you saying that you'd like to use automatic approval in the case of those specific email domains?

 

Kind regards,

Stefan

33 Tickets 14
Johan replied 2 years ago on December 30, 2024 at 12:08pm

Hi

Thanks for getting back. Yes, that is correct, it is for pre-select a group before approval. This way it makes it more fool proof since the "manager" of the site is not used to handle it. 

2,762
KingsPlugins replied 2 years ago on January 2, 2025 at 7:46pm

Great, thank you for confirming that,

To achieve this, please add the following PHP code snippet to your site:

add_action('user_register', 'check_email_domain_set_meta', 9999999, 1);
add_action('woocommerce_created_customer', 'check_email_domain_set_meta', 9999999, 1);

function check_email_domain_set_meta($user_id) {
    // Get the user email
    $user = get_userdata($user_id);
    $email = $user->user_email;
    
    $domain = substr(strrchr($email, "@"), 1);
    
    if (strtolower($domain) === 'gmail.com') {
        update_user_meta($user_id, 'b2bking_default_approval_manual', 'group_308');
    } elseif (strtolower($domain) === 'yahoo.com') {
        update_user_meta($user_id, 'b2bking_default_approval_manual', 'group_567');
    }
}

In this code there are 2 default options there "gmail.com" and "yahoo.com", which go to group IDs 308 and 567.

You would need to replace the email domains (the ending part of the email), as well as replace the group IDs.

 

The group ID of each group is the number in the URL when you edit that group in the backend (e.g. ?post=308)

 

Let me know if you have any issues with that,

Kind regards,

Stefan

Sign In