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 people that have access to an account, to see prices, order, communicate, etc. These people may or may not need to have permission and access to various feature within the account.

For instance, a company may want a user to have access to 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. An 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.

How subaccounts are created and edited in B2BKing #

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

Subaccounts section added in 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 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 choose login details (username, email, password), personal details of the account user, and set what permissions the account has.

Creating a subaccount view

How subaccounts work - Customizations to WooCommerce #

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
  • View all account orders
  • View all account offers
  • View all account conversations
  • View all account purchase lists
Permissions of each subaccount

Enabling and disabling subaccounts #

Subaccounts can be enabled or disabled through a dedicated setting in B2BKing's Settings panel.

Enable subaccount setting in B2BKing's Settings panel

Meta Data / Programmatic control over subaccounts: #

-> The subaccounts our plugin uses are just regular WordPress/WooCommerce accounts, with the relationships between accounts and account permissions defined through USER META DATA ( in the wordpress wp_usermeta table ).

Therefore the B2BKing aspect of subaccounts can be fully managed if you have access to user meta data, which should be possible via the WP or the WooCommerce API - and is possible to manipulate at any major WP action hook such as 'plugins_loaded'.

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

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

Here are all the relevant meta data for 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 this 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 with these keys:

-  b2bking_account_permission_buy

b2bking_account_permission_view_orders

b2bking_account_permission_view_offers

b2bking_account_permission_view_conversations

b2bking_account_permission_view_lists

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

​​​​​

B) Functions:

You can use this function to make an account to be a subaccount of another

/*
This snippet connects 2 accounts in B2BKing, making an account a subaccount of the other.
$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