You need to enable JavaScript to run this app. WooCommerce REST API创建订单被额外加税的解决方案咨询
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce REST API创建订单被额外加税的解决方案咨询

问题原因
  • WooCommerce REST API V3 /wp-json/wc/v3/orders 接口的订单创建逻辑,默认不会继承后台「是的,我输入的价格包含税费」的全局税率配置,会将请求传入的订单项金额识别为不含税金额,因此会在传入金额基础上额外计算叠加税费,最终导致订单总金额异常。
  • 后台手动创建订单时,系统会主动读取全局税率配置,将输入的商品金额按含税逻辑拆分税费,因此计税结果符合预期。
修复方案

因为你无法修改外部平台传入的请求报文,可以通过WooCommerce原生的Hook钩子修改API创建订单时的计税逻辑,强制和后台全局配置对齐,代码示例如下:

// 强制REST API创建订单时,使用价格含税的全局配置
add_filter( 'woocommerce_rest_pre_insert_shop_order_object', function( $order, $request, $creating ) {
    if ( $creating ) {
        add_filter( 'woocommerce_prices_include_tax', '__return_true', 999 );
    }
    return $order;
}, 10, 3 );

// 修正订单项计税逻辑,按含税价格拆分税费避免额外加税
add_action( 'woocommerce_rest_insert_shop_order_item', function( $item, $request, $creating ) {
    if ( ! $creating || ! is_a( $item, 'WC_Order_Item_Product' ) ) {
        return;
    }
    $order = $item->get_order();
    $tax_rates = WC_Tax::get_rates_from_location(
        $order->get_tax_based_on(),
        $order->get_billing_country(),
        $order->get_billing_state(),
        $order->get_billing_postcode(),
        $order->get_billing_city()
    );
    $subtotal = $item->get_subtotal();
    $subtotal_tax = WC_Tax::calc_inclusive_tax( $subtotal, $tax_rates );
    $item->set_subtotal_tax( array_sum( $subtotal_tax ) );
    $total = $item->get_total();
    $total_tax = WC_Tax::calc_inclusive_tax( $total, $tax_rates );
    $item->set_total_tax( array_sum( $total_tax ) );
    $item->save();
}, 10, 3 );

将上述代码添加到当前WordPress站点主题的functions.php文件,或者自定义功能插件中即可生效,无需修改外部平台传入的请求内容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.12 14:51:01