Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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_tax_class', 'wc_diff_rate_for_user', 1, 2 );
@lakisxaxas

This comment has been minimized.

Copy link

commented May 24, 2016

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Apr 4, 2017

Thanks @lomars 👍

@willgorham

This comment has been minimized.

Copy link

commented Jul 10, 2017

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

@willgorham

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Jul 31, 2017

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

This comment has been minimized.

Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.