How To Change The Tax Rate Based on User Role in WooCommerce

$0.00

If you have a WooCommerce customer with the user role customer-tax-exempt, this code will set the tax rate at zero for single and variable products.

Swap out customer-tax-exempt with the slug for the user role you want to set as zero.

add_filter( 'woocommerce_product_get_tax_class', 'woocommerce_different_tax_rate_user_role', 1, 2 );
add_filter( 'woocommerce_product_variation_get_tax_class', 'woocommerce_different_tax_rate_user_role', 1, 2 );
function woocommerce_different_tax_rate_user_role( $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( 'customer-tax-exempt', $user->roles ) ) {
  
  $tax_class = 'zero-rate';
    
  }

  return $tax_class;
  
}

[product_tags]