Public Ticket #2977309

User role vs group

Closed
Comments
33 Tickets 14
Johan started the conversation 5 years ago on February 9, 2022 at 12:59pm

Hi,

I can refer to Ticket #2620848.
The problem is as described, I need users in group to inherit the same role as the person who create the sub account. 
I found some code that kind of explained how to make iit work, but I could not manage it. 
Is there a way to make it work?

2,762
KingsPlugins replied 5 years ago on February 9, 2022 at 11:15pm

Hi Johan,

Thank you for following up on that ticket,

I didn't clearly understand if you need the WP Role or the B2BKing group (they're different). But anyway I wrote a snippet that should copy both from the parent user, to the subaccount user.

Please add this as a PHP code snippet to your site:

add_action('b2bking_after_subaccount_created', function($subaccount_id){
    // wp role
    $parent_id = get_user_meta($subaccount_id, 'b2bking_account_parent', true);
    $subaccount_obj = new WP_User($subaccount_id);
    $parent_obj = new WP_User( $parent_id );
    $roles = ( array ) $parent_obj->roles;
    $i = 1;
    foreach ($roles as $user_role){
        if ($i === 1){
            $subaccount_obj->set_role($user_role);
            $i++;
        } else {
            $subaccount_obj->add_role($user_role);
        }
    }
    // customer group
    $parent_group = get_user_meta($parent_id,'b2bking_customergroup',true);
    update_user_meta($subaccount_id,'b2bking_b2buser','yes');
    update_user_meta($subaccount_id,'b2bking_customergroup', $parent_group);
}, 10, 1);

A PHP snippet can be added to functions.php, or by following our guide here: https://b2bkingplugin.com/docs/how-to-add-a-snippet-php-or-js/

Let me know if that worked, and if there's anything I can help with,

Kind regards,

Stefan

1

Sign In