Okay
  Public Ticket #3302117
Assigning additional roles to new Amelia Cusomer
Closed

Comments

  • Danny started the conversation

    Hi team,

    I'm working with a client that has several different booking forms. Some of the booking forms don't require payment and they are to be used only by her corporate or education clients (whose payment is managed separately).

    The employees or teachers are given a link and password to a password protected page on her site to access the booking form for their particular organisation.

    I want to be able to perform actions such as showing or hiding certain links, content and other areas of the site based on whether it's an education client or a business client.

    I'm exploring user roles to achieve this, and so far I have created two new user roles: 'education' and 'business'. These are duplicates of the 'Amelia Customer' role.

    I have found a snippet that takes the domain name of the person – the email address they enter when using a booking form – and assigns that user to a specific user role:

    add_action( 'user_register', 'set_role_by_email' );
    function set_role_by_email( $user_id ){
        $user = get_user_by( 'id', $user_id );
        $domain = substr(
            strrchr(
                $user->data->user_email, 
                "@"
            ), 1
        ); //Get Domain     $business_domains = array( 'business1.co.uk', 'business2.com' );
        if( in_array( $domain, $business_domains ) ){
            foreach( $user->roles as $role )
                $user->remove_role( $role ); //Remove existing Roles
            $user->add_role( 'business' ); //Add role
        }     $education_domains = array( 'school.org.uk' );
        if( in_array( $domain, $education_domains ) ){
            foreach( $user->roles as $role )
                $user->remove_role( $role ); //Remove existing Roles
            $user->add_role( 'education' ); //Add role
        } }

    I can confirm this code works when creating a new user from the backend, and through the frontend using the default Wordpress register form.

    However, this doesn't seem to be firing when a Wordpress user is created via the Step by Step 2.0 booking form.

    I think I'm missing a hook somewhere along the line. I did have a quick scan of the Amelia source code and docs to see if there were any hooks that could help me achieve this, but I couldn't find anything.

    As far as I understand, when a customer without an account creates a booking, Amelia takes their details, creates an Amelia Customer and then creates a Wordpress user with the 'Amelia Customer' role assigned.

    Is there a way of being able to hook into this process, and add or replace the user role if an account is created via the booking form? And if so, an explanation of how this could work would be appreciated.

    Thanks!

    Danny

  • Danny replied

    Of course, now I've spent that time typing out my issue I think I have resolved it!

    I found a file in the Amelia source code CreateWPUser.php that indicated how Amelia creates a WP user.

    Because Amelia doesn't use the 'user_register' function when generating a Wordpress user, I've had to update my hook to 'add_user_role' instead.

    I've also had to disable the 'remove_role' from the code as it conflicted with Amelia's function, and didn't show me the 'booking complete' step on the form.

    One caveat to this – it will simply add another user role to your users, and not remove the 'Amelia Customer' role. But I think this may be for the best in terms of compatibility with Amelia and preventing any conflicts.

    For my purposes, this works totally fine. Although having a dedicated hook for Amelia would be great to prevent any conflict issues in the future. I've kept this thread open for the public, in case anyone is having a similar problem.

    Here's my updated code:

    add_action( 'add_user_role', 'set_role_by_email' );
    function set_role_by_email( $user_id ){
        $user = get_user_by( 'id', $user_id );
        $domain = substr(
            strrchr(
                $user->data->user_email, 
                "@"
            ), 1
        ); //Get Domain
        $business_domains = array( 'business1.co.uk', 'business2.com' );
        if( in_array( $domain, $business_domains ) ){
            foreach( $user->roles as $role )
            $user->add_role( 'business' ); //Add role
        }
        $education_domains = array( 'school.org.uk' );
        if( in_array( $domain, $education_domains ) ){
            foreach( $user->roles as $role )
            $user->add_role( 'education' ); //Add role
        }
    }

    Thanks,

    Danny

  • Danny replied

    Got some bonus code here as well for anyone who wants to explicitly assign an additional user role if the user's email address domain does not match a domain found in the previous two arrays.

    This can be handy if you want to show information specifically to logged in users who are not (in my case) 'business' or 'education' clients.

    add_action( 'add_user_role', 'set_role_by_email' );
    function set_role_by_email( $user_id ){
        $user = get_user_by( 'id', $user_id );
        $domain = substr(
            strrchr(
                $user->data->user_email, 
                "@"
            ), 1
        ); /* Get Domain */
        
        /* Define the email domains that will have the 'business' user role assigned */
        $business_domains = array( 'business1.com', 'business2.co.uk' );
        if( in_array( $domain, $business_domains ) ){
            foreach( $user->roles as $role )
            $user->add_role( 'business' ); //Add role
        }
        /* Define the email domains that will have the 'education' user role assigned */
        $education_domains = array( 'school.org.uk' );
        if( in_array( $domain, $education_domains ) ){
            foreach( $user->roles as $role )
            $user->add_role( 'education' ); //Add role
        }
        
        /* Assign a custom user role called 'personal' if the email domain does not match any of the domains defined in 'business' or 'education' */
        $both_types = array_merge($business_domains, $education_domains);
        if( !in_array( $domain, $both_types ) ){
            foreach( $user->roles as $role )
                $user->add_role( 'personal' ); //Add role
        }
        
    }
    
  •  1,171
    Uroš replied

    Hello Danny,

    Thank you for reaching out to us and for sharing this with us.

    I will have this forwarded to our developers and this is a public ticket, thus the other users can also see this solution.

    Please let me know if you have any other questions.


    Kind Regards, 

    Uros Jovanovic
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables