Developer Documentation: Hooks, Functions, Custom Code

Here 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 do check our documentation articles here as well: There are over 100 articles, some user-oriented, some dev-oriented, designed to help you achieve a large number 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 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 or not and returns true or false (bool)

Available functions:

  • b2bking()->is_b2b_user($user_id) - checks if 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 a 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 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 or not
  • 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 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). Read below about getting 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 being 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 logged out user
  • b2bking_reject_user_admin_before_delete ($user_id) - before user is deleted in the user approval reject function
  • b2bking_after_subaccount_created ($user_id) - after subaccount is created
  • b2bking_bulkorder_after_back_to_top - after "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 saved

Filters and their default value:

  • b2bking_force_cancel_api_requests (false) - tries to prevent b2bking from running during api requests
  • b2bking_allowed_file_types_text ('jpg, jpeg, png, txt, pdf, doc, docx') - controls the allowed files upload text
  • b2bking_allowed_file_types_size (5000000) - allowed file upload size, 5mb 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. May help show methods which are not zone based and otherwise don't show 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 rarely fix permalinks / redirect issues
  • b2bking_admin_customers_page_role (customer) - role or array of roles for which customers show in B2BKing -> Customers, by default only the 'customer' role shows
  • 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 that go through via 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) - apply dynamic rules to direct categories only, but not to subcategories or parents
  • b2bking_subaccounts_limit (1000) - limits how many subaccounts a user can create, by default set to 1000. Could also be used to allow different users different numbers of subaccounts
  • b2bking_search_results_number_order_form_cream (100) - default number of search results that show 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 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 set it 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 frontend text of 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 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 = 'newemail1@gmail.com, email2@yahoo.com';

	return $recipient;

}, 10, 2);

add_filter('b2bking_recipient_new_message_quote', function($recipient, $conversationid){
	
	$recipient = 'newemail1@gmail.com, email2@yahoo.com';

	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@gmail.com', 'Your custom message here...', get_current_user_id(), 0 );

In the above code all that needs to be changed is the email (email@gmail.com) and the message.

For example, here's how we can 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 their 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 (the ID is 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 (the ID is 45)

Display the user's group on the frontend #

If you are looking to show the user 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 specific content to specific groups or users only, please see this article

Use WP Roles with B2BKing #

If you are using 3rd party plugins which use WP Role instead of the B2BKing group, it is possible for B2BKing to sync the 2. For that, please see the wp roles sync article

Customize B2BKing Emails #

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

Import / Set Up Dynamic Rules Programmatically #

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

Import Users / Set Group Programmatically #

If you are importing users and you 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 such a thing, you must clear caches (See below)

Configure Subaccounts Programmatically #

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

Clear Plugin Caches Programmatically #

After you make any programmatic change to rules, prices, users, etc, you should also clear B2BKing 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 if you have any questions, you can contact us by opening a ticket at https://webwizards.ticksy.com

Powered by BetterDocs