跳到主要内容

WooCommerce Store API

Store API 为面向客户的购物车、结账和产品功能的开发提供公开的 REST API 端点。它遵循 WordPress REST API 中使用的许多样板。

与 WooCommerce REST API 不同,Store API 无需身份验证,并且不提供对敏感商店数据或其他客户信息的访问权限。

使用 cURL 的有效 API 请求示例:

curl "https://example-store.com/wp-json/wc/store/v1/products"

Store API 的可能用途包括:

  1. 获取可显示、可搜索或过滤的产品列表
  2. 将产品添加到购物车并返回已更新的购物车对象以供显示
  3. 获取购物车的运输费率
  4. 将客户的购物车转换为订单,收集地址,然后促进支付

要求与限制

  • 这是一个无需认证的 API。访问时不需要 API 密钥或认证令牌。
  • 所有 API 回复均返回 JSON 格式的数据。
  • 从 API 返回的数据反映的是当前用户(客户)的信息。WooCommerce 中的客户会话是基于 Cookie 的。
  • Store API 不能用于通过 ID 查找其他客户和订单;只能访问属于当前用户的数据。
  • 同样,Store API 不能用于写入商店数据,例如设置。如需更广泛的访问权限,请使用经过认证的 WC REST API
  • 允许写入的端点(例如,更新当前客户地址)需要一个随机数令牌
  • Store API 与渲染目标无关,不应对内容的显示位置做出假设。例如,除非数据类型本身是 HTML,否则不鼓励返回 HTML。

商店 API 命名空间

商店 API 中的所有资源都位于 wc/store/v1 命名空间内,并且由于此 API 扩展了 WordPress API,访问它需要 /wp-json/ 基础路径。当前唯一的版本是 v1。如果省略版本,将提供 v1 版本。

例子:

GET /wp-json/wc/store/v1/products
GET /wp-json/wc/store/v1/cart

该 API 使用 JSON 来序列化数据。您无需在 API 网址末尾指定 .json

资源和端点

以下列出了商店 API 中可用的资源,并提供了指向更详细文档的链接。

Installation

To install the package, use the following command:

pip install %s

This will install the package and all of its dependencies.

Usage

The package provides a simple API for interacting with the service. Here's a basic example:

import %s

# Create a new client
client = %s.Client()

# Call the API
response = client.get_data(%s)

# Print the response
print(response)

Documentation

For more information, please refer to the documentation: https://example.com/documentation.

API Reference

The following is a list of available API endpoints:

  • GET /users: Retrieves a list of all users.
  • GET /users/{id}: Retrieves a specific user by ID.
  • POST /users: Creates a new user.
  • PUT /users/{id}: Updates an existing user.
  • DELETE /users/{id}: Deletes a user.

Contributing

We welcome contributions from the community. If you'd like to contribute, please follow the guidelines in the contributing document.

License

This package is licensed under the MIT License. See the LICENSE file for more information.

Troubleshooting

If you're having trouble with the package, please check the following:

  • Make sure you have the latest version of the package installed.
  • Check the logs for any errors.
  • Consult the FAQ for common issues and solutions.

If you're still having trouble, please contact us for support.

ResourceMethodsEndpoints
CartGET/wc/store/v1/cart
POST/wc/store/v1/cart/add-item
POST/wc/store/v1/cart/remove-item
POST/wc/store/v1/cart/update-item
POST/wc/store/v1/cart/apply-coupon
POST/wc/store/v1/cart/remove-coupon
POST/wc/store/v1/cart/update-customer
POST/wc/store/v1/cart/select-shipping-rate
Cart ItemsGET, POST, DELETE/wc/store/v1/cart/items
GET, POST, PUT, DELETE/wc/store/v1/cart/items/:key
Cart CouponsGET, POST, DELETE/wc/store/v1/cart/coupons
GET, DELETE/wc/store/v1/cart/coupon/:code
CheckoutGET, POST, PUT/wc/store/v1/checkout
Checkout orderPOST/wc/store/v1/checkout/:id
OrderGET/wc/store/v1/order/:id
ProductsGET/wc/store/v1/products
GET/wc/store/v1/products/:id
Product Collection DataGET/wc/store/v1/products/collection-data
Product AttributesGET/wc/store/v1/products/attributes
GET/wc/store/v1/products/attributes/:id
Product Attribute TermsGET/wc/store/v1/products/attributes/:id/terms
Product CategoriesGET/wc/store/v1/products/categories
Product BrandsGET/wc/store/v1/products/brands
Product ReviewsGET/wc/store/v1/products/reviews
Product TagsGET/wc/store/v1/products/tags

分页

如果集合包含大量结果,则可能会进行分页。 在列出资源时,您可以传递以下参数:

参数描述
page集合的当前页。 默认为 1
per_page结果集中返回的最大项目数。 默认为 10。 最大值为 100

在下面的示例中,我们每页列出 20 个产品,并返回第 2 页。

curl "https://example-store.com/wp-json/wc/store/v1/products?page=2&per_page=20"

此外,还会发送额外的分页头部,其中包含其它信息。

头部描述
X-WP-Total集合中项目的总数。
X-WP-TotalPages集合中页面的总数。
Link包含指向其他页面的链接;next(下一页)、prev(上一页)和 up(上级),如果适用。

Status codes

The following table gives an overview of how the API functions generally behave.

Request typeDescription
GETAccess one or more resources and return 200 OK and the result as JSON.
POSTReturn 201 Created if the resource is successfully created and return the newly created resource as JSON.
PUTReturn 200 OK if the resource is modified successfully. The modified result is returned as JSON.
DELETEReturns 204 No Content if the resource was deleted successfully.

The following table shows the possible return codes for API requests.

Response codeDescription
200 OKThe request was successful, the resource(s) itself is returned as JSON.
204 No ContentThe server has successfully fulfilled the request and that there is no additional content to send in the response payload body.
201 CreatedThe POST request was successful and the resource is returned as JSON.
400 Bad RequestA required attribute of the API request is missing.
403 ForbiddenThe request is not allowed.
404 Not FoundA resource could not be accessed, for example it doesn't exist.
405 Method Not AllowedThe request is not supported.
409 ConflictThe request could not be completed due to a conflict with the current state of the target resource. The current state may also be returned.
500 Server ErrorWhile handling the request something went wrong server-side.

贡献

Store API 中的每个路由主要包含 3 个部分:

  1. 路由 (Route) - 负责将请求映射到端点。Store API 中的路由扩展 AbstractRoute 类;该类包含处理请求和返回 JSON 回复的共享功能。路由确保返回有效的回复,并处理集合、错误和分页。
  2. 结构化数据 (Schema) - 路由不负责格式化资源。我们使用 结构化数据 类来表示每种类型的资源,例如,一个产品、一个购物车或一个购物车项目。Store API 中的结构化数据类应扩展 AbstractSchema 类。
  3. 实用工具 (Utility) - 在更高级的场景中,当 Store API 需要访问 WooCommerce 核心中的复杂数据,或者多个路由需要访问相同的数据时,路由应使用控制器或实用工具类。例如,Store API 具有订单控制器和购物车控制器,分别用于查找订单和购物车数据。

通常,路由处理以下类型的请求:

  • GET 请求用于读取产品、购物车或结账数据。
  • POSTPUT 请求用于更新购物车和结账数据。
  • DELETE 请求用于移除购物车数据。
  • OPTIONS 请求用于检索当前路由的 JSON 结构化数据。

请查看 Store API 核心原则。 这涵盖了我们的开发方法,以及诸如版本控制、哪些数据可以包含以及如何构建新路由等主题。

可扩展性

Store API 的可扩展性是通过将某些路由和结构化数据暴露给 ExtendSchema 类来实现的。有关贡献者在此方面的文档,请访问 此处

如果某个路由包含可扩展性界面,第三方开发者可以使用共享的 ExtendSchema::class 实例来注册额外的端点数据和额外的结构化数据。

这与传统的过滤器钩子方法不同,它更具限制性,但可以减少第三方扩展破坏现有路由和端点或覆盖其他应用程序可能依赖的返回数据的可能性。

如果需要新的结构化数据,并且以下任何一条语句为真,请选择 扩展 Store API,而不是向现有的 Store API 结构化数据中添加新的结构化数据:

  • 数据是扩展的一部分,而不是核心功能。
  • 数据与某个资源相关,但从技术上讲不是该资源的一部分。
  • 数据难以查询(在性能方面),或者具有非常狭窄或特定的使用场景。

如果数据是敏感的(例如,一个核心设置,应该保持私有),或者与当前用户无关(例如,通过订单 ID 查找订单),请 选择使用经过身份验证的 WC REST API

如果您想添加 新的路由和端点,而不是扩展 Store API 的 结构化数据,则无需扩展 Store API。 您可以使用 WordPress 的核心功能来创建新的路由,如果您希望,可以选择使用与 Store API 相同的模式。 参见: