wordpress之woocommer代码查询手册---product产品篇
Products
This section lists all API that can be used to create, edit or otherwise manipulate products.
Products Properties
Attribute | Type | Description |
---|
title | string | Product name |
id | integer | Product ID (post ID) read-only |
created_at | string | UTC DateTime when the product was created read-only |
updated_at | string | UTC DateTime when the product was last updated read-only |
type | string | Product type. By default in WooCommerce the following types are available: simple , grouped , external , variable . Default is simple |
status | string | Product status (post status). Default is publish |
downloadable | boolean | If the product is downloadable or not. Downloadable products give access to a file upon purchase |
virtual | boolean | If the product is virtual or not. Virtual products are intangible and aren’t shipped |
permalink | string | Product URL (post permalink) read-only |
sku | string | SKU refers to a Stock-keeping unit, a unique identifier for each distinct product and service that can be purchased |
price | float | Current product price. This is setted from regular_price and sale_price read-only |
regular_price | float | Product regular price |
sale_price | float | Product sale price |
sale_price_dates_from | float | Sets the sale start date. Date in the YYYY-MM-DD format write-only |
sale_price_dates_to | float | Sets the sale end date. Date in the YYYY-MM-DD format write-only |
price_html | string | Price formatted in HTML, e.g. $ 3.00$ 2.00 read-only |
taxable | boolean | Show if the product is taxable or not read-only |
tax_status | string | Tax status. The options are: taxable , shipping (Shipping only) and none |
tax_class | string | Tax class |
managing_stock | boolean | Enable stock management at product level |
stock_quantity | integer | Stock quantity. If is a variable product this value will be used to control stock for all variations, unless you define stock at variation level. |
in_stock | boolean | Controls whether or not the product is listed as “in stock” or “out of stock” on the frontend. |
backorders_allowed | boolean | Shows if backorders are allowed read-only |
backordered | boolean | Shows if a product is on backorder (if the product have the stock_quantity negative) read-only |
backorders | mixed | If managing stock, this controls whether or not backorders are allowed. If enabled, stock quantity can go below 0. The options are: false (Do not allow), notify (Allow, but notify customer), and true (Allow) write-only |
sold_individually | boolean | When true this only allow one item to be bought in a single order |
purchaseable | boolean | Shows if the product can be bought read-only |
featured | boolean | Featured Product |
visible | boolean | Shows whether or not the product is visible in the catalog read-only |
catalog_visibility | string | Catalog visibility. The following options are available: visible (Catalog and search), catalog (Only in catalog), search (Only in search) and hidden (Hidden from all). Default is visible |
on_sale | boolean | Shows if the product is on sale or not read-only |
weight | string | Product weight in decimal format |
dimensions | array | List of the product dimensions. See Dimensions Properties |
shipping_required | boolean | Shows if the product need to be shipped or not read-only |
shipping_taxable | boolean | Shows whether or not the product shipping is taxable read-only |
shipping_class | string | Shipping class slug. Shipping classes are used by certain shipping methods to group similar products |
shipping_class_id | integer | Shipping class ID read-only |
description | string | Product description |
enable_html_description | bool | Enable HTML for product description write-only |
short_description | string | Product short description |
enable_html_short_description | string | Enable HTML for product short description write-only |
reviews_allowed | boolean | Shows/define if reviews are allowed |
average_rating | string | Reviews average rating read-only |
rating_count | integer | Amount of reviews that the product have read-only |
related_ids | array | List of related products IDs (integer ) read-only |
upsell_ids | array | List of up-sell products IDs (integer ). Up-sells are products which you recommend instead of the currently viewed product, for example, products that are more profitable or better quality or more expensive |
cross_sell_ids | array | List of cross-sell products IDs. Cross-sells are products which you promote in the cart, based on the current product |
parent_id | integer | Product parent ID (post_parent) |
categories | array | List of product categories names (string ). In write-mode need to pass a array of categories IDs (integer ) (uses wp_set_object_terms()) |
tags | array | List of product tags names (string ). In write-mode need to pass a array of tags IDs (integer ) (uses wp_set_object_terms()) |
images | array | List of products images. See Images Properties |
featured_src | string | Featured image URL read-only |
attributes | array | List of product attributes. See Attributes Properties. Note: the attribute must be registered in WooCommerce before. |
default_attributes | array | Defaults variation attributes. These are the attributes that will be pre-selected on the frontend. See Default Attributes Properties write-only |
downloads | array | List of downloadable files. See Downloads Properties |
download_limit | integer | Amount of times the product can be downloaded. In write-mode you can sent a blank string for unlimited re-downloads. e.g '' |
download_expiry | integer | Number of days that the customer has up to be able to download the product. In write-mode you can sent a blank string for never expiry. e.g '' |
download_type | string | Download type, this controls the schema. The available options are: '' (Standard Product), application (Application/Software) and music (Music) |
purchase_note | string | Optional note to send the customer after purchase. |
total_sales | integer | Amount of sales read-only |
variations | array | List of products variations. See Variations Properties |
parent | array | List the product parent data when query for a variation read-only |
product_url | string | Product external URL. Only for external products write-only |
button_text | string | Product external button text. Only for external products write-only |
Dimensions Properties
Attribute | Type | Description |
---|
length | string | Product length in decimal format |
width | string | Product width in decimal format |
height | string | Product height in decimal format |
unit | string | Product name read-only |
Images Properties
Attribute | Type | Description |
---|
id | integer | Image ID (attachment ID) |
created_at | string | UTC DateTime when the image was created read-only |
updated_at | string | UTC DateTime when the image was last updated read-only |
src | string | Image URL. In write-mode you can use to send new images |
title | string | Image title (attachment title) read-only |
alt | string | Image alt text (attachment image alt text) read-only |
position | integer | Image position. 0 means that the image is featured |
Attributes Properties
Attribute | Type | Description |
---|
name | string | Attribute name required |
slug | string | Attribute slug |
position | integer | Attribute position |
visible | boolean | Shows/define if the attribute is visible on the “Additional Information” tab in the product’s page |
variation | boolean | Shows/define if the attribute can be used as variation |
options | array | List of available term names of the attribute |
Default Attributes Properties
Attribute | Type | Description |
---|
name | string | Attribute name |
slug | string | Attribute slug |
option | string | Selected term name of the attribute |
Downloads Properties
Attribute | Type | Description |
---|
id | string | File ID (file md5 hash) read-only |
name | string | File name |
file | string | File URL. In write-mode you can use this property to send new files |
Variations Properties
Attribute | Type | Description |
---|
id | integer | Variation ID (post ID) read-only |
created_at | string | UTC DateTime when the variation was created read-only |
updated_at | string | UTC DateTime when the variation was last updated read-only |
downloadable | boolean | If the variation is downloadable or not. Downloadable variations give access to a file upon purchase |
virtual | boolean | If the variation is virtual or not. Virtual variations are intangible and aren’t shipped |
permalink | string | Variation URL (post permalink) read-only |
sku | string | SKU refers to a Stock-keeping unit, a unique identifier for each distinct product and service that can be purchased |
price | float | Current variation price. This is setted from regular_price and sale_price read-only |
regular_price | float | Variation regular price |
sale_price | float | Variation sale price |
sale_price_dates_from | float | Sets the sale start date. Date in the YYYY-MM-DD format write-only |
sale_price_dates_to | float | Sets the sale end date. Date in the YYYY-MM-DD format write-only |
taxable | boolean | Show if the variation is taxable or not read-only |
tax_status | string | Tax status. The options are: taxable , shipping (Shipping only) and none |
tax_class | string | Tax class |
managing_stock | boolean | Enable stock management at variation level |
stock_quantity | integer | Stock quantity. If is a variable variation this value will be used to control stock for all variations, unless you define stock at variation level. |
in_stock | boolean | Controls whether or not the variation is listed as “in stock” or “out of stock” on the frontend. |
backordered | boolean | Shows if a variation is on backorder (if the variation have the stock_quantity negative) read-only |
purchaseable | boolean | Shows if the variation can be bought read-only |
visible | boolean | Shows whether or not the product parent is visible in the catalog read-only |
on_sale | boolean | Shows if the variation is on sale or not read-only |
weight | string | Variation weight in decimal format |
dimensions | array | List of the variation dimensions. See Dimensions Properties |
shipping_class | string | Shipping class slug. Shipping classes are used by certain shipping methods to group similar products |
shipping_class_id | integer | Shipping class ID read-only |
images | array | Variation featured image. See Images Properties |
attributes | array | List of variation attributes. Similar to a simple or variable product, but for variation indicate the attributes used to form the variation. See Attributes Properties |
downloads | array | List of downloadable files. See Downloads Properties |
download_limit | integer | Amount of times the variation can be downloaded. In write-mode you can sent a blank string for unlimited re-downloads. e.g '' |
download_expiry | integer | Number of days that the customer has up to be able to download the varition. In write-mode you can sent a blank string for never expiry. e.g '' |
Create A Product
This API helps you to create a new product.
HTTP Request
POST
/wc-api/v3/products
Example of how to create a simple
product:
curl -X POST https://example.com/wc-api/v3/products \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"product": {
"title": "Premium Quality",
"type": "simple",
"regular_price": "21.99",
"description": "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.",
"short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
"categories": [
9,
14
],
"images": [
{
"src": "http://example.com/wp-content/uploads/2015/01/premium-quality-front.jpg",
"position": 0
},
{
"src": "http://example.com/wp-content/uploads/2015/01/premium-quality-back.jpg",
"position": 1
}
]
}
}'1234567891011121314151617181920212223242526
var data = {
product: {
title: 'Premium Quality',
type: 'simple',
regular_price: '21.99',
description: '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.',
short_description: 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
categories: [ 9, 14 ],
images: [ {
src: 'http://example.com/wp-content/uploads/2015/01/premium-quality-front.jpg',
position: 0 }, {
src: 'http://example.com/wp-content/uploads/2015/01/premium-quality-back.jpg',
position: 1 } ] }};WooCommerce.post('products', data, function(err, data, res) {
console.log(res);});123456789101112131415161718192021222324252627
data = { "product": { "title": "Premium Quality", "type": "simple", "regular_price": "21.99", "description": "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.", "short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.", "categories": [ 9, 14 ], "images": [ { "src": "http://example.com/wp-content/uploads/2015/01/premium-quality-front.jpg", "position": 0 }, { "src": "http://example.com/wp-content/uploads/2015/01/premium-quality-back.jpg", "position": 1 } ] }}print(wcapi.post("products", data).json())12345678910111213141516171819202122232425
data = {
product: {
title: "Premium Quality",
type: "simple",
regular_price: "21.99",
description: "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.",
short_description: "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
categories: [ 9, 14 ],
images: [ {
src: "http://example.com/wp-content/uploads/2015/01/premium-quality-front.jpg",
position: 0 }, {
src: "http://example.com/wp-content/uploads/2015/01/premium-quality-back.jpg",
position: 1 } ] }}woocommerce.post("products", data).parsed_response12345678910111213141516171819202122232425
JSON response example:
{ "product": { "title": "Premium Quality", "id": 546, "created_at": "2015-01-22T19:46:16Z", "updated_at": "2015-01-22T19:46:16Z", "type": "simple", "status": "publish", "downloadable": false, "virtual": false, "permalink": "https://example.com/product/premium-quality/", "sku": "", "price": "21.99", "regular_price": "21.99", "sale_price": null, "price_html": "$ 21.99", "taxable": true, "tax_status": "taxable", "tax_class": "", "managing_stock": false, "stock_quantity": 0, "in_stock": true, "backorders_allowed": false, "backordered": false, "sold_individually": false, "purchaseable": true, "featured": false, "visible": true, "catalog_visibility": "visible", "on_sale": false, "weight": null, "dimensions": { "length": "", "width": "", "height": "", "unit": "cm" }, "shipping_required": true, "shipping_taxable": true, "shipping_class": "", "shipping_class_id": null, "description": "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.\n", "short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.\n", "reviews_allowed": true, "average_rating": "0.00", "rating_count": 0, "related_ids": [ 37, 47, 31, 19, 22 ], "upsell_ids": [], "cross_sell_ids": [], "parent_id": 0, "categories": [ "Clothing", "T-shirts" ], "tags": [], "images": [ { "id": 547, "created_at": "2015-01-22T19:46:16Z", "updated_at": "2015-01-22T19:46:16Z", "src": "http://example.com/wp-content/uploads/2015/01/premium-quality-front.jpg", "title": "", "alt": "", "position": 0 }, { "id": 548, "created_at": "2015-01-22T19:46:17Z", "updated_at": "2015-01-22T19:46:17Z", "src": "http://example.com/wp-content/uploads/2015/01/premium-quality-back.jpg", "title": "", "alt": "", "position": 1 } ], "featured_src": "http://example.com/wp-content/uploads/2015/01/premium-quality-front.jpg", "attributes": [], "downloads": [], "download_limit": 0, "download_expiry": 0, "download_type": "", "purchase_note": "", "total_sales": 0, "variations": [], "parent": [] }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
Example of how to create a variable
product:
curl -X POST https://example.com/wc-api/v3/products \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"product": {
"title": "Ship Your Idea",
"type": "variable",
"description": "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.",
"short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
"categories": [
9,
14
],
"images": [
{
"src": "http://example.com/wp-content/uploads/2015/01/ship-your-idea-black-front.jpg",
"position": 0
},
{
"src": "http://example.com/wp-content/uploads/2015/01/ship-your-idea-black-back.jpg",
"position": 1
},
{
"src": "http://example.com/wp-content/uploads/2015/01/ship-your-idea-green-front.jpg",
"position": 2
},
{
"src": "http://example.com/wp-content/uploads/2015/01/ship-your-idea-green-back.jpg",
"position": 3
}
],
"attributes": [
{
"name": "Color",
"slug": "color",
"position": "0",
"visible": false,
"variation": true,
"options": [
"Black",
"Green"
]
}
],
"default_attributes": [
{
"name": "Color",
"slug": "color",
"option": "Black"
}
],
"variations": [
{
"regular_price": "19.99",
"image": [
{
"src": "http://example.com/wp-content/uploads/2015/01/ship-your-idea-black-front.jpg",
"position": 0
}
],
"attributes": [
{
"name": "Color",
"slug": "color",
"option": "black"
}
]
},
{
"regular_price": "19.99",
"image": [
{
"src": "http://example.com/wp-content/uploads/2015/01/ship-your-idea-green-front.jpg",
"position": 0
}
],
"attributes": [
{
"name": "Color",
"slug": "color",
"option": "green"
}
]
}
]
}
}'123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
var data = {
product: {
title: 'Ship Your Idea',
type: 'variable',
description: '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.',
short_description: 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
categories: [ 9, 14 ],
images: [ {
src: 'http://example.com/wp-content/uploads/2015/01/ship-your-idea-black-front.jpg',
position: 0 }, {
src: 'http://example.com/wp-content/uploads/2015/01/ship-your-idea-black-back.jpg',
position: 1 }, {
src: 'http://example.com/wp-content/uploads/2015/01/ship-your-idea-green-front.jpg',
position: 2 }, {
src: 'http://example.com/wp-content/uploads/2015/01/ship-your-idea-green-back.jpg',
position: 3 } ],
attributes: [ {
name: 'Color',
slug: 'color',
position: '0',
visible: false,
variation: true,
options: [ 'Black', 'Green' ] } ],
default_attributes: [ {
name: 'Color',
slug: 'color',
option: 'Black' } ],
variations: [ {
regular_price: '19.99',
image: [ {
src: 'http://example.com/wp-content/uploads/2015/01/ship-your-idea-black-front.jpg',
position: 0 } ],
attributes: [ {
name: 'Color',
slug: 'color',
option: 'black' } ] }, {
regular_price: '19.99',
image: [ {
src: 'http://example.com/wp-content/uploads/2015/01/ship-your-idea-green-front.jpg',
position: 0 } ],
attributes: [ {
name: 'Color',
slug: 'color',
option: 'green' } ] } ] }};WooCommerce.post('products', data, function(err, data, res) {
console.log(res);});12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
data = { "product": { "title": "Ship Your Idea", "type": "variable", "description": "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.", "short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.", "categories": [ 9, 14 ], "images": [ { "src": "http://example.com/wp-content/uploads/2015/01/ship-your-idea-black-front.jpg", "position": 0 }, { "src": "http://example.com/wp-content/uploads/2015/01/ship-your-idea-black-back.jpg", "position": 1 }, { "src": "http://example.com/wp-content/uploads/2015/01/ship-your-idea-green-front.jpg", "position": 2 }, { "src": "http://example.com/wp-content/uploads/2015/01/ship-your-idea-green-back.jpg", "position": 3 } ], "attributes": [ { "name": "Color", "slug": "color", "position": "0", "visible": False, "variation": True, "options": [ "Black", "Green" ] } ], "default_attributes": [ { "name": "Color", "slug": "color", "option": "Black" } ], "variations": [ { "regular_price": "19.99", "image": [ { "src": "http://example.com/wp-content/uploads/2015/01/ship-your-idea-black-front.jpg", "position": 0 } ], "attributes": [ { "name": "Color", "slug": "color", "option": "black" } ] }, { "regular_price": "19.99", "image": [ { "src": "http://example.com/wp-content/uploads/2015/01/ship-your-idea-green-front.jpg", "position": 0 } ], "attributes": [ { "name": "Color", "slug": "color", "option": "green" } ] } ] }}print(wcapi.post("products", data).json())1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
data = {
product: {
title: "Ship Your Idea",
type: "variable",
description: "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.",
short_description: "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
categories: [ 9, 14 ],
images: [ {
src: "http://example.com/wp-content/uploads/2015/01/ship-your-idea-black-front.jpg",
position: 0 }, {
src: "http://example.com/wp-content/uploads/2015/01/ship-your-idea-black-back.jpg",
position: 1 }, {
src: "http://example.com/wp-content/uploads/2015/01/ship-your-idea-green-front.jpg",
position: 2 }, {
src: "http://example.com/wp-content/uploads/2015/01/ship-your-idea-green-back.jpg",
position: 3 } ],
attributes: [ {
name: "Color",
slug: "color",
position: "0",
visible: false,
variation: true,
options: [ "Black", "Green" ] } ],
default_attributes: [ {
name: "Color",
slug: "color",
option: "Black" } ],
variations: [ {
regular_price: "19.99",
image: [ {
src: "http://example.com/wp-content/uploads/2015/01/ship-your-idea-black-front.jpg",
position: 0 } ],
attributes: [ {
name: "Color",
slug: "color",
option: "black" } ] }, {
regular_price: "19.99",
image: [ {
src: "http://example.com/wp-content/uploads/2015/01/ship-your-idea-green-front.jpg",
position: 0 } ],
attributes: [ {
&