Developer Documentation: Hooks, Functions, Custom Code

In this article, you can find a number of useful hooks, functions, snippets, and references to help you when working with B2BKing as a developer.

Please also check our documentation articles here: There are over 100 articles, some user-oriented and some dev-oriented, designed to help you accomplish a wide range of tasks.

Available Plugin Functions #

The plugin has a variety of global functions that you can use anywhere on your site. These can all be found in b2bking/includes/class-b2bking-global-helper.php

Example:

  • b2bking()->is_b2b_user($user_id) - checks if a user is B2B

Usage example:

  • if (b2bking()->is_b2b_user(123)) { }

The above example checks if the user with ID 123 is a B2B user and returns true or false (bool).

Available functions:

  • b2bking()->is_b2b_user($user_id) - checks if a user is B2B
  • b2bking()->get_user_group($user_id) - returns the group ID of a user
  • b2bking()->get_user_group_name($user_id) - returns the group name of a user
  • b2bking()->update_user_group($user_id, $group_id) - changes the user's group
  • b2bking()->get_woocs_price($price) - takes a price and applies the FOX (formerly WOOCS) currency conversion rate to get the correct final price. This function supports other currency plugins as well.
  • b2bking()->get_all_product_categories($product_id) - returns an array of all categories, including all parent categories of subcategories a product belongs to
  • b2bking()->clear_caches_transients() - clears B2BKing internal caches and transients
  • b2bking()->b2bking_clear_rules_caches() - clears the caches of B2BKing dynamic rules and regenerates them. This function should be called after making rule changes. If you work with rules via API, you should call this function at the end of your rule changes so the plugin's cache can process them.
  • b2bking()->get_all_rules($rule_type, $user_id) - gets all dynamic rules that apply to a specific user. $rule_type can be "all" or a specific type like "discount_percentage" or "fixed_price".
  • b2bking()->get_user_subaccounts($user_id) - returns an array of user IDs containing all subaccounts of a specific user
  • b2bking()->is_subaccount($user_id) - returns true or false depending on whether the user is a subaccount
  • b2bking()->get_user_company($user_id) - gets the user's company name, if any
  • b2bking()->get_user_phone($user_id) - gets the user's phone number, if any
  • b2bking()->is_approved($user_id) - returns true or false depending on whether the user is approved or waiting for approval
  • b2bking()->get_user_custom_field($user_id, $field_id) - gets the value of a custom field for a specific user (for example, it can be used for a registration field such as VAT to get the VAT number of that user). See below for how to get a field's ID.


Available Plugin Hooks #

There are a large number of action and filter hooks in the plugin, and more are being added all the time. Therefore, this is not a complete list—just a brief overview. If you're looking for specific hooks or areas, it's best to check the code directly or get in touch with our support team.

Action hooks:

  • b2bking_before_registration_approval
  • b2bking_after_user_registration_data ($user_id)

The above hooks are applied on the user profile page in the "Data collected at registration" panel.

  • b2bking_conversation_message_start - before a message is sent
  • b2bking_conversation_after_message_inserted ( $conversationid, $current_message_nr, $message ) - after a message has been sent

The above hooks are applied before and after a message is sent.

  • b2bking_before_send_quote_cart ($cart_items, $message, $user_id) - before a quote request is sent
  • b2bking_quote_logged_out_user ($guest_email) - quote sent by a logged-out user
  • b2bking_reject_user_admin_before_delete ($user_id) - before a user is deleted in the user approval reject function
  • b2bking_after_subaccount_created ($user_id) - after a subaccount is created
  • b2bking_bulkorder_after_back_to_top - after the "back to top" message in the order form
  • b2bking_purchase_list_created ($purchase_list_id, $user_id) - fires after a purchase list is created
  • b2bking_purchase_list_updated ($purchase_list_id, $user_id) - fires after a list is updated
  • b2bking_tiered_table_after_quantity ($product_id) - fires in the tiered price table, after the quantity
  • b2bking_tiered_table_after_price ($product_id, $price) - fires in the tiered price table, after the price
  • b2bking_after_registration_dropdown - fires after the "user type" dropdown on the registration page
  • b2bking_after_register_user_save_fields ($user_id) - fires after a user registration has finished being saved and custom fields have been stored

Filters and their default values:

  • b2bking_force_cancel_api_requests (false) - attempts to prevent B2BKing from running during API requests
  • b2bking_allowed_file_types_text ('jpg, jpeg, png, txt, pdf, doc, docx') - controls the allowed file upload text
  • b2bking_allowed_file_types_size (5000000) - allowed file upload size, 5 MB by default
  • b2bking_allowed_file_types (array( "image/jpeg", "image/jpg", "image/png", "text/plain", "application/pdf", "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/octet-stream" )) - allowed types
  • b2bking_registration_manual_approval_message ("Thank you for registering. Your account requires manual approval. Please wait to be approved.") - message that users see when they register and approval is needed
  • b2bking_use_zone_shipping_control (true) - disabling this via filter makes B2BKing use the non-zone-based shipping control. This may help show methods that are not zone-based and otherwise don't appear in B2BKing -> Groups
  • b2bking_use_wp_roles (false) - enabling this via filter makes B2BKing sync group and role — more info below
  • b2bking_show_sku_dynamic_rules (false)
  • b2bking_allow_file_upload_multiple (false)
  • b2bking_flush_permalinks (true) - disabling this can occasionally fix permalink/redirect issues
  • b2bking_admin_customers_page_role (customer) - role or array of roles for which customers appear in B2BKing -> Customers; by default, only the 'customer' role is shown
  • b2bking_cache_time_setting (1600) - B2BKing dashboard cache refresh time in seconds
  • b2bking_invoice_payment_order_status (on-hold) - can be used to change the initial order status of orders placed through the invoice gateway. For example, you may want to set it to 'processing'
  • b2bking_purchase_order_status (on-hold)
  • b2bking_apply_rules_to_direct_categories_only (false) - applies dynamic rules to direct categories only, not to subcategories or parents
  • b2bking_subaccounts_limit (1000) - limits how many subaccounts a user can create, set to 1000 by default. Can also be used to allow different users different numbers of subaccounts
  • b2bking_search_results_number_order_form_cream (100) - default number of search results shown in the Cream order form
  • b2bking_search_results_number_order_form_indigo (100)
  • b2bking_search_results_number_order_form (10) - default number of results in the classic order form
  • b2bking_allow_outofstock_order_form (false) - whether to show out-of-stock items in the order form
  • b2bking_seen_all_products_text ('You've seen all the products. Back to top')
  • b2bking_purchase_list_file_name ('b2bking_purchase_list) - file name used when downloading purchase lists
  • b2bking_cart_subtotal - (WC()->cart->get_subtotal()) - cart subtotal used in most dynamic rules. Can be modified to exclude or include tax by adding WC()->cart->get_subtotal_tax()
  • b2bking_addtax_order_total_calculation_basis - (WC()->cart->cart_contents_total) - cart subtotal used in add-tax rules
  • b2bking_rounding_precision (2) - internal rounding precision of B2BKing
  • b2bking_enable_user_cookies (true) - can be set to false to remove the b2bking_user_type cookie
  • b2bking_remove_tiered_table_quote_mode (true) - can be set to false to show the tiered table in quote mode
  • b2bking_save_as_purchase_list_text ("Save as purchase list") - changes the frontend text of the save list button
  • b2bking_csvorder_add_text ("Add to cart")

Add a Custom Column on the Backend B2BKing Customers Page #

Here's how to add a phone number column:

add_action('b2bking_b2bcustomers_column_header', function(){
	echo '<th>Phone Number</th>';
});

add_action('b2bking_b2bcustomers_column_footer', function(){
	echo '<th>Phone Number</th>';
});

add_filter('b2bking_b2bcustomers_row_content', function($row, $user_id){
	$phone = get_user_meta($user_id,'billing_phone', true);
	if (empty($phone)){
		$phone = get_user_meta($user_id,'shipping_phone', true);
		if (empty($phone)){
			$phone = '-';
		}
	}
	$column = '<td>'.$phone.'</td>';

	if (is_array($row)){
		array_push($row, $phone);
	} else {
		$row.=$column;
	}


	return $row;
}, 10, 2);

Change the Admin Email That Receives Messages / Quotes #

By default, emails for messages and quote requests go to the email configured on the admin side. To change that or add multiple emails:

add_filter('b2bking_recipient_new_message', function($recipient, $conversationid){
	
	$recipient = '[email protected], [email protected]';

	return $recipient;

}, 10, 2);

add_filter('b2bking_recipient_new_message_quote', function($recipient, $conversationid){
	
	$recipient = '[email protected], [email protected]';

	return $recipient;

}, 10, 2);

Send a Custom Email Programmatically #

B2BKing has a built-in "New message" email that is quite flexible and can be used with custom code to send an email in various situations. Here's how to fire an email:

do_action( 'b2bking_new_message', '[email protected]', 'Your custom message here...', get_current_user_id(), 0 );

In the above code, all that needs to be changed is the email ([email protected]) and the message.

For example, here's how to send an email to a subaccount when their account is first created:

add_action('b2bking_after_subaccount_created', function($user_id){
	$user = new WP_User($user_id);
	do_action( 'b2bking_new_message', $user->user_email, 'Congratulations on your new account ...', get_current_user_id(), 0 );
}, 10, 1);

The above code will send them a welcome email.

How to Check if a User Is B2B #

Use the b2bking()->is_b2b_user($user_id) function described above.

Get the Current User's Group #

Use b2bking()->get_user_group($user_id) to get the group ID.

Use b2bking()->get_user_group_name($user_id) to get the group name.

Get a Group ID, Rule ID, User ID, Field ID, etc. #

In B2BKing documentation, we frequently refer to a rule ID or field ID, etc. This is the post ID of a post, as all B2BKing items like rules or fields are custom post types.

To get the rule ID of a dynamic rule, go to B2BKing -> Dynamic Rules and click on the rule. The number in the URL such as ?post=123 is the ID (123 in that case).

To get the field ID of a B2BKing custom field, go to B2BKing -> Registration Fields and click on the field. The number in the URL such as ?post=45 is the ID (45 in that case).

Display the User's Group on the Frontend #

If you're looking to show users their group and perhaps a total spent or progress made, please see this article.

Show Content or Elements / CSS for B2C/B2B Users Only #

If you're looking to show specific elements or content to specific groups or users only, please see this article.

Use WP Roles with B2BKing #

If you are using third-party plugins that rely on WP roles instead of the B2BKing group, it is possible for B2BKing to sync the two. For details, please see the WP roles sync article.

Customize B2BKing Emails #

To modify B2BKing emails—either their content or their styling—please see the linked article above.

Import / Set Up Dynamic Rules Programmatically #

If you'd like to import dynamic rules programmatically, in bulk, or via the REST API, please see the linked article above.

Import Users / Set Group Programmatically #

If you are importing users and need to set a specific user group during import, please see this article.

Set B2B Pricing Programmatically #

If you need to set B2B prices for a product programmatically, please see this article for the appropriate product meta keys.

Importantly, after doing so, you must clear caches (see below).

Configure Subaccounts Programmatically #

If you need to configure or import subaccounts programmatically, please see the linked article, which describes the user meta key structure needed.

Clear Plugin Caches Programmatically #

After making any programmatic changes to rules, prices, users, etc., you should also clear B2BKing's internal caches by calling this function:

b2bking()->clear_caches_transients();

If you make programmatic changes or additions to dynamic rules, you should also call:

b2bking()->b2bking_clear_rules_caches();

Need Help? #

If you need help with anything or have any questions, you can contact us by opening a ticket at https://webwizards.ticksy.com

Powered by BetterDocs