Set up different tax rates for B2B and B2C in WooCommerce

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 set different taxes for each city, country, postcode, etc.

Let's work with an example. Follow these steps:

1) First, we go to WooCommerce -> Settings -> Tax and add 'B2B' as an additional tax class:

1867521542.png

After saving settings, this creates an additional tax tab for 'B2B Rates':

2) Now we add the following PHP code snippet:

add_action('plugins_loaded', function(){
	
	add_filter( 'woocommerce_product_tax_class', 'wc_diff_rate_for_user', 10000, 2 );
	add_filter( 'woocommerce_product_variation_get_tax_class', 'wc_diff_rate_for_user', 10000, 2 );
	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;
	}
	
	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, we set a 10% rate for Canada

7216138389.png

Then we set a 20% standard rate for Canada (let's say this is the B2C rate):

9438736548.png

Now let's test:

I add products worth $1000 to cart.

When I'm logged in as a B2C user, the rate applied is the standard rate (20%) and the final cost is $1200.

When I'm logged in as a B2B user, the B2B rate is applied (10%) and the final cost is $1100:

Powered by BetterDocs