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

如何通过Woocommerce REST API获取店铺订单总数用于分页?

获取WooCommerce订单总数的方法

方法1:提取API响应头的X-WP-Total字段

当你调用订单列表API时,HTTP响应头里会自带X-WP-Total字段,它的值就是店铺的订单总数量。为了减少返回的数据量,你可以把per_page参数设为1:

https://example.com/wp-json/wc/v3/orders?consumer_key=ck_xxx&consumer_secret=cs_xxx&per_page=1

发起请求后,直接从响应头中读取X-WP-Total的值即可,这是最快捷的方式。

方法2:调用订单统计API端点

WooCommerce提供了专门的统计接口,能直接返回包含订单总数的汇总数据,请求地址如下:

https://example.com/wp-json/wc/v3/reports/orders/totals?consumer_key=ck_xxx&consumer_secret=cs_xxx

返回的JSON数据里,orders_count字段对应的数值就是订单总数,示例返回结构:

[
  {
    "total": "125",
    "total_sales": "5000.00",
    "tax_total": "450.00",
    "shipping_total": "300.00",
    "net_total": "4250.00",
    "average": "40.00",
    "total_refunds": "0.00",
    "orders_count": 125
  }
]

方法3:自定义REST API端点(适合有开发能力的场景)

如果需要更灵活的订单状态过滤,你可以在WordPress主题的functions.php中添加自定义代码,创建一个专属的订单总数查询端点:

add_action('rest_api_init', function () {
  register_rest_route('custom/v1', '/order-count', array(
    'methods' => 'GET',
    'callback' => 'get_total_order_count',
    'permission_callback' => function () {
      return current_user_can('manage_woocommerce');
    }
  ));
});

function get_total_order_count() {
  $args = array(
    'status' => array('processing', 'completed', 'on-hold'), // 按需指定需要统计的订单状态
    'return' => 'ids'
  );
  $orders = wc_get_orders($args);
  return count($orders);
}

完成代码添加后,请求以下地址就能拿到自定义过滤后的订单总数:

https://example.com/wp-json/custom/v1/order-count?consumer_key=ck_xxx&consumer_secret=cs_xxx

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 12:47:43