WooCommerce 文档

title: "WooCommerce 核心 API 中的关键流程" post_status: publish comment_status: open taxonomy: category: - woocommerce post_tag: - Contributing - Contribution - Repos


WooCommerce 核心 API 中的关键流程

在我们的文档中,我们已经确定了 WooCommerce 核心 API 中的关键用户流程。这些流程是我们测试工作的指南,帮助我们集中精力在最重要的方面。它们也为评估修改的影响以及确定问题优先级提供了宝贵的见解。

需要注意的是,这些流程是动态的,会随着平台的发展而不断演变。它们会定期进行更新、添加和重新排序,以适应我们系统不断变化的需求。

产品

路由 流程名称 接口 测试文件
产品 可以查看所有产品 /wp-json/wc/v3/products tests/api-core-tests/tests/products/product-list.test.js
产品 可以搜索产品 /wp-json/wc/v3/products tests/api-core-tests/tests/products/product-list.test.js
产品 可以添加一个简单产品 /wp-json/wc/v3/products tests/api-core-tests/tests/products/products-crud.test.js
产品 可以添加一个可变产品 /wp-json/wc/v3/products tests/api-core-tests/tests/products/products-crud.test.js
产品 可以添加一个虚拟产品 /wp-json/wc/v3/products tests/api-core-tests/tests/products/products-crud.test.js
产品 可以查看单个产品 /wp-json/wc/v3/products/{id} tests/api-core-tests/tests/products/products-crud.test.js
产品 可以更新产品 /wp-json/wc/v3/products/{id} tests/api-core-tests/tests/products/products-crud.test.js
产品 可以删除产品 /wp-json/wc/v3/products/{id} tests/api-core-tests/tests/products/products-crud.test.js

订单

路由 流程名称 接口地址 测试文件
订单 可以创建订单 /wp-json/wc/v3/orders tests/api-core-tests/tests/orders/orders-crud.test.js
订单 可以查看单个订单 /wp-json/wc/v3/orders/{id} tests/api-core-tests/tests/orders/orders-crud.test.js
订单 可以更新订单 /wp-json/wc/v3/orders/{id} tests/api-core-tests/tests/orders/orders-crud.test.js
订单 可以删除订单 /wp-json/wc/v3/orders/{id} tests/api-core-tests/tests/orders/orders-crud.test.js
订单 可以查看所有订单 /wp-json/wc/v3/orders tests/api-core-tests/tests/orders/orders.test.js
订单 可以搜索订单 /wp-json/wc/v3/orders tests/api-core-tests/tests/orders/order-search.test.js
订单 可以新增复杂订单 - 包含多种产品类型和税类 /wp-json/wc/v3/orders tests/api-core-tests/tests/orders/order-complex.test.js

退款

路由 流程名称 接口地址 测试文件
退款 可以退款订单 /wp-json/wc/v3/orders/{id}/refunds tests/api-core-tests/tests/refunds/refund.test.js

优惠券

路由 流程名称 接口地址 测试文件
优惠券 可以创建优惠券 /wp-json/wc/v3/coupons tests/api-core-tests/tests/coupons/coupons.test.js
优惠券 可以更新优惠券 /wp-json/wc/v3/coupons/{id} tests/api-core-tests/tests/coupons/coupons.test.js
优惠券 可以删除优惠券 /wp-json/wc/v3/coupons/{id} tests/api-core-tests/tests/coupons/coupons.test.js
优惠券 可以将优惠券添加到订单 /wp-json/wc/v3/orders/{id}/coupons tests/api-core-tests/tests/coupons/coupons.test.js

配送

路线 流程名称 接口 测试文件
配送区域 可以创建配送区域 /wp-json/wc/v3/shipping/zones tests/api-core-tests/tests/shipping/shipping-zones.test.js
配送方式 可以为配送区域创建配送方式 /wp-json/wc/v3/shipping/zones/{id}/methods n/a
配送类型 可以创建产品配送类型 /wp-json/wc/v3/products/shipping_classes tests/api-core-tests/tests/products/products-crud.test.js

Shipping Classes

This document describes how to use the API to manage Shipping Classes.

Creating a Shipping Class

To create a new Shipping Class, you can use the following API endpoint:

POST /wp-json/wc/v3/products/shipping_classes

The request body should contain the following parameters:

For example:

{
  "name": "Expedited Shipping",
  "description": "Faster shipping option",
  "method_id": 123
}

Updating a Shipping Class

To update an existing Shipping Class, you can use the following API endpoint:

PUT /wp-json/wc/v3/products/shipping_classes/%d

where %d is the ID of the Shipping Class you want to update.

The request body should contain the parameters you want to update.

Deleting a Shipping Class

To delete a Shipping Class, you can use the following API endpoint:

DELETE /wp-json/wc/v3/products/shipping_classes/%d

where %d is the ID of the Shipping Class you want to delete.

Example Usage

Here is an example of how to create a new Shipping Class using the API:

import requests

url = "https://example.com/wp-json/wc/v3/products/shipping_classes"
headers = {"Content-Type": "application/json"}
data = {
  "name": "Expedited Shipping",
  "description": "Faster shipping option",
  "method_id": 123
}

response = requests.post(url, headers=headers, json=data)

if response.status_code == 201:
  print("Shipping Class created successfully!")
  print(response.json())
else:
  print("Error creating Shipping Class:")
  print(response.status_code)
  print(response.text)

This example uses the requests library to make an HTTP POST request to the API endpoint. The headers dictionary specifies the Content-Type header, which indicates that the request body is in JSON format. The data dictionary contains the parameters for the new Shipping Class.

The response from the API is checked to see if it was successful. If the status code is 201 (Created), then the Shipping Class was created successfully. The response body contains the details of the new Shipping Class. If the status code is not 201, then an error occurred, and the error message is printed.

Important Considerations

This document provides a basic overview of how to use the API to manage Shipping Classes. For more information, please refer to the API documentation.

Testing

The following TEST cases are available for verifying the functionality of the Shipping Classes API:

These TEST cases can be run using the tests/api-core-tests/tests/products/products-crud.test.js File.

Troubleshooting

If you are having trouble using the API, please consult the following resources:

If you are still unable to resolve the issue, please contact the support team for assistance.

Error Codes

The following error codes may be returned by the API:

Pro features may require a paid subscription.

Shipping Zones

You can create Shipping Zones to define different shipping rates for different regions. Each Shipping Zone can have multiple Shipping Methods associated with it.

Shipping Methods

Shipping Methods define how products are shipped. You can create different Shipping Methods for different Shipping Classes and Shipping Zones.

Core functionality is available in the free version.

Low latency is important for a good user experience.

In some cases, you may need to manually configure the Shipping Classes.

This is one example.

Or you can use a different approach.

The value is n/a.

The Name of the Product is required.

The Method is not supported.

The Class is not defined.