3
votes

How can I charge different tax rates based on the shipping method a customer selects at checkout in Woocommerce? My store has one shipping option that lets international customers avoid the 7% VAT charged here in Thailand.

Here's how to disable taxes when Local Pickup is selected as the shipping option according to Woocommerce documentation:

add_filter( 'woocommerce_apply_base_tax_for_local_pickup', '__return_false' );

But how do I disable taxes on a custom shipping option?

Edit: I've started to work out a solution, but I could use some help with line 2. i.e. How to get the current shipping method?

function remove_tax_for_fob( $cart ) {
    $ok_remove = get_shipping_method( 'FOB' );
    if ($ok_remove){ 
        $cart->remove_taxes();
}
return $cart;
} 
add_action( 'woocommerce_calculate_totals', 'remove_tax_for_fob' );
2
Documentation states that in the calculate_shipping method of your custom shipping option you can set 'taxes' => false . So before you set the rate check if the client is international, and then add the tax as required.Anand Shah
@AnandShah It was my understanding that setting 'taxes' => 'false' here just excludes taxes from the shipping cost calculation, not that it excludes taxes from the order amount. Am I mistaken? That would be great if so!j8d
You are right, sorry I misunderstood the requirement. So whether to charge tax or not on the entire order is dependent on the shipping method the customer chooses?Anand Shah
WC()->customer->is_vat_exempt = true if the custom shipping option is selected will set the tax to 0Anand Shah
Yes, when I went through the code of Cart class, I did find that method too but couldn't get it work. If you manage to get it working, please share your solutionAnand Shah

2 Answers

2
votes

Here is the solution. Thanks for your help, Anand Shah!

/* Remove tax from cart for FOB orders */
function remove_tax_for_fob( $cart ) {
    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = $chosen_methods[0]; 
    if($chosen_shipping =='FOB') {
        $cart->remove_taxes();
    }
    return $cart;
}
add_action( 'woocommerce_calculate_totals', 'remove_tax_for_fob' );
1
votes

Try the following, will need a bit of polishing though

add_action( 'woocommerce_review_order_before_submit','custom_review_order_before_submit');

function custom_review_order_before_submit() {

    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = $chosen_methods[0]; 

    if( "FOB" == $chosen_shipping ) {

        WC()->customer->is_vat_exempt = true;

    } else {

        WC()->customer->is_vat_exempt = false;

    }    

}