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

WooCommerce对接API:产品ID与订单数量动态变量咨询

WooCommerce 对应动态变量说明
  • Product ID:WooCommerce中商品的ID对应变量为 product.id(若通过REST API获取商品对象,字段为id;在订单行项目上下文里,可通过item.product_id获取当前订单中对应商品的ID)。
  • Order Count:
    • 若指当前订单中该商品的购买数量,对应订单行项目的item.quantity字段;
    • 若指该商品的总销售订单数,需用WooCommerce内置函数wc_get_product_sales_count( $product_id )获取。

代码调整示例

假设已从后端拿到订单数据,前端可按如下方式填充变量:

// 从后端获取订单数据(例如通过WP REST API传递)
const order = /* 订单JSON数据 */;

// 处理订单中的商品行项目
const products = order.line_items.map(item => ({
    ProductId: item.product_id,
    OrderCount: item.quantity
}));

const options2 = {
    method: 'POST',
    headers: {
        "SettleTypeId": `STID`,
        "RRN": `RNN`,
        "ApMerchantId": `MerchantID`, // 注意原代码此处多了个冒号,需修正
        "Products": products
    }
};

fetch('URL', options2)
    .then(response => response.json())
    .then(response => console.log(response))
    .catch(err => console.error(err));

额外提示

  • 调用WooCommerce REST API时,返回的商品详情中ID字段为id,订单行项目的数量字段为quantity、商品ID字段为product_id;
  • 原代码请求头里的"ApMerchantId:"存在语法错误(多余冒号),会导致请求失败,需及时修正。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 08:40:42