Disable Tax Calculation for specific user role(s) in WooCommerce

75 Views Asked by At

I have two different user roles. Chef and Customer. For Chef I want to add the Tax, but for Customer I don't want to add the Tax. Problem is, for Chef, there is no issue in adding Tax as WooCommerce allows it. But I don't want to add Tax for user Role Customer.

This is what I tried by putting in functions.php, but it is not working.

function exclude_tax_for_customer_role($cart) {
    if (is_user_logged_in()) {
        $user = wp_get_current_user();
        $user_roles = $user->roles;

        // Check if the user has the "Customer" role
        if (in_array('customer', $user_roles)) {
            // Set tax amounts to zero
            $cart->remove_taxes();
        }
    }
}
add_action('woocommerce_cart_loaded', 'exclude_tax_for_customer_role');
1

There are 1 best solutions below

1
LoicTheAztec On BEST ANSWER

Updated

The WC_Customer class has is_vat_exempt property, that is useful to exempt a user from taxes.

Note: This property can also be used for guest users (see the additions).

Try the following to disable taxes for defined user role(s):

add_action( 'template_redirect', 'set_defined_user_roles_vat_exempt' );
function set_defined_user_roles_vat_exempt(){
    global $current_user;

    if ( ! is_user_logged_in() ) 
        return; 

    $targeted_user_roles = array('customer'); // Here define the desired user role(s)

    // Set defined user role(s) "Vat exempt" if it isn't set yet
    if ( count( array_intersect( $targeted_user_roles, $current_user->roles ) ) > 0 && ! WC()->customer->is_vat_exempt() ) {
        WC()->customer->set_is_vat_exempt( true );
    }   
}

Code goes in functions.php file of your theme (or in a plugin). Tested and work.


Additions:

  1. Disable taxes for guest users:
add_action( 'template_redirect', 'set_guest_users_vat_exempt' );
function set_guest_users_vat_exempt(){
    // Set guest users "Vat exempt" if it isn't set yet
    if ( ! is_user_logged_in() && ! WC()->customer->is_vat_exempt() ) {
        WC()->customer->set_is_vat_exempt( true );
    } 
}
  1. Disable taxes for guest users and defined user role(s):
add_action( 'template_redirect', 'set_guest_and_defined_user_roles_vat_exempt' );
function set_guest_and_defined_user_roles_vat_exempt(){
    global $current_user;

    $targeted_user_roles = array('customer'); // Here define the desired user role(s)

    // Set guest users and defined user role(s) "Vat exempt" if it isn't set yet
    if ( ( ! is_user_logged_in() || in_array( 'customer', $current_user->roles ) )
    && ! WC()->customer->is_vat_exempt() ) {
        WC()->customer->set_is_vat_exempt( true );
    } 
}

Code goes in functions.php file of your theme (or in a plugin). Both are tested and work.