跳到主要内容
将当前页面内容以 Markdown 格式复制到剪贴板

优惠券

优惠券 API 允许您创建、查看、更新和删除单个或一批优惠券代码。

优惠券属性

AttributeTypeDescription
idintegerUnique identifier for the object. READ-ONLY
codestringCoupon code. MANDATORY
amountstringThe amount of discount. Should always be numeric, even if setting a percentage.
date_createddate-timeThe date the coupon was created, in the site's timezone. READ-ONLY
date_created_gmtdate-timeThe date the coupon was created, as GMT. READ-ONLY
date_modifieddate-timeThe date the coupon was last modified, in the site's timezone. READ-ONLY
date_modified_gmtdate-timeThe date the coupon was last modified, as GMT. READ-ONLY
discount_typestringDetermines the type of discount that will be applied. Options: percent, fixed_cart and fixed_product. Default is fixed_cart.
descriptionstringCoupon description.
date_expiresstringThe date the coupon expires, in the site's timezone.
date_expires_gmtstringThe date the coupon expires, as GMT.
usage_countintegerNumber of times the coupon has been used already. READ-ONLY
individual_usebooleanIf true, the coupon can only be used individually. Other applied coupons will be removed from the cart. Default is false.
product_idsarrayList of product IDs the coupon can be used on.
excluded_product_idsarrayList of product IDs the coupon cannot be used on.
usage_limitintegerHow many times the coupon can be used in total.
usage_limit_per_userintegerHow many times the coupon can be used per customer.
limit_usage_to_x_itemsintegerMax number of items in the cart the coupon can be applied to.
free_shippingbooleanIf true and if the free shipping method requires a coupon, this coupon will enable free shipping. Default is false.
product_categoriesarrayList of category IDs the coupon applies to.
excluded_product_categoriesarrayList of category IDs the coupon does not apply to.
exclude_sale_itemsbooleanIf true, this coupon will not be applied to items that have sale prices. Default is false.
minimum_amountstringMinimum order amount that needs to be in the cart before coupon applies.
maximum_amountstringMaximum order amount allowed when using the coupon.
email_restrictionsarrayList of email addresses that can use this coupon.
used_byarrayList of user IDs (or guest email addresses) that have used the coupon. READ-ONLY
meta_dataarrayMeta data. See Coupon - Meta data properties

Coupon - Meta data properties

AttributeTypeDescription
idintegerMeta ID. READ-ONLY
keystringMeta key.
valuestringMeta value.

创建优惠券

这个 API 可以帮助您创建新的优惠券。

POST /wp-json/wc/v3/coupons
curl -X POST https://example.com/wp-json/wc/v3/coupons \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"code": "10off",
"discount_type": "percent",
"amount": "10",
"individual_use": true,
"exclude_sale_items": true,
"minimum_amount": "100.00"
}'

获取优惠券

此 API 允许您通过编号获取并查看特定的优惠券。

GET /wp-json/wc/v3/coupons/<id>
curl https://example.com/wp-json/wc/v3/coupons/719 \
-u consumer_key:consumer_secret

列出所有优惠券

此 API 帮助您列出所有已创建的优惠券。

GET /wp-json/wc/v3/coupons
curl https://example.com/wp-json/wc/v3/coupons \
-u consumer_key:consumer_secret

Available parameters

ParameterTypeDescription
contextstringScope under which the request is made; determines fields present in response. Options: view and edit. Default is view.
pageintegerCurrent page of the collection. Default is 1.
per_pageintegerMaximum number of items to be returned in result set. Default is 10.
searchstringLimit results to those matching a string.
afterstringLimit response to resources published after a given ISO8601 compliant date.
beforestringLimit response to resources published before a given ISO8601 compliant date.
modified_afterstringLimit response to resources modified after a given ISO8601 compliant date.
modified_beforestringLimit response to resources modified after a given ISO8601 compliant date.
dates_are_gmtbooleanWhether to interpret dates as GMT when limiting response by published or modified date.
excludearrayEnsure result set excludes specific IDs.
includearrayLimit result set to specific ids.
offsetintegerOffset the result set by a specific number of items.
orderstringOrder sort attribute ascending or descending. Options: asc and desc. Default is desc.
orderbystringSort collection by object attribute. Options: date, modified, id, include, title and slug. Default is date.
codestringLimit result set to resources with a specific code.

Update a coupon

This API lets you make changes to a coupon.

PUT /wp-json/wc/v3/coupons/<id>
curl -X PUT https://example.com/wp-json/wc/v3/coupons/719 \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"amount": "5"
}'

删除优惠券

此 API 可以帮助您删除一个优惠券。

DELETE /wp-json/wc/v3/coupons/<id>
curl -X DELETE https://example.com/wp-json/wc/v3/coupons/719?force=true \
-u consumer_key:consumer_secret

可用参数

参数类型描述
force字符串使用 true 是否永久删除优惠券,默认值为 false

批量更新优惠券

此 API 可以帮助您批量创建、更新和删除多个优惠券。

备注

注意:默认情况下,限制为最多可以创建、更新或删除 100 个对象。

POST /wp-json/wc/v3/coupons/batch
curl -X POST https://example.com/wp-json/wc/v3/coupons/batch \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"create": [
{
"code": "20off",
"discount_type": "percent",
"amount": "20",
"individual_use": true,
"exclude_sale_items": true,
"minimum_amount": "100.00"
},
{
"code": "30off",
"discount_type": "percent",
"amount": "30",
"individual_use": true,
"exclude_sale_items": true,
"minimum_amount": "100.00"
}
],
"update": [
{
"id": 719,
"minimum_amount": "50.00"
}
],
"delete": [
720
]
}'