Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate Tax Based on Shipping Method

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?

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' );
like image 440
j8d Avatar asked Dec 11 '25 19:12

j8d


1 Answers

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' );
like image 63
j8d Avatar answered Dec 13 '25 09:12

j8d



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!