Code Snippets Library / List

Note: This article is a work-in-progress. We are building a list of all major code snippets that can be used with B2BKing.

Registration / Checkout #

Allow multiple file uploads during registration #

By default, B2BKing registration file uploads allow single files. You can make them allow multiple files by adding the following PHP code snippet to the site:

add_filter('b2bking_allow_file_upload_multiple','__return_true');

After adding that snippet, multiple files are accepted:

"Choose files" input after adding the above snippet

Set a default country in registration #

You can make B2BKing's registration default to a particular country, by adding the following PHP code snippet:

jQuery(document).ready(function(){
    jQuery('.b2bking_country_field_selector select').val('GR');
    jQuery('.b2bking_country_field_selector select').trigger('change');
});

The above uses 'GR' for Greece - you can change that to any other country code.

If you use a VAT field, adding this code snippet will also help show the VAT field by default, if the default country has VAT enabled.

Set a default VAT VIES validation prefix #

By default, when customers enter a VAT number for VIES validation, it is needed to enter the full number, including the prefix (e.g. DE123456789). You can make it optional to enter the prefix by setting a default prefix, through this snippet:

add_filter('b2bking_set_default_prefix_vat', function($prefix){
    return 'DE';
}, 10, 1);

After adding this, customers can enter both "DE123456789" and "123456789", and it will be processed the same way.

Quote Requests #

Disable Conversations, but Use Quote Requests #

By default, B2BKing groups conversations and quotes, and both have to be either enabled or disabled. It is possible however to disable conversations and keep using quote requests (exclusively through emails), by adding this following PHP code snippet:

add_filter('b2bking_my_account_menu_items', function($items){
    unset($items['conversations']);
    return $items;
}, 10, 1);
add_filter('b2bking_quote_requests_without_messaging','__return_true');
add_filter('b2bking_send_quote_email_logged_in_users','__return_true');

Redirect to a Specific Page After Quote Requests #

Normally when sending a quote request, the plugin will either refresh the page or redirect to the conversations URL. You may prefer to use a custom thank you page instead. To do that, add this snippet:

add_filter('b2bking_quote_request_url_redirect', function($url){    
​    return 'https://your-custom-link.com';
}, 10, 1);

You can set any URL in the above code.

You may want to also prevent the default "Your quote request has been received" message from showing. To remove that message, you can further add this snippet:

add_filter('b2bking_quote_request_success_message', function($val){
	return 0;
}, 10, 1);

Tiered Pricing #

Show QTY+ instead of quantity range #

By default the tiered pricing table shows quantity ranges, such as 1-10, 11-20, etc. You can make it show 1+, 11+ etc. instead, by adding this snippet:

add_filter('b2bking_tiered_pricing_table_show_direct_qty', function($val){
    return '+';
}, 10, 1);

Use lowest instead of highest range quantity #

By default, when a table row is clicked, the plugin selects the highest possible range quantity (in the input quantity field). You can make it use the lowest quantity instead, by adding this PHP code snippet to your site:

add_filter('b2bking_tiered_table_use_lowest_quantity', function($val){
	return 1;
}, 10, 1);

Sum up variations: use total cart quantity #

By default, the "sum up variations" feature counts all cart variations of the same product to determine the tiered pricing tier. You may want to make it count all site products instead. To do it, add this snippet:

add_filter('b2bking_tiered_pricing_uses_total_cart_qty','__return_true');

Sum up variations: use category quantity #

You may want to make sum up variations count all products in a specific category or categories. To do it, add this snippet:

add_filter('b2bking_tiered_pricing_count_total_qty', function($qty, $cart_item){

    // allowed category IDs, here you can configure categories that should be counted, by id
    $allowed_categories = array(15, 245);

    if (isset($cart_item['variation_id']) && intval($cart_item['variation_id']) !== 0){
        $current_product_id = $cart_item['variation_id'];
        $current_product_id = wp_get_post_parent_id($current_product_id);
    } else {
        $current_product_id = $cart_item['product_id'];
    }

    $allowed = false;
    foreach ($allowed_categories as $category_id){
        if( b2bking()->b2bking_has_taxonomy($category_id, 'product_cat', $current_product_id)){
            $allowed = true;
            break;
        }
    }

    if (!$allowed){
        $qty = 0;
    }

    return $qty;

}, 10, 2);

Dynamic Rules #

Show discounted prices as the regular price #

By default, dynamic discount rules will apply the price as a "sale price", showing both the old crossed out price, and the new sale price.

You can make these rules show only the new price, as a "regular price", without showing it as a discount or showing a discount badge. To do that, add the following snippet:

add_filter('b2bking_dynamic_recalculate_sale_price_badge', '__return_false');
add_filter('b2bking_dynamic_recalculate_sale_price_display', function($price_html, $product, $sale_price){
    return wc_price($sale_price);
}, 10, 3);

Discount rules ignore products with a sale price #

The following snippet applies to discount rules that have the "Calculated discounted price becomes sale price" checkbox enabled. If this snippet is added, these rules will ignore products that have a regular sale price set.

add_filter('b2bking_applicable_rules_products', 'b2bking_discount_rules_sale_price', 10, 5);
function b2bking_discount_rules_sale_price($results, $rule_type, $product_id, $user_id, $categories_array){
    $rules = $results[0];
    if ($rule_type == 'discount_everywhere'){
        // calculate discount percentage of rule
        $sale_price = get_post_meta($product_id,'_sale_price', true);
        if (!empty($sale_price)){
            $rules = array();
        }
    }
    
    return array($rules, $results[1]);
}

Purchase Lists #

Different theme for purchase lists #

By default, purchase lists display using the "classic" form theme. You can use the cream or indigo themes instead, by adding this PHP code snippet to your site:

add_filter('b2bking_purchase_list_display_theme', function($theme){
	return 'cream'; // cream or indigo here
}, 10, 1);

After adding the above, individual lists will display as follows:

Company Credit #

Give B2B customers cashback (credit) on purchases. #

You may want for example to give B2B customers a 1% credit cashback on all their orders. To achieve that, add the following code snippet to your site:


add_action('woocommerce_checkout_order_processed', function($order_id){
	$order = wc_get_order($order_id);
	$user_id = $order->get_customer_id();

	if (b2bking()->is_b2b_user()){
		$order_total = $order->get_total();
		$amount = $order_total / 100; // 1% credit


		$note = esc_html__('Credit for order ','b2bkingcredit').'#'.$order_id;
		// get user history
		$user_credit_history = sanitize_text_field(get_user_meta($user_id,'b2bking_user_credit_history', true));

		// create reimbursed transaction
		$date = date_i18n( 'Y/m/d', time()+(get_option('gmt_offset')*3600) ); 
		$operation = 'reimburse';
		$consumed_balance = get_user_meta($user_id,'b2bking_user_credit_consumed_balance', true);
		$new_consumed_balance = floatval($consumed_balance) - floatval($amount);		
		$transaction_new = $date.':'.$operation.':'.$amount.':'.$new_consumed_balance.':'.$note;

		// update credit history
		update_user_meta($user_id,'b2bking_user_credit_history',$user_credit_history.';'.$transaction_new);
		// update user consumed balance
		update_user_meta($user_id,'b2bking_user_credit_consumed_balance',$new_consumed_balance);

		$order->update_meta_data('added_extra_credit', $amount);
		$order->save();
	}
}, 10, 1);

add_action( 'woocommerce_order_status_changed', 'custom_order_status_change_action', 10, 4 );
function custom_order_status_change_action( $order_id, $old_status, $new_status, $order ) {
    if ( in_array( $new_status, array( 'cancelled', 'failed', 'refunded' ) ) ) {
    	$customer_id = $order->get_customer_id();
    	$credit = floatval($order->get_meta('added_extra_credit'));
    	if ($credit > 0){
    		$total = $credit;
    		// remove credit from order
    		$note = esc_html__('Cancelled credit for order','b2bkingcredit').' #'.$order_id;

    		// get user history
    		$user_credit_history = sanitize_text_field(get_user_meta($customer_id,'b2bking_user_credit_history', true));

    		// create transaction
    		$date = date_i18n( 'Y/m/d', time()+(get_option('gmt_offset')*3600) ); 
    		$operation = 'purchase';
    		$consumed_balance = get_user_meta($customer_id,'b2bking_user_credit_consumed_balance', true);
    		$new_consumed_balance = floatval($consumed_balance) + floatval($total);		
    		$transaction_new = $date.':'.$operation.':'.$total.':'.$new_consumed_balance.':'.$note;

    		// update credit history
    		update_user_meta($customer_id,'b2bking_user_credit_history',$user_credit_history.';'.$transaction_new);
    		// update user consumed balance
    		update_user_meta($customer_id,'b2bking_user_credit_consumed_balance',$new_consumed_balance);


    		$order->update_meta_data('added_extra_credit', 0);
    		$order->save();

    	}
    }
}

Order Form #

Speed improvements for Cream / Indigo Forms #

The following snippet can help improve speed when using the B2BKing Cream and Indigo order forms:

add_filter('b2bking_bulkorder_skip_sort', '__return_true');
add_filter('b2bking_search_results_number_order_form_cream', function($results){
    return 25;
}, 10, 1);
add_filter('b2bking_search_results_number_order_form_indigo', function($results){
    return 25;
}, 10, 1);

Miscellaneous #

Show assigned agent for each customer on their My Account page #

To show customers which sales rep is assigned to them, you can add the following code snippet:

add_action('woocommerce_account_dashboard', function(){
    // get current user's agent
    $user_id = get_current_user_id();
    $user_agent = get_user_meta($user_id,'salesking_assigned_agent', true);
    if (!empty($user_agent) && $user_agent !== 'none'){
        // get agent details
        $agent = new WP_User($user_agent);
        echo '<h4>Sales Representative</h4>';
        echo '<p>The following rep has been assigned to you:</p>';
        echo '<strong>Name:</strong> '.$agent->display_name.' ('.$agent->first_name.' '.$agent->last_name.')'.'<br>';
        echo '<strong>Email:</strong> '.$agent->user_email.'<br>';
        echo '<strong>Phone:</strong> '.get_user_meta($user_agent,'billing_phone', true);
    }
});

It will show as:

Powered by BetterDocs