wordpress之woocommer代码查询手册---product产品篇

Products

This section lists all API that can be used to create, edit or otherwise manipulate products.

Products Properties

AttributeTypeDescription
titlestringProduct name
idintegerProduct ID (post ID) read-only
created_atstringUTC DateTime when the product was created read-only
updated_atstringUTC DateTime when the product was last updated read-only
typestringProduct type. By default in WooCommerce the following types are available: simple, grouped, external, variable. Default is simple
statusstringProduct status (post status). Default is publish
downloadablebooleanIf the product is downloadable or not. Downloadable products give access to a file upon purchase
virtualbooleanIf the product is virtual or not. Virtual products are intangible and aren’t shipped
permalinkstringProduct URL (post permalink) read-only
skustringSKU refers to a Stock-keeping unit, a unique identifier for each distinct product and service that can be purchased
pricefloatCurrent product price. This is setted from regular_price and sale_price read-only
regular_pricefloatProduct regular price
sale_pricefloatProduct sale price
sale_price_dates_fromfloatSets the sale start date. Date in the YYYY-MM-DD format write-only
sale_price_dates_tofloatSets the sale end date. Date in the YYYY-MM-DD format write-only
price_htmlstringPrice formatted in HTML, e.g. $ 3.00$ 2.00 read-only
taxablebooleanShow if the product is taxable or not read-only
tax_statusstringTax status. The options are: taxable, shipping (Shipping only) and none
tax_classstringTax class
managing_stockbooleanEnable stock management at product level
stock_quantityintegerStock 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_stockbooleanControls whether or not the product is listed as “in stock” or “out of stock” on the frontend.
backorders_allowedbooleanShows if backorders are allowed read-only
backorderedbooleanShows if a product is on backorder (if the product have the stock_quantity negative) read-only
backordersmixedIf 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_individuallybooleanWhen true this only allow one item to be bought in a single order
purchaseablebooleanShows if the product can be bought read-only
featuredbooleanFeatured Product
visiblebooleanShows whether or not the product is visible in the catalog read-only
catalog_visibilitystringCatalog 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_salebooleanShows if the product is on sale or not read-only
weightstringProduct weight in decimal format
dimensionsarrayList of the product dimensions. See Dimensions Properties
shipping_requiredbooleanShows if the product need to be shipped or not read-only
shipping_taxablebooleanShows whether or not the product shipping is taxable read-only
shipping_classstringShipping class slug. Shipping classes are used by certain shipping methods to group similar products
shipping_class_idintegerShipping class ID read-only
descriptionstringProduct description
enable_html_descriptionboolEnable HTML for product description write-only
short_descriptionstringProduct short description
enable_html_short_descriptionstringEnable HTML for product short description write-only
reviews_allowedbooleanShows/define if reviews are allowed
average_ratingstringReviews average rating read-only
rating_countintegerAmount of reviews that the product have read-only
related_idsarrayList of related products IDs (integer) read-only
upsell_idsarrayList 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_idsarrayList of cross-sell products IDs. Cross-sells are products which you promote in the cart, based on the current product
parent_idintegerProduct parent ID (post_parent)
categoriesarrayList of product categories names (string). In write-mode need to pass a array of categories IDs (integer) (uses wp_set_object_terms())
tagsarrayList of product tags names (string). In write-mode need to pass a array of tags IDs (integer) (uses wp_set_object_terms())
imagesarrayList of products images. See Images Properties
featured_srcstringFeatured image URL read-only
attributesarrayList of product attributes. See Attributes Properties. Note: the attribute must be registered in WooCommerce before.
default_attributesarrayDefaults variation attributes. These are the attributes that will be pre-selected on the frontend. See Default Attributes Properties write-only
downloadsarrayList of downloadable files. See Downloads Properties
download_limitintegerAmount of times the product can be downloaded. In write-mode you can sent a blank string for unlimited re-downloads. e.g ''
download_expiryintegerNumber 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_typestringDownload type, this controls the schema. The available options are: '' (Standard Product), application (Application/Software) and music (Music)
purchase_notestringOptional note to send the customer after purchase.
total_salesintegerAmount of sales read-only
variationsarrayList of products variations. See Variations Properties
parentarrayList the product parent data when query for a variation read-only
product_urlstringProduct external URL. Only for external products write-only
button_textstringProduct external button text. Only for external products write-only

Dimensions Properties

AttributeTypeDescription
lengthstringProduct length in decimal format
widthstringProduct width in decimal format
heightstringProduct height in decimal format
unitstringProduct name read-only

Images Properties

AttributeTypeDescription
idintegerImage ID (attachment ID)
created_atstringUTC DateTime when the image was created read-only
updated_atstringUTC DateTime when the image was last updated read-only
srcstringImage URL. In write-mode you can use to send new images
titlestringImage title (attachment title) read-only
altstringImage alt text (attachment image alt text) read-only
positionintegerImage position. 0 means that the image is featured

Attributes Properties

AttributeTypeDescription
namestringAttribute name required
slugstringAttribute slug
positionintegerAttribute position
visiblebooleanShows/define if the attribute is visible on the “Additional Information” tab in the product’s page
variationbooleanShows/define if the attribute can be used as variation
optionsarrayList of available term names of the attribute

Default Attributes Properties

AttributeTypeDescription
namestringAttribute name
slugstringAttribute slug
optionstringSelected term name of the attribute

Downloads Properties

AttributeTypeDescription
idstringFile ID (file md5 hash) read-only
namestringFile name
filestringFile URL. In write-mode you can use this property to send new files

Variations Properties

AttributeTypeDescription
idintegerVariation ID (post ID) read-only
created_atstringUTC DateTime when the variation was created read-only
updated_atstringUTC DateTime when the variation was last updated read-only
downloadablebooleanIf the variation is downloadable or not. Downloadable variations give access to a file upon purchase
virtualbooleanIf the variation is virtual or not. Virtual variations are intangible and aren’t shipped
permalinkstringVariation URL (post permalink) read-only
skustringSKU refers to a Stock-keeping unit, a unique identifier for each distinct product and service that can be purchased
pricefloatCurrent variation price. This is setted from regular_price and sale_price read-only
regular_pricefloatVariation regular price
sale_pricefloatVariation sale price
sale_price_dates_fromfloatSets the sale start date. Date in the YYYY-MM-DD format write-only
sale_price_dates_tofloatSets the sale end date. Date in the YYYY-MM-DD format write-only
taxablebooleanShow if the variation is taxable or not read-only
tax_statusstringTax status. The options are: taxable, shipping (Shipping only) and none
tax_classstringTax class
managing_stockbooleanEnable stock management at variation level
stock_quantityintegerStock 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_stockbooleanControls whether or not the variation is listed as “in stock” or “out of stock” on the frontend.
backorderedbooleanShows if a variation is on backorder (if the variation have the stock_quantity negative) read-only
purchaseablebooleanShows if the variation can be bought read-only
visiblebooleanShows whether or not the product parent is visible in the catalog read-only
on_salebooleanShows if the variation is on sale or not read-only
weightstringVariation weight in decimal format
dimensionsarrayList of the variation dimensions. See Dimensions Properties
shipping_classstringShipping class slug. Shipping classes are used by certain shipping methods to group similar products
shipping_class_idintegerShipping class ID read-only
imagesarrayVariation featured image. See Images Properties
attributesarrayList of variation attributes. Similar to a simple or variable product, but for variation indicate the attributes used to form the variation. See Attributes Properties
downloadsarrayList of downloadable files. See Downloads Properties
download_limitintegerAmount of times the variation can be downloaded. In write-mode you can sent a blank string for unlimited re-downloads. e.g ''
download_expiryintegerNumber 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: [          {
   &