跳到主要内容

订单 API

订单 API 返回用于支付订单的信息。

获取订单

GET /order/{ORDER_ID}?key={KEY}&billing_email={BILLING_EMAIL}

此接口有一个必需的参数,即 key。 对于访客订单,必须添加 billing_email

curl "https://example-store.com/wp-json/wc/store/v1/order/{ORDER_ID}?key={KEY}&billing_email={BILLING_EMAIL}"

返回完整的订单对象回复(参见 订单回复)。

回复

订单接口返回的回复格式与 /cart 相同;它是一个订单对象,包含订单项目、已应用的优惠券、配送地址和费率,以及非敏感的客户数据。

订单回复

本节描述了系统对订单的回复。

响应格式

回复采用以下 JSON 格式:

{
"status": "success" | "failed",
"order_id": "%s",
"message": "操作成功" | "订单不存在" | "支付失败" | "其他错误信息"
}
  • status: 表示操作的成功或失败。
  • order_id: 订单的唯一标识符。
  • message: 包含操作结果的详细信息。

成功回复 (status: success)

当订单处理成功时,系统会返回以下回复:

{
"status": "success",
"order_id": "%s",
"message": "操作成功"
}

失败回复 (status: failed)

当订单处理失败时,系统会返回以下回复:

{
"status": "failed",
"order_id": "%s",
"message": "订单不存在" | "支付失败" | "其他错误信息"
}

错误处理

如果出现错误,系统会返回 status: failed 的回复,并包含详细的错误信息。 例如,如果订单不存在,系统会返回:

{
"status": "failed",
"order_id": "%s",
"message": "订单不存在"
}

API 使用示例

以下是一个使用 getOrderResponse 函数的示例:

def getOrderResponse(order_id):
"""
获取订单的回复。

Args:
order_id: 订单的 ID。

Returns:
一个包含回复信息的字典。
"""
# ... (省略实现细节) ...
return {
"status": "success",
"order_id": order_id,
"message": "操作成功"
}

其他说明

  • getOrderResponse 函数可能返回不同的 message 值,具体取决于订单的状态。
  • 如果 order_id 无效,getOrderResponse 函数会返回 status: failed 的回复。
  • 系统会记录所有订单操作,包括成功和失败的回复。
  • %s 是一个占位符,表示要插入的字符串值。
  • {name} 是一个占位符,表示要插入的变量名。
  • Or 表示“或”。
  • on 表示“开”。
  • Response 表示“回复”。
{
"id": 147,
"status": "待审",
"coupons": [
{
"code": "discount20",
"totals": {
"currency_code": "GBP",
"currency_symbol": "£",
"currency_minor_unit": 2,
"currency_decimal_separator": ".",
"currency_thousand_separator": ",",
"currency_prefix": "£",
"currency_suffix": "",
"total_discount": "421",
"total_discount_tax": "0"
}
}
],
"shipping_address": {
"first_name": "Peter",
"last_name": "Venkman",
"company": "",
"address_1": "550 Central Park West",
"address_2": "Corner Penthouse Spook Central",
"city": "New York",
"state": "NY",
"postcode": "10023",
"country": "US",
"phone": "555-2368"
},
"billing_address": {
"first_name": "Peter",
"last_name": "Venkman",
"company": "",
"address_1": "550 Central Park West",
"address_2": "Corner Penthouse Spook Central",
"city": "New York",
"state": "NY",
"postcode": "10023",
"country": "US",
"email": "admin@example.com",
"phone": "555-2368"
},
"items": [
{
"key": "9bf31c7ff062936a96d3c8bd1f8f2ff3",
"id": 15,
"quantity": 1,
"quantity_limits": {
"minimum": 1,
"maximum": 99,
"multiple_of": 1,
"editable": true
},
"name": "Beanie",
"short_description": "<p>This is a simple product.</p>",
"description": "<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>",
"sku": "woo-beanie",
"low_stock_remaining": null,
"backorders_allowed": false,
"show_backorder_badge": false,
"sold_individually": false,
"permalink": "https://local.wordpress.test/product/beanie/",
"images": [
{
"id": 44,
"src": "https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2.jpg",
"thumbnail": "https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-324x324.jpg",
"srcset": "https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2.jpg 801w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-324x324.jpg 324w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-100x100.jpg 100w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-416x416.jpg 416w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-300x300.jpg 300w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-150x150.jpg 150w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-768x768.jpg 768w",
"sizes": "(max-width: 801px) 100vw, 801px",
"name": "beanie-2.jpg",
"alt": ""
}
],
"variation": [],
"prices": {
"currency_code": "GBP",
"currency_symbol": "£",
"currency_minor_unit": 2,
"currency_decimal_separator": ".",
"currency_thousand_separator": ",",
"currency_prefix": "£",
"currency_suffix": "",
"price": "1000",
"regular_price": "2000",
"sale_price": "1000",
"price_range": null,
"raw_prices": {
"precision": 6,
"price": "10000000",
"regular_price": "20000000",
"sale_price": "10000000"
}
},
"item_data": [],
"totals": {
"currency_code": "GBP",
"currency_symbol": "£",
"currency_minor_unit": 2,
"currency_decimal_separator": ".",
"currency_thousand_separator": ",",
"currency_prefix": "£",
"currency_suffix": "",
"line_subtotal": "1000",
"line_subtotal_tax": "0",
"line_total": "800",
"line_total_tax": "0"
},
"catalog_visibility": "view"
},
{
"key": "e369853df766fa44e1ed0ff613f563bd",
"id": 34,
"quantity": 1,
"quantity_limits": {
"minimum": 1,
"maximum": 99,
"multiple_of": 1,
"editable": true
},
"name": "WordPress Pennant",
"short_description": "<p>This is an 外部产品.</p>",
"description": "<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>",
"sku": "wp-pennant",
"low_stock_remaining": null,
"backorders_allowed": false,
"show_backorder_badge": false,
"sold_individually": false,
"permalink": "https://local.wordpress.test/product/wordpress-pennant/",
"images": [
{
"id": 57,
"src": "https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1.jpg",
"thumbnail": "https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-324x324.jpg",
"srcset": "https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1.jpg 800w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-324x324.jpg 324w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-100x100.jpg 100w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-416x416.jpg 416w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-300x300.jpg 300w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-150x150.jpg 150w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-768x768.jpg 768w",
"sizes": "(max-width: 800px) 100vw, 800px",
"name": "pennant-1.jpg",
"alt": ""
}
],
"variation": [],
"prices": {
"currency_code": "GBP",
"currency_symbol": "£",
"currency_minor_unit": 2,
"currency_decimal_separator": ".",
"currency_thousand_separator": ",",
"currency_prefix": "£",
"currency_suffix": "",
"price": "1105",
"regular_price": "1105",
"sale_price": "1105",
"price_range": null,
"raw_prices": {
"precision": 6,
"price": "11050000",
"regular_price": "11050000",
"sale_price": "11050000"
}
},
"item_data": [],
"totals": {
"currency_code": "GBP",
"currency_symbol": "£",
"currency_minor_unit": 2,
"currency_decimal_separator": ".",
"currency_thousand_separator": ",",
"currency_prefix": "£",
"currency_suffix": "",
"line_subtotal": "1105",
"line_subtotal_tax": "0",
"line_total": "884",
"line_total_tax": "0"
},
"catalog_visibility": "view"
}
],
"needs_payment": true,
"needs_shipping": true,
"totals": {
"subtotal":"2105",
"total_discount": "421",
"total_shipping": "500",
"total_fees": "0",
"total_tax": "0",
"total_refund": "0",
"total_price": "2184",
"total_items": "2105",
"total_items_tax": "0",
"total_fees_tax": "0",
"total_discount_tax": "0",
"total_shipping_tax": "0",
"tax_lines": []
},
"errors": [],
"payment_requirements": [ "products" ],
}

错误回复

如果无法执行某个订单操作,将会返回一个错误回复。该回复将包含一个原因代码和一个错误信息:

{
"code": "woocommerce_rest_invalid_order",
"message": "提供的订单 ID 或关键字无效。",
"data": {
"status": 401
}
}