You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WooCommerce中移除优惠券NEXT50的Tax @5%税类?

WooCommerce优惠券税费排除方案

需求说明

我在WooCommerce中激活了优惠券码NEXT50,优惠金额为-50卢比,希望产品和运费的*Tax @5%*税类不应用于该优惠券,且发票中也不体现该税费。我是WooCommerce新手,请问是否有代码或其他解决方法?

当前税费设置

  • 启用税率和计算:已勾选
  • 输入价格含税费:是,我将输入含税价格
  • 税费计算依据:客户账单地址、运费
  • 税类:Tax @5%
  • 店铺显示价格:含税
  • 购物车和结账时显示价格:含税
  • 税费总计显示:分项列出

注:以上设置无法修改

尝试过的无效代码

// Adjust the coupon's taxable status
add_filter( 'woocommerce_coupon_get_discount_amount', 'set_coupon_taxable_false', 10, 5 );
function set_coupon_taxable_false( $discount, $discounting_amount, $cart_item, $coupon, $cart ) {
if ( $coupon === 'next50' ) {
// Here you can manipulate the discount or the taxable status
// Assuming you want to make it a non-taxable fee
$fee = new WC_Cart_Fee();
$fee->set_id( 'next50' );
$fee->set_amount( -$discount );
$fee->set_taxable( false ); // Set to false for this coupon
$fee->set_total( -$discount );

// Add the fee to the cart
WC()->cart->add_fee( $fee->get_name(), $fee->get_amount(), $fee->is_taxable(), '' );

// Optionally, return 0 if you want to avoid stacking discounts
return 0;
}

return $discount;

}

有效解决方案代码

你之前的代码思路有误,应通过调整优惠券的计税属性和折扣计算逻辑来实现需求。将以下代码添加到主题的functions.php文件中,或使用代码片段插件:

// 标记NEXT50优惠券为非计税项
add_filter( 'woocommerce_coupon_is_taxable', 'exclude_next50_coupon_from_tax', 10, 2 );
function exclude_next50_coupon_from_tax( $is_taxable, $coupon ) {
    // 匹配优惠券码(不区分大小写)
    if ( strtolower( $coupon->get_code() ) === 'next50' ) {
        $is_taxable = false;
    }
    return $is_taxable;
}

// 适配含税价格场景的折扣计算
add_filter( 'woocommerce_coupon_get_discount_amount', 'adjust_next50_coupon_discount_tax', 10, 5 );
function adjust_next50_coupon_discount_tax( $discount, $discounting_amount, $cart_item, $coupon, $cart ) {
    if ( strtolower( $coupon->get_code() ) === 'next50' ) {
        // 获取当前商品/运费的税率
        $tax_rates = WC_Tax::get_rates( $cart_item['data']->get_tax_class() );
        if ( ! empty( $tax_rates ) && wc_prices_include_tax() ) {
            $tax_rate = reset( $tax_rates );
            // 将含税折扣转换为不含税金额,避免税费被错误扣除
            $discount = $discount / ( 1 + $tax_rate['rate'] / 100 );
        }
    }
    return $discount;
}

代码说明

  1. woocommerce_coupon_is_taxable过滤器:直接将NEXT50优惠券标记为非计税项,系统不会对该优惠券的折扣金额计算税费。
  2. woocommerce_coupon_get_discount_amount过滤器:针对“输入价格含税费”的设置,将折扣金额转换为不含税数值,确保优惠券只扣除商品/运费的不含税部分,同时保证发票中不会体现该优惠券相关的税费。

验证方法

  1. 添加商品到购物车,应用NEXT50优惠券。
  2. 查看购物车和结账页面的税费明细,确认税费未因优惠券减少。
  3. 生成测试订单,查看发票,确认无该优惠券相关的税费记录。

内容的提问来源于stack exchange,提问作者amit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 22:17:45