1
votes

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?

1

1 Answers

1
votes

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.