How to set reduced tax rates by user class on Woocommerce?

View QuestionsCategory: QuestionsHow to set reduced tax rates by user class on Woocommerce?
admin Staff asked 6 years ago

I want to set lower tax rates for certain user class / user role in Woocommerce.

1 Answers
Best Answer
admin Staff answered 6 years ago

Instructions:

  1. Go to Woocommerce -> Settings -> Tax
  2. Go to Reduced Rate Rates
  3. Type in or import the proper tax rates
    1. If you are importing, note that the “Tax Class” column needs to say “Reduced rate”. Otherwise if you leave it blank, all of the entries will go to the normal rate table
  4. Use the code below, add the user roles that should have lower tax rate to $not_taxed_roles
  5. Add the modified code to functions.php of the theme / child theme you are using.

Credit: https://github.com/woocommerce/woocommerce/issues/14939

/**
 * Apply a different tax rate based on the user role.
 *
 * @param $tax_class
 * @param $product
 *
 * @return string
 */
function woo_diff_rate_for_user( $tax_class, $product ) {

    $current_user = wp_get_current_user();
    $roles = $current_user->roles;
    $current_user_role = $roles[0];

    $not_taxed_roles = array("customer", "administrator");

    if (is_user_logged_in() && in_array($current_user_role, $not_taxed_roles)) {

        //$tax_class = "Zero Rate";
        $tax_class = "Reduced Rate";
    }
    return $tax_class;
}
add_filter( "woocommerce_product_get_tax_class", "woo_diff_rate_for_user", 1, 2 );
add_filter( "woocommerce_product_variation_get_tax_class", "woo_diff_rate_for_user", 1, 2 );

In addition to the above, if you want to have shipping tax rate to have the same changes as above, you need to set your woocommerce tax settings:

  1. Go to Woocommerce -> Settings -> Tax
  2. Change Shipping tax class to “Shipping Tax Class based on Cart Items”