5

I am trying to set up a woocommerce store so that users who have a role of wholesaler or designer will automatically be exempt from tax and just have tax disappear from the cart/checkout. I've used the dynamic pricing plugin to provide different prices to different roles but there is no options for tax variations.

Someone posted this code:

// Place the following code in your theme's functions.php file and replace tax_exempt_role with the name of the role to apply to
add_action( 'init', 'woocommerce_customer_tax_exempt' );
function woocommerce_customer_tax_exempt() {
    global $woocommerce;
    if ( is_user_logged_in() ) {
        $tax_exempt = current_user_can( 'tax_exempt_role');
        $woocommerce->customer->set_is_vat_exempt( $tax_exempt );
    }
}

This seems to be working on the front end but breaks the backend. after adding it to functions.php when i go back into the admin area and see this: https://i.stack.imgur.com/x2D4Z.png (is this just the new chrome error page?)

The other thing I couldn't figure out is how to add 2 roles instead of just one.

Thanks

James
  • 193
  • 3
  • 11

2 Answers2

8

The following worked for me for user role "wholesaler". Added to functions.php.

add_filter( 'woocommerce_before_checkout_billing_form', 'prevent_wholesaler_taxes' );

function prevent_wholesaler_taxes() {

     global $woocommerce;

     if( current_user_can('wholesaler')) {

              $woocommerce->customer->set_is_vat_exempt(true);

         } else {

              $woocommerce->customer->set_is_vat_exempt(false);
         }
} //end prevent_wholesaler_taxes

To add multiple user roles, just add to the current_user_can(); function. I think this could work:

 if( current_user_can('wholesaler')||current_user_can('another_user_role') ) 
dryan1144
  • 96
  • 2
  • `woocommerce_before_checkout_billing_form` is an action so `add_action`would be the proper usage. Instead of usage `add_filter` – Bas van Dijk Dec 03 '18 at 10:11
2

I noticed that when using 'woocommerce_before_checkout_billing_form', you have to update or refresh the checkout page first, then you have to refresh the cart page for it to take effect.

Use these action hooks, 'woocommerce_before_cart_contents' and 'woocommerce_before_shipping_calculator' for the tax exemption to take effect without refreshing the checkout page first.

Note: use the same callback function code as above.

Jplus2
  • 2,216
  • 2
  • 28
  • 49
  • This is correct if people fill out the form from top to bottom, but the problem is that the custom field we've inserted won't trigger the AJAX call to recalculate the cart totals in the same way as entering addresses will. And (surprisingly) it doesn't seem that the final "Place Order" click will trigger a recalculate either. Relying on users progressing logically down the page is a little risky. – richplane Jan 19 '18 at 17:40