عثمان غني
عثمان غني

Reputation: 2708

Tax exclusive if coupon applied else tax inclusive in items

I have setup taxes inclusive for all my items and filled MRP in price. But now I want to apply tax inclusive if customer didn't applied coupon i.e. buying on MRP. But when customer applies coupon I need to apply taxes on after discount amount.

Is it possible with settings within Woocommerce or is there any plugin available?

For e.g.
**Case I**
Product MRP = 670
Shipping    =  50
Tax 18%     = 102
Final price = 670 (Including Taxes) 
It's Fine.


**Case II**
Product MRP = 670
Discount 40%= 268
Price       = 402
Shipping    =  50
Tax 18%     =  61
Final price = 452 (Including Taxes)
But I need tax to calculated exclusively on discounted price i.e. 402+18% = 474+50 (Ship) = 524

I have tried following filter in my custom plugin:

add_filter( 'woocommerce_calc_tax', 'inc_or_exc',10,3 );
// add_filter( 'woocommerce_calculate_totals', 'calculate_totals',11 );
function inc_or_exc( $taxes,$price,$rates ) {
    // echo "<pre>";
    if(!empty(WC()->cart->coupon_discount_amounts)){
        return  WC_Tax::calc_exclusive_tax( $price, $rates );
    }else{
        return  WC_Tax::calc_inclusive_tax( $price, $rates );
    }
}

But it calculates taxes bit strange. If item MRP is 100, it shows 98.85 and also totals are not updating with new taxes and shipping rates after plugin run. If I disable plugin then item MRP is shown fine i.e. 100.

Upvotes: 10

Views: 958

Answers (1)

عثمان غني
عثمان غني

Reputation: 2708

Finally I have solved it.

First I applied inclusive exlusive filter. Then called woocommerce_calculated_total with custom condition and achieved my motive.

add_filter( 'woocommerce_calc_tax', 'inc_or_exc',10,3 );
// do_action('add_points');

add_filter( 'woocommerce_calculated_total', 'custom_calculated_total', 10, 2 );
function inc_or_exc( $taxes,$price,$rates ) {
    // echo "<pre>";
    if(!empty(WC()->cart->coupon_discount_amounts)){
        return  WC_Tax::calc_exclusive_tax( $price, $rates );
    }else{
        return  WC_Tax::calc_inclusive_tax( $price, $rates );
    }
}

function custom_calculated_total( $total, $cart ){
    // echo "<pre>";
    if(!empty(WC()->cart->coupon_discount_amounts)){
        return round( $total + WC()->cart->get_cart_contents_tax(), $cart->dp );
    }else{
        return round( $total, $cart->dp );
    }   
}

Upvotes: 7

Related Questions