0

I am working on a wholesale site and want the site to automatically change the user role from customer to wholesale customer after login. This will allow the wholesale customers to see wholesale pricing. I have customized this code (below) and have added it in the Code Snippet plugin, but it doesn't seem to be working. Could anyone help me see what I'm missing?

    function uiwc_change_role()
    {
        // get WP_user object
        $user = wp_get_current_user();
        // if the this is a registered user and this user is not an admin
        if (false !== $user && !user_can($user, 'administrator')) {
            //set the new role to our customer
            $user->set_role('wholesale-customer');
        }
    }
    add_action('wp_login', 'uiwc_change_role', 100, 0);  

Thanks so much for any help you can provide!

1 Answer 1

0

With the wp_login hook the logged in user might not be set as the current user yet, so instead of using wp_get_current_user(), use the user object that's passed to the hook callback. Then you can be certain that you're setting the role on the correct user, rather than relying on the global state, which is unreliable. It is always preferable to use arguments passed to the callback over global state.

Also, using user_can() to check the role is discouraged. Internally user_can() uses $user->has_cap(), which has this note in its documentation:

While checking against a role in place of a capability is supported in part, this practice is discouraged as it may produce unreliable results.

Instead check $user->roles for the user's roles. I'd also consider checking if the user is a customer, rather than not an administrator.

With both of those points considered, your code would look like this:

add_action(
    'wp_login',
    function( $user_login, $user ) { // We want $user
        if ( in_array( 'customer', $user->roles ) ) {
            $user->set_role( 'wholesale-customer' );
        }
    },
    10,
    2 // Necessary for us to get $user
);

Another thing to consider is that if you're using the WooCommerce Wholesale Prices plugin, the user role is actually wholesale_customer, with an underscore, not a dash.

2
  • Hi, Jacob! Thank you so much for this. I tried it, using "wholesale_customer" because I am using the plugin, but it didn't work for me. I'm not sure why ... Someone else I spoke to yesterday suggested this code, and it worked: gist.github.com/kimhawleydesign/…. I know now from you that using 'user_can()' is discouraged, and I want to make sure I use the correct user object, as well. Do you see a way I could modify the new code so that it still works but maybe does it more correctly? Thank you again for your help!
    – Kim Hawley
    Sep 20, 2019 at 17:28
  • Just wanted to follow up on this, if you have a moment. I would like to update this code so that it not only works, but is as correct as possible. Any pointers you can provide would be very much appreciated.
    – Kim Hawley
    Sep 23, 2019 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.