Buy B2BKing
$199 $149
Note: This article is a work-in-progress. We are building a list of all major code snippets that can be used with B2BKing.
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:
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.
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.
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');
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);
You may want to display a quote request button directly on the product page, instead of only on the cart page. This can be achieved by adding the following PHP snippet to your site:
add_filter('b2bking_quote_button_product_page','__return_true');
The result would be a quote button, added to the single product page, as follows:
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);
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);
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');
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);
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);
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]); }
By default rules start with the regular price and will overwrite the sale price if there is one set. By adding the following code snippet, you can instead have discount rules start with the sale price, so that the discount and sale price stack.
add_filter('b2bking_discount_rules_start_with_sale_price','__return_true');
Normally discount rules start with the regular price of the product and recalculate the sale price. However, if the rule already has a sale price, that will get overwritten. You can make rules give the user the highest discount, either the rule price or the sale price, by adding the following snippet:
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){ $user_id = get_current_user_id(); $currentusergroupidnr = b2bking()->get_user_group($user_id); $b2b_regular_price = get_post_meta($product_id, 'b2bking_regular_product_price_group_'.$currentusergroupidnr, true ); $regular_price = get_post_meta($product_id,'_regular_price', true); if (!empty($b2b_regular_price)){ $regular_price = $b2b_regular_price; } $b2b_sale_price = get_post_meta($product_id, 'b2bking_sale_product_price_group_'.$currentusergroupidnr, true ); $sale_price = get_post_meta($product_id,'_sale_price', true); // get current sale price $current_sale_price = ''; if (!empty($b2b_sale_price)){ $current_sale_price = $b2b_sale_price; } else { if (!empty($sale_price)){ $current_sale_price = $sale_price; } } $current_sale_price = b2bking()->tofloat($current_sale_price); $regular_price = b2bking()->tofloat($regular_price); $rules = $results[0]; // if there is a sale price, choose largest discount between sale price and rule if (!empty($current_sale_price)){ if ($rule_type == 'discount_everywhere'){ foreach ($rules as $index => $rule_id){ // calculate rule sale price $type = get_post_meta($rule_id, 'b2bking_rule_what', true); $howmuch = b2bking()->tofloat(get_post_meta($rule_id, 'b2bking_rule_howmuch', true)); if ($type === 'discount_percentage'){ $rule_sale_price = $regular_price * (100-$howmuch)/100; } else { $rule_sale_price = $regular_price - $howmuch; } if ($rule_sale_price > $current_sale_price){ unset($rules[$index]); } } } } return array($rules, $results[1]); }
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:
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(); } } }
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);
By default the quantities in the order form are set to 1. You can make the cream order form quantities start at 0, by adding this code:
add_filter('b2bking_bulkorder_cream_value_final', function($val){ return 0; }, 10, 1); add_filter('b2bking_bulkorder_cream_min_final', function($val){ return 0; }, 10, 1);
By default, the bulk order form accounts for tiered pricing in its calculations, but does not show the tiered pricing directly. It is possible to add a column with tiered pricing, using this code snippet:
add_action('b2bking_bulkorder_cream_custom_heading', function(){ ?> <div class="b2bking_bulkorder_form_container_content_header_cream_sku"> <?php esc_html_e('Price', 'b2bking'); ?> </div> <?php }); add_action('b2bking_bulkorder_cream_custom_column', function($product){ ?> <div class="b2bking_bulkorder_cream_sku"><?php $post_id = $product->get_id(); $sale_price = $product->get_sale_price(); $sale_price = round(floatval(b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $sale_price))),2); $regular_price = $product->get_regular_price(); $regular_price = round(floatval(b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $regular_price))),2); if ($product->is_on_sale()){ $price_display = wc_format_sale_price($regular_price, $sale_price); } else { $price_display = wc_price($regular_price); } $user_id = get_current_user_id(); $user_id = b2bking()->get_top_parent_account($user_id); $currentusergroupidnr = apply_filters('b2bking_b2b_group_for_pricing', b2bking()->get_user_group($user_id), $user_id); $price_tiers = get_post_meta($post_id, 'b2bking_product_pricetiers_group_'.$currentusergroupidnr, true); $user_price = array(); $grpriceexists = 'no'; // if didn't find anything as a price tier + user does not have group price, give regular price tiers // if no tiers AND no group price exists, get B2C tiered pricing if ($currentusergroupidnr){ $grregprice = get_post_meta($post_id, 'b2bking_regular_product_price_group_'.$currentusergroupidnr, true ); $grsaleprice = get_post_meta($post_id, 'b2bking_sale_product_price_group_'.$currentusergroupidnr, true ); if (!empty($grregprice) && b2bking()->tofloat($grregprice) !== 0){ $grpriceexists = 'yes'; } if (!empty($grsaleprice) && b2bking()->tofloat($grsaleprice) !== 0){ $grpriceexists = 'yes'; } } if (empty($price_tiers) && $grpriceexists === 'no'){ $price_tiers = get_post_meta($product->get_id(), 'b2bking_product_pricetiers_group_b2c', true ); } $price_tiers = b2bking()->convert_price_tiers($price_tiers, $product); if (!empty($price_tiers) && strlen($price_tiers) > 1 ){ $price_tiers_array = explode(';', $price_tiers); $price_tiers_array = array_filter($price_tiers_array); // need to order this array by the first number (elements of form 1:5, 2:5, 6:5) $helper_array = array(); foreach ($price_tiers_array as $index=> $pair){ $pair_array = explode(':', $pair); $helper_array[$pair_array[0]] = b2bking()->tofloat($pair_array[1], 4); } ksort($helper_array); $price_tiers_array = array(); foreach ($helper_array as $index=>$value){ array_push($price_tiers_array,$index.':'.$value); } echo '<table class="b2bking_tier_table">'; foreach ($price_tiers_array as $pricetieroption) { $tier_values = explode(':', $pricetieroption); echo '<tr> <td class="b2bking_tier_row_table" style="white-space: nowrap;">' . $tier_values[0] . '+</td> <td class="b2bking_tier_row_table" style="white-space: nowrap;">' . wc_price($tier_values[1]) . '</td> </tr>'; } echo '</table>'; } else { echo $price_display; } ?></div> <?php }, 10, 1);
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:
You may want to redirect B2B and B2C users to different pages after login. It can be achieved with this snippet:
add_filter( 'woocommerce_login_redirect', 'custom_login_redirect', 1000000, 2 ); function custom_login_redirect( $redirect, $user ) { $is_b2b = get_user_meta($user->ID, 'b2bking_b2buser', true); if ( $is_b2b === 'yes' ){ $redirect = '/page1'; // B2B page here } else { $redirect = '/page2'; // B2C page here } return $redirect; }
In this example, B2B users will be redirected to site.com/page1 and B2C users will be redirected to site.com/page2
Powered by BetterDocs