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

通过REST API创建WooCommerce订单时行项目未计算税金问题咨询

解决WooCommerce REST API创建订单时税金不显示的问题

核心原因

默认情况下,WooCommerce REST API会根据订单的 billing/shipping 地址自动重新计算税金,这会覆盖你手动传入的line_items.taxes数组内容,导致订单中看不到CGST、SGST、IGST。

解决方案

1. 修改API请求参数,禁用自动税金计算

在请求体中添加"calculate_taxes": false,明确告诉系统不要自动计算税金,保留你手动传入的税金数据。同时需要补充行项目的subtotal(不含税金额)和total(含税总金额),确保金额与税金匹配。

修改后的请求示例:

{
    "payment_method": "cod",
    "payment_method_title": "Cash on delivery",
    "set_paid": true,
    "customer_id": 1200584, 
    "customer_note": "Some of my notes", 
    "calculate_taxes": false, // 关键参数:禁用自动税金计算
    "billing": {
        "first_name": "Example", 
        "last_name": "Developer", 
        "address_1": "Example address", 
        "address_2": "Delhi2", 
        "city": "Delhi", 
        "state": "Delhi", 
        "postcode": "110020", 
        "country": "IN", 
        "email": "developer@example.com", 
        "phone": "9876543210"
    }, 
    "shipping": {
        "first_name": "Example", 
        "last_name": "Developer", 
        "address_1": "Example address", 
        "address_2": "Delhi2", 
        "city": "Delhi", 
        "state": "Delhi", 
        "postcode": "110020", 
        "country": "IN", 
        "email": "developer@example.com", 
        "phone": "9876543210"
    }, 
    "line_items": [
        {
            "product_id": "849660",
            "quantity": 1,
            "subtotal": "761.904762", // 不含税金额:38.095238 ÷ 5%
            "total": "800.00", // 含税总金额:761.904762 + 38.095238
            "taxes": [
                {
                    "rate_code": "IN-DL-CGST-1",
                    "rate_id": "9193",
                    "label": "CGST",
                    "compound": false,
                    "tax_total": "0",
                    "rate_percent": "0%"
                },
                {
                    "rate_code": "IN-DL-SGST-2",
                    "rate_id": "9229",
                    "label": "SGST",
                    "compound": false,
                    "tax_total": "0",
                    "rate_percent": "0%"
                },
                {
                    "rate_code": "IN-DL-IGST-3",
                    "rate_id": "9265",
                    "label": "IGST",
                    "compound": false,
                    "tax_total": "38.095238",
                    "rate_percent": "5%"
                }
            ]
        }
    ],
    "shipping_lines": [
        {
            "method_id": "flat_rate",
            "method_title": "Flat Rate",
            "total": "60.00"
        }
    ],
    "fee_lines": [
        {
            "name": "COD Charges",
            "total": "30.00"
        }
    ]
}

2. 检查WooCommerce后台税金设置

  • 进入WooCommerce > 设置 > 税金,确保启用税金计算,且已正确配置CGST、SGST、IGST对应的税则,税则的适用地区需匹配订单中的德里(IN-DL)地址。
  • 开启“允许手动输入税金”选项(在税金设置的“选项”标签下),确保系统支持手动指定税金数据。

3. 使用过滤器强制保留税金数据

如果上述方法仍不生效,可以通过PHP过滤器在订单创建前手动写入税金信息,避免被系统覆盖:

add_filter('woocommerce_rest_pre_insert_shop_order_object', 'force_preserve_line_item_taxes', 10, 3);
function force_preserve_line_item_taxes($order, $request, $creating) {
    if (!$creating || !isset($request['line_items'])) {
        return $order;
    }

    foreach ($request['line_items'] as $item_index => $item_data) {
        if (!isset($item_data['taxes']) || empty($item_data['taxes'])) {
            continue;
        }

        // 获取对应行项目对象
        $line_items = $order->get_items('line_item');
        $line_item = isset($line_items[$item_index]) ? $line_items[$item_index] : null;
        
        if (!$line_item) {
            continue;
        }

        // 手动添加税金数据
        foreach ($item_data['taxes'] as $tax) {
            $line_item->add_tax([
                'rate_id'       => $tax['rate_id'],
                'label'         => $tax['label'],
                'tax_total'     => $tax['tax_total'],
                'rate_percent'  => $tax['rate_percent']
            ]);
        }
        $line_item->save();
    }

    return $order;
}

将这段代码添加到主题的functions.php文件或自定义插件中即可。

内容的提问来源于stack exchange,提问作者Md Nazrul Islam

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 06:02:33