php - 如果使用优惠券则不含税,否则商品中含税
问题描述
我已经为我的所有物品设置了含税,并填写了 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。
解决方案
最后我已经解决了。
首先,我应用了包含独占过滤器。然后以自定义条件调用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 );
}
}
推荐阅读
- flutter - 颤振,函数应该返回一个小部件
- node.js - React native expo cli 安装错误:404 Not Found - GET https://registry.npmjs.org/error-ex
- php - 将表与自身连接并显示在屏幕上
- redis - 使用具有内部和公共 IP 的 Redis 集群
- angular - 如何从一个 BehaviorSubject 存储中为 Angular 中动态创建的 HTML 标记获取不同的值?
- java - 如何提取 JSON 值?
- python - 如何在 Selenium 中单击并打开页面上的日历?
- node.js - Mongoose 查询:将一个值与同一文档的另一个转换值进行比较
- bash - 在 UNIX 中获取 txt 文件中的 Oracle 输出
- c# - 如何在另一个windows窗体或水晶报表窗体c#中捕获项目级数据集