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');
Updated
The
WC_Customerclass hasis_vat_exemptproperty, 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):
Code goes in functions.php file of your theme (or in a plugin). Tested and work.
Additions:
Code goes in functions.php file of your theme (or in a plugin). Both are tested and work.