我为我的所有商品设置了含税,并在价格中填写了 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 );
}
}
但是它计算税金有点奇怪。如果 Material MRP 为 100,则显示 98.85,并且在插件运行后总计不会更新为新的税费和运费。如果我禁用插件,那么 Material MRP 会显示正常,即 100。
最佳答案
终于解决了。
首先,我应用了包容排他过滤器。然后使用自定义条件调用 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 );
}
}
关于php - 如果使用优惠券则不含税,否则商品含税,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56737369/