Okay
  Public Ticket #2813000
Get B2BGroupID in custom WP plugin
Closed

Comments

  •  8
    Martin Stolk started the conversation

    I would like to check in my WP PHP plugin to what B2B group the logged-in user belongs or check if a user is part of a specific B2B Group.

    - Is there a B2BKing function I can call to retrieve the B2B group ID and/or the B2B group's name? (Something like $groupID =  get_user_meta(get_current_user_group_ID(), true); )

    - Is there an overview of all B2BKing hooks and variables?

    Elaborated usage scenario: I have coded a custom free product offering plugin that should automatically add and remove some free products if certain conditions are met. These combinations are too complex too implement with the current B2BKing dynamic rules. Because they vary over the different B2B Groups, I need a way to distinguish these groups in my script and trigger different functions based on this information.

  •  2,217
    WebWizards replied

    Hi Martin,

    You can get the group id for a user with this line:

    $user_id = get_current_user_id();
    $group_id = get_user_meta($user_id, 'b2bking_customergroup', true);

    You can get the group name with this line:

    $group_name = get_the_title($group_id);

    Another thing: if this is a mixed b2b/b2c site, I would recommend you also check that the user is B2B. The final code would look like this:

    $user_id = get_current_user_id();
    $user_is_b2b = get_user_meta($user_id,'b2bking_b2buser', true);
    if ($user_is_b2b === 'yes'){
        $group_id = get_user_meta($user_id, 'b2bking_customergroup', true);
        $group_name = get_the_title($group_id);
    }


    Generally, for information about the meta keys B2BKing uses to set users / group and how to set users programmatically, please see the following article: https://woocommerce-b2b-plugin.com/docs/how-to-set-user-group-programmatically-during-import-in-bulk-with-scripts/


    Let me know if I can help with anything,

    Kind regards,

    Stefan