Skip to content

Instantly share code, notes, and snippets.

@woogist
Last active October 4, 2022 10:40
Show Gist options
  • Save woogist/48c776c39df23d53df8d to your computer and use it in GitHub Desktop.
Save woogist/48c776c39df23d53df8d to your computer and use it in GitHub Desktop.
Apply different tax rates based on user role
<?php
/**
* Apply a different tax rate based on the user role.
*/
function wc_diff_rate_for_user( $tax_class, $product ) {
if ( is_user_logged_in() && current_user_can( 'administrator' ) ) {
$tax_class = 'Zero Rate';
}
return $tax_class;
}
add_filter( 'woocommerce_product_get_tax_class', 'wc_diff_rate_for_user', 1, 2 );
add_filter( 'woocommerce_product_variation_get_tax_class', 'wc_diff_rate_for_user', 1, 2 );
@lakisxaxas
Copy link

Dear Sir/Madame,
I am writing to ask your assistance.
I have set up an e-shop with woocommerce plugin.
There are two options for the costumers after registration: the retail and the wholesales.
My problem is that I want prices to include taxes for retail customers and exclude taxes for wholesale customers. Taxes for wholesale customers should appear only in the cart page and in the checkout page.
However, I don’t know how to do it.
I will appreciate any help from you.
Thank you in advance.

@lomars
Copy link

lomars commented Nov 30, 2016

There is a big mistake here with current_user_can( 'administrator' ), as current_user_can() WordPress conditional function is for role "capabilities", BUT certainly NOT for user roles…
So this condition will not work, because 'administrator' is a user role (not a capability).

The correct code should be:

<?php
/**
 * Apply a different tax rate based on the user role.
 */
function wc_diff_rate_for_user( $tax_class, $product ) {
        $user_id = get_current_user_id();
        $user_data = get_userdata($user_id);
	if ( is_user_logged_in() && in_array( 'administrator', $user_data->roles ) ) {
		$tax_class = 'Zero Rate';
	}
	return $tax_class;
}
add_filter( 'woocommerce_product_tax_class', 'wc_diff_rate_for_user', 1, 2 );

@Topque
Copy link

Topque commented Jan 2, 2017

If this function active, new products with custom taxes dont work for me!
In this case administrador only have Zero Rate tax role. When products have custom tax "Custom Tax" administrador is on "Zero Rate" tax, why?

@sdulaney
Copy link

sdulaney commented Apr 4, 2017

Thanks @lomars 👍

@willgorham
Copy link

Should update the filter 'woocommerce_product_tax_class' (now deprecated) to 'woocommerce_product_get_tax_class'

@willgorham
Copy link

willgorham commented Jul 10, 2017

For reference, this is what I now use:

/**
 * Apply a zero tax rate for 'administrator' user role.
 */
function wc_diff_rate_for_user( $tax_class, $product ) {
  $user_id = get_current_user_id();
  $user = get_user_by( 'id', $user_id );

  if ( is_user_logged_in() && ! empty( $user ) && in_array( 'administrator', $user->roles ) ) {
    $tax_class = 'Zero Rate';
  }

  return $tax_class;
}
add_filter( 'woocommerce_product_get_tax_class', 'wc_diff_rate_for_user', 1, 2 );

@webthread
Copy link

Prior to WC3.X this filter would run on the frontend and the admin order page. It still works on the frontend with the @willgorham update but doesn't fire on the admin order page. Does anybody know a way to make the filter run on the admin order section as well?

@klishb
Copy link

klishb commented Jun 21, 2018

None of these solutions seem to work with the new "Automated taxes" option powered by JetPack. In fact I had an item in my cart that was not taxable and it was correctly showing no tax would be charged when using the "Automated taxes" option. All of the options listed in this thread resulted in me being charged tax even on the same cart was the previously not taxed. It seems to break the API call to TaxJar although I'm not seeing an error in the tax log.

@giorgio0033
Copy link

For reference, this is what I now use:

/**
 * Apply a zero tax rate for 'administrator' user role.
 */
function wc_diff_rate_for_user( $tax_class, $product ) {
  $user_id = get_current_user_id();
  $user = get_user_by( 'id', $user_id );

  if ( is_user_logged_in() && ! empty( $user ) && in_array( 'administrator', $user->roles ) ) {
    $tax_class = 'Zero Rate';
  }

  return $tax_class;
}
add_filter( 'woocommerce_product_get_tax_class', 'wc_diff_rate_for_user', 1, 2 );

Hello, can you please help, i need to apply zero tax to everyone except wholesaler role. How is that possible?

@Danielrbc
Copy link

Thanks for you code example.

What about if you need to do something similar but on the woocommerce backend admin side?
How can you get the $user_id from the order or with filter use?

Many thanks to all!!

@trztrztrz
Copy link

Hi, that code works fine for me.

I would like to display the prices excluding taxes on shop and single product page for specific user role BUT keep the taxes in cart and checkout.

Many thanks if someone helps me with that :)

@KevinChauvet
Copy link

Hi, that code works fine for me.

I would like to display the prices excluding taxes on shop and single product page for specific user role BUT keep the taxes in cart and checkout.

Many thanks if someone helps me with that :)

Same for me !

@Danielrbc
Copy link

Hi, that code works fine for me.
I would like to display the prices excluding taxes on shop and single product page for specific user role BUT keep the taxes in cart and checkout.
Many thanks if someone helps me with that :)

Same for me !

You can do that you need on woocommerce configuration of taxes.
Woocommerce>>settings>taxes.

Good Luck!

@KevinChauvet
Copy link

Hi, that code works fine for me.
I would like to display the prices excluding taxes on shop and single product page for specific user role BUT keep the taxes in cart and checkout.
Many thanks if someone helps me with that :)

Same for me !

You can do that you need on woocommerce configuration of taxes.
Woocommerce>>settings>taxes.

Good Luck!

Not really. In france, in B2B you have to show the prices tax excluded everywhere except on the total in the cart. So I need to force the detailed taxes in the total cart and excluded tax everywhere else ...

@Danielrbc
Copy link

OK, then use this code. Maybe can help you.

add_filter( 'woocommerce_get_price_html', 'custom_price_message' ); function custom_price_message($price) { $new_price = $price . ' <span class="custom-price-prefix">' . __('<br>(plus VAT)').'</span>'; return $new_price; }

@KevinChauvet
Copy link

OK, then use this code. Maybe can help you.

add_filter( 'woocommerce_get_price_html', 'custom_price_message' ); function custom_price_message($price) { $new_price = $price . ' <span class="custom-price-prefix">' . __('<br>(plus VAT)').'</span>'; return $new_price; }

Yeah I already did that, the problem is that it's just a change for the suffix, not the final price ! The price is still without taxes and I need to force the VAT to be applied on the final price :)

@lufemas
Copy link

lufemas commented Jun 23, 2021

It does not work when a product is added or edit in the Order Admin Page, it is probably another hook that's called, if anyone know please tell me

@tahmidbintaslim
Copy link

Hi ! Is there anyone who used this function for Woocommerce Membership Plugin which has Membership Plans?

@GogoulosT
Copy link

For reference, this is what I now use:

/**
 * Apply a zero tax rate for 'administrator' user role.
 */
function wc_diff_rate_for_user( $tax_class, $product ) {
  $user_id = get_current_user_id();
  $user = get_user_by( 'id', $user_id );

  if ( is_user_logged_in() && ! empty( $user ) && in_array( 'administrator', $user->roles ) ) {
    $tax_class = 'Zero Rate';
  }

  return $tax_class;
}
add_filter( 'woocommerce_product_get_tax_class', 'wc_diff_rate_for_user', 1, 2 );

Hello, can you please help, i need to apply zero tax to everyone except wholesaler role. How is that possible?

Hello

I used this code and worked great for simple products. For variable products i still see prices with VAT.
Please advise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment