Buy B2BKing
$299 $199
In previous articles, we've discussed how to show prices including tax for B2C and excluding tax for B2B.
This article is NOT about that. In this article, we will discuss how to set different tax rates for B2B and B2C users. For example, this could mean a B2B user would pay a 10% tax in Quebec, while a B2C user would pay 15%. This method also allows you to set different taxes for each city, country, postcode, etc.
Let's work through an example. Follow these steps:
1) First, go to WooCommerce -> Settings -> Tax and add 'B2B' as an additional tax class:
After saving the settings, this creates an additional tax tab for 'B2B Rates':
2) Now add the following PHP code snippet:
add_filter( 'woocommerce_product_get_tax_class', 'wc_diff_rate_for_user', 10000, 2 ); add_filter( 'woocommerce_product_variation_get_tax_class', 'wc_diff_rate_for_user', 10000, 2 ); add_action('woocommerce_checkout_create_order_line_item', 'save_tax_class_to_order', 20, 4); function wc_diff_rate_for_user( $tax_class, $product ) { $user_id = get_current_user_id(); $is_b2b = get_user_meta($user_id,'b2bking_b2buser', true); if ($is_b2b === 'yes'){ $tax_class = 'B2B'; } return $tax_class; } function save_tax_class_to_order($item, $cart_item_key, $values, $order){ $user_id = get_current_user_id(); $is_b2b = get_user_meta($user_id,'b2bking_b2buser', true); if ($is_b2b === 'yes'){ $tax_class = 'b2b'; // lowecase $item->set_tax_class($tax_class); } } add_filter( 'b2bking_taxable_fee_tax_class', function($val){ $user_id = get_current_user_id(); $is_b2b = get_user_meta($user_id,'b2bking_b2buser', true); if ($is_b2b === 'yes'){ $val = 'B2B'; } return $val; });
This ensures that the B2B rate is selected for B2B users (B2B users as defined by the B2BKing plugin = part of a B2B group).
3) To test this, in WooCommerce -> Settings -> Tax -> B2B Rates, set a 10% rate for Canada:
Then set a 20% standard rate for Canada (let's say this is the B2C rate):
Now let's test:
I add products worth $1,000 to the cart.
When I'm logged in as a B2C user, the standard rate (20%) is applied and the final cost is $1,200.
When I'm logged in as a B2B user, the B2B rate (10%) is applied and the final cost is $1,100:
Powered by BetterDocs