1

I am currently trying to implement some dynamic taxing for products and shipping in WooCommerce. So far, I have used this method: WooCommerce Documentation on Tax Classes and User Roles

for setting the Tax Class of Products added to the cart based on the users data. That data is a custom checkbox in the admin area that I have created. If the check box is selected, the user should get zero-rate taxing.

This works as expected for the most part, but it ends up creating two problems that I cannot work out.

1)The checkout page never fully loads (The order summary and payment option panels have the ever-spinning 'loading' icons over them) and

2)The shipping tax does not update. Specifically, if I go from a tax class that has sales tax and shipping tax to zero-rate tax, the sales tax is updated correctly (to none), but the shipping fee still has tax applied.

I have the tax rates set up correctly in the WooCommerce settings (the zero-rate has no shipping tax), but no change.

I am using this filter to apply the change:

add_filter('woocommerce_product_get_tax_class', 'wc_diff_rate_for_user', 1, 2);

Can anyone help me get my shipping rates set properly for the cart and checkout?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Case Silva
  • 466
  • 7
  • 26

1 Answers1

1

There is a better way if you want a specific user role to be exempted of taxes. The WC_Customer class has an interesting property which is "is_vat_exempt".

It is possible to set all customer from a specific user role to be "Vat Exempt" using this:

add_action( 'template_redirect', 'is_user_vat_exempt' );
function is_user_vat_exempt(){
    // Only for logged in users
    if ( ! is_user_logged_in() ) 
        return; 

    // Set specific user role "Vat exempt" if is not set yet
    if ( current_user_can( 'some_user_role' ) && ! WC()->customer->is_vat_exempt() ) {
        WC()->customer->set_is_vat_exempt( true );
    }   
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

This will solve all your problems and you will need to remove your related code that is not needed anymore.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hey Loic, thanks for taking the time. This MOSTLY works. With a few additions to the IF statement, I can get the limitations to behave like the CSV file I created for my 'Zero Rate' tax rate mostly. The problem is that it doesn't change this for cart display, only the Cart Totals area, so it still displays with tax in the header/cart preview. If I could get the Tax and Shipping rates reflected in my Zero Rate tax rate, it would be the most ideal. – Case Silva Nov 27 '18 at 15:16
  • 1
    @CaseSilva This is because you have some other customizations or a plugin that is interfering… I don't have the problem you discribe. – LoicTheAztec Nov 27 '18 at 16:01
  • I did get it to work fully as you describe. Not sure what changed (I didn't deactivate any other plugin or functionality), but it's working now. Thanks for the help! – Case Silva Nov 27 '18 at 16:46