Subaccounts - Multi-User Accounts & Metadata/API Info

B2BKing introduces Subaccounts as a way of providing a multiple buyers per account system.

(New!) Log in as subaccounts #

Starting with B2BKing 4.7.0, the main account can click to log in as each subaccount:

Login as each subaccount
Switch back to the main account

If you would like to disable this feature, you can add the following snippet to your site:

add_filter('b2bking_allow_subaccount_login','__return_false');

Why have multiple buyers per account? #

In B2B e-commerce, companies often need to have multiple employees that have access to an account, to see prices, place orders, communicate with the rest of the team, etc.

These employees may or may not need permission to access various features within the account. For instance, a company may want a user to have access to the catalog and prices, but not have permission to place orders.

B2BKing introduces a subaccounts system integrated with permissions that enables and extends WooCommerce with a powerful multiple buyers per account integration.

What is a subaccount? #

A subaccount is an individual account with its own username and password, connected to the main company account. The main account admin can create, edit and delete subaccounts. A subaccount has permissions, including: permission to buy, permission to view orders, offers, purchase lists, conversations, etc. It is also possible to enable company order approval, so that subaccounts can place orders, but these orders require approval from the main account before going through.

How subaccounts are created and edited in B2BKing #

B2BKing adds a "Subaccounts" section to users' My Account area in WooCommerce. Here subaccounts can be added, edited and deleted.

Subaccounts section added to My account in WooCommerce

Subaccounts view in "Customers" section #

You can easily view and organize accounts and subaccounts through B2BKing's dedicated "Customers" section. Here you can search for users by company name, account type or customer group.

"Customers" view in B2BKing's admin backend

Creating a subaccount #

To create a subaccount, you must enter login details (username, email, password), personal details of the account user, and set what permissions the subaccount has.

View: Creating a subaccount

Whether or not the username and password are available here as fields, depends on your settings in WooCommerce -> Settings -> Accounts & Privacy -> Account creation. View settings.

Subaccount functionalities #

Subaccounts can access the catalog and view the same products, prices, offers, lists, etc as the main company account, if they have permission to access each respective area.

B2BKing adds a "Placed by" column in Orders, to show who placed any individual order within an account. B2BKing also allows full control over whether subaccounts have permissions to view other account orders.

"Placed by" column in Orders

Another example of this implementation is in conversations. Multiple users can participate in each conversation, and the message's author is attached to each message.

Multiple buyers per account in conversations

Permissions system #

An account owner can choose for each subaccount what permissions and level of access the subaccount should have. Permissions include:

  • Place an order
  • Orders require approval (part of the company order approval feature)
  • View all account orders
  • View all account offers
  • View all account conversations
  • View all account purchase lists
  • View all account subscriptions (WooCommerce Subscriptions plugin is needed)
Permissions of each subaccount

Enabling and disabling subaccounts #

The subaccounts functionality can be enabled or disabled through a dedicated setting in B2BKing's settings panel.

Enable subaccounts setting in B2BKing's Settings panel

Set existing users as subaccounts - Admin control #

It is possible to set existing users as subaccounts, by using the tools in B2BKing -> Tools -> User Editor:

There you can find 2 separate tools:

Set users as subaccounts of account:

This tool accepts the user IDs of parent and sub-accounts, and connects the 2 when clicking the button.

Turn subaccounts into regular accounts:

This tool likewise accepts user IDs, and removes the subaccount status of the accounts, making them normal user accounts.

Multiple Levels of Subaccounts #

By default, there are only 2 levels of subaccounts, meaning that a company account can create subaccounts, but subaccounts cannot create their own (further) subaccounts.

It is possible to enable a 3rd level of subaccounts, by adding this PHP code snippet to your site:

add_filter('b2bking_allow_multiple_subaccount_levels','__return_true');

Meta Data / Programmatic control over subaccounts: #

-> The subaccounts B2BKing introduces are regular WordPress / WooCommerce accounts, with the relationships between accounts and account permissions defined through user meta data (defined in the WordPress wp_usermeta table ).

Therefore subaccount relationships can be fully managed through user meta data, which can be done through the WP or the WooCommerce API.

-> The UI is just a tool that controls this metadata. It can be completely removed, as long as you set / edit the meta data in a different way.

------------

Here are all the relevant meta keys and values which determine relationships and subaccount permissions:

A) Meta Keys:

Each subaccount needs the following 2 meta keys:

  • "b2bking_account_type" must be set to "subaccount" - this tells the plugin the user account is a subaccount
  • "b2bking_account_parent" must be set to the WordPress USER ID of the parent account (e.g. 123)

Each parent account must have the following meta key:

  • "b2bking_subaccounts_list" must contain a comma-separated list of child subaccount IDs (e.g. 123, 54, 333)

Permissions are set for each subaccount through these keys:

  • b2bking_account_permission_buy
  • b2bking_account_permission_buy_approval
  • b2bking_account_permission_view_orders
  • b2bking_account_permission_view_offers
  • b2bking_account_permission_view_conversations
  • b2bking_account_permission_view_lists
  • b2bking_account_permission_view_subscriptions

These keys can have the value of 0 (disabled) or 1 (enabled).

​​​

B) Functions:

You can use the following function to turn an account into the subaccount of another:

/*
This snippet connects 2 accounts in B2BKing, making one account the subaccount of another.
$parent_account_id should be the main account ID
$subaccount_account_id needs to be set to the desired subaccount ID
*/
function b2bking_connect_subaccount($parent_account_id, $subaccount_account_id){
    update_user_meta($subaccount_account_id,'b2bking_account_type', 'subaccount');
    update_user_meta($subaccount_account_id,'b2bking_account_parent', $parent_account_id);
    
    $current_subaccounts_list = get_user_meta($parent_account_id,'b2bking_subaccounts_list', true);
    update_user_meta($parent_account_id,'b2bking_subaccounts_list', $current_subaccounts_list.','.$subaccount_account_id);
    // the following code sets subaccount permissions (1 = enabled 0 = not enabled)
    // enable all permissions for subaccount
    update_user_meta($subaccount_account_id, 'b2bking_account_permission_buy', 1);
    update_user_meta($subaccount_account_id, 'b2bking_account_permission_view_orders', 1); 
    update_user_meta($subaccount_account_id, 'b2bking_account_permission_view_offers', 1); 
    update_user_meta($subaccount_account_id, 'b2bking_account_permission_view_conversations', 1); 
    update_user_meta($subaccount_account_id, 'b2bking_account_permission_view_lists', 1); 
}

Usage example:

b2bking_connect_subaccount(12, 456);
// in this example 456 is the subaccount ID, and 12 is the main account ID

Powered by BetterDocs