首页 > 解决方案 > 如果使用优惠券则不含税,否则商品中含税

问题描述

我已经为我的所有物品设置了含税,并填写了 MRP 价格。但现在我想申请含税,如果客户没有申请优惠券,即在 MRP 上购买。但是当客户申请优惠券时,我需要对折扣后的金额征税。

Woocommerce 中的设置是否可行,或者是否有可用的插件?

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

我在我的自定义插件中尝试了以下过滤器:

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 );
    }
}

但它计算税收有点奇怪。如果物料 MRP 为 100,则显示 98.85,并且插件运行后总计不会更新新税费和运费。如果我禁用插件,则项目 MRP 显示正常,即 100。

标签: phpwordpresswoocommerce

解决方案


最后我已经解决了。

首先,我应用了包含独占过滤器。然后以自定义条件调用woocommerce_calculated_total并实现了我的动机。

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 );
    }   
}

推荐阅读