Sample code for 30+ languages & platforms
Lianja

WooCommerce Update a Product

See more WooCommerce Examples

Demonstrates how to make changes to a product in WooCommerce.

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

loHttp = createobject("CkHttp")

// Implements the following CURL command:

// curl -X PUT https://example.com/wp-json/wc/v3/products/794 \
//     -u consumer_key:consumer_secret \
//     -H "Content-Type: application/json" \
//     -d '{
//   "regular_price": "24.54"
// }'

// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code

loHttp.BasicAuth = .T.
loHttp.Login = "consumer_key"
loHttp.Password = "consumer_secret"

// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON

// The following JSON is sent in the request body.

// {
//   "regular_price": "24.54"
// }

loJson = createobject("CkJsonObject")
loJson.UpdateString("regular_price","24.54")

loHttp.SetRequestHeader("Content-Type","application/json")

loSbRequestBody = createobject("CkStringBuilder")
loJson.EmitSb(loSbRequestBody)

loResp = createobject("CkHttpResponse")
llSuccess = loHttp.HttpSb("PUT","https://example.com/wp-json/wc/v3/products/794",loSbRequestBody,"utf-8","application/json",loResp)
if (llSuccess = .F.) then
    ? loHttp.LastErrorText
    release loHttp
    release loJson
    release loSbRequestBody
    release loResp
    return
endif

loSbResponseBody = createobject("CkStringBuilder")
loResp.GetBodySb(loSbResponseBody)
loJResp = createobject("CkJsonObject")
loJResp.LoadSb(loSbResponseBody)
loJResp.EmitCompact = .F.

? "Response Body:"
? loJResp.Emit()

lnRespStatusCode = loResp.StatusCode
? "Response Status Code = " + str(lnRespStatusCode)
if (lnRespStatusCode >= 400) then
    ? "Response Header:"
    ? loResp.Header
    ? "Failed."
    release loHttp
    release loJson
    release loSbRequestBody
    release loResp
    release loSbResponseBody
    release loJResp
    return
endif

// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)

// {
//   "id": 794,
//   "name": "Premium Quality",
//   "slug": "premium-quality-19",
//   "permalink": "https://example.com/product/premium-quality-19/",
//   "date_created": "2017-03-23T17:01:14",
//   "date_created_gmt": "2017-03-23T20:01:14",
//   "date_modified": "2017-03-23T17:01:14",
//   "date_modified_gmt": "2017-03-23T20:01:14",
//   "type": "simple",
//   "status": "publish",
//   "featured": false,
//   "catalog_visibility": "visible",
//   "description": "<p>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.</p>\n",
//   "short_description": "<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>\n",
//   "sku": "",
//   "price": "24.54",
//   "regular_price": "24.54",
//   "sale_price": "",
//   "date_on_sale_from": null,
//   "date_on_sale_from_gmt": null,
//   "date_on_sale_to": null,
//   "date_on_sale_to_gmt": null,
//   "price_html": "<span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">&#36;</span>24.54</span>",
//   "on_sale": false,
//   "purchasable": true,
//   "total_sales": 0,
//   "virtual": false,
//   "downloadable": false,
//   "downloads": [
//   ],
//   "download_limit": -1,
//   "download_expiry": -1,
//   "external_url": "",
//   "button_text": "",
//   "tax_status": "taxable",
//   "tax_class": "",
//   "manage_stock": false,
//   "stock_quantity": null,
//   "stock_status": "instock",
//   "backorders": "no",
//   "backorders_allowed": false,
//   "backordered": false,
//   "sold_individually": false,
//   "weight": "",
//   "dimensions": {
//     "length": "",
//     "width": "",
//     "height": ""
//   },
//   "shipping_required": true,
//   "shipping_taxable": true,
//   "shipping_class": "",
//   "shipping_class_id": 0,
//   "reviews_allowed": true,
//   "average_rating": "0.00",
//   "rating_count": 0,
//   "related_ids": [
//     479,
//     387,
//     22,
//     463,
//     396
//   ],
//   "upsell_ids": [
//   ],
//   "cross_sell_ids": [
//   ],
//   "parent_id": 0,
//   "purchase_note": "",
//   "categories": [
//     {
//       "id": 9,
//       "name": "Clothing",
//       "slug": "clothing"
//     },
//     {
//       "id": 14,
//       "name": "T-shirts",
//       "slug": "t-shirts"
//     }
//   ],
//   "tags": [
//   ],
//   "images": [
//     {
//       "id": 792,
//       "date_created": "2017-03-23T14:01:13",
//       "date_created_gmt": "2017-03-23T20:01:13",
//       "date_modified": "2017-03-23T14:01:13",
//       "date_modified_gmt": "2017-03-23T20:01:13",
//       "src": "https://example.com/wp-content/uploads/2017/03/T_2_front-4.jpg",
//       "name": "",
//       "alt": ""
//     },
//     {
//       "id": 793,
//       "date_created": "2017-03-23T14:01:14",
//       "date_created_gmt": "2017-03-23T20:01:14",
//       "date_modified": "2017-03-23T14:01:14",
//       "date_modified_gmt": "2017-03-23T20:01:14",
//       "src": "https://example.com/wp-content/uploads/2017/03/T_2_back-2.jpg",
//       "name": "",
//       "alt": ""
//     }
//   ],
//   "attributes": [
//   ],
//   "default_attributes": [
//   ],
//   "variations": [
//   ],
//   "grouped_products": [
//   ],
//   "menu_order": 0,
//   "meta_data": [
//   ],
//   "_links": {
//     "self": [
//       {
//         "href": "https://example.com/wp-json/wc/v3/products/794"
//       }
//     ],
//     "collection": [
//       {
//         "href": "https://example.com/wp-json/wc/v3/products"
//       }
//     ]
//   }
// }

// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON

loDate_created = createobject("CkDtObj")
loDate_created_gmt = createobject("CkDtObj")
loDate_modified = createobject("CkDtObj")
loDate_modified_gmt = createobject("CkDtObj")
loDate_on_sale_from = createobject("CkDtObj")
loDate_on_sale_from_gmt = createobject("CkDtObj")
loDate_on_sale_to = createobject("CkDtObj")
loDate_on_sale_to_gmt = createobject("CkDtObj")

lnId = loJResp.IntOf("id")
lcName = loJResp.StringOf("name")
lcSlug = loJResp.StringOf("slug")
lcPermalink = loJResp.StringOf("permalink")
loJResp.DtOf("date_created",.F.,loDate_created)
loJResp.DtOf("date_created_gmt",.F.,loDate_created_gmt)
loJResp.DtOf("date_modified",.F.,loDate_modified)
loJResp.DtOf("date_modified_gmt",.F.,loDate_modified_gmt)
lcV_type = loJResp.StringOf("type")
lcStatus = loJResp.StringOf("status")
llFeatured = loJResp.BoolOf("featured")
lcCatalog_visibility = loJResp.StringOf("catalog_visibility")
lcDescription = loJResp.StringOf("description")
lcShort_description = loJResp.StringOf("short_description")
lcSku = loJResp.StringOf("sku")
lcPrice = loJResp.StringOf("price")
lcRegular_price = loJResp.StringOf("regular_price")
lcSale_price = loJResp.StringOf("sale_price")
loJResp.DtOf("date_on_sale_from",.F.,loDate_on_sale_from)
loJResp.DtOf("date_on_sale_from_gmt",.F.,loDate_on_sale_from_gmt)
loJResp.DtOf("date_on_sale_to",.F.,loDate_on_sale_to)
loJResp.DtOf("date_on_sale_to_gmt",.F.,loDate_on_sale_to_gmt)
lcPrice_html = loJResp.StringOf("price_html")
llOn_sale = loJResp.BoolOf("on_sale")
llPurchasable = loJResp.BoolOf("purchasable")
lnTotal_sales = loJResp.IntOf("total_sales")
llVirtual = loJResp.BoolOf("virtual")
llDownloadable = loJResp.BoolOf("downloadable")
lnDownload_limit = loJResp.IntOf("download_limit")
lnDownload_expiry = loJResp.IntOf("download_expiry")
lcExternal_url = loJResp.StringOf("external_url")
lcButton_text = loJResp.StringOf("button_text")
lcTax_status = loJResp.StringOf("tax_status")
lcTax_class = loJResp.StringOf("tax_class")
llManage_stock = loJResp.BoolOf("manage_stock")
lcStock_quantity = loJResp.StringOf("stock_quantity")
lcStock_status = loJResp.StringOf("stock_status")
lcBackorders = loJResp.StringOf("backorders")
llBackorders_allowed = loJResp.BoolOf("backorders_allowed")
llBackordered = loJResp.BoolOf("backordered")
llSold_individually = loJResp.BoolOf("sold_individually")
lcWeight = loJResp.StringOf("weight")
lcDimensionsLength = loJResp.StringOf("dimensions.length")
lcDimensionsWidth = loJResp.StringOf("dimensions.width")
lcDimensionsHeight = loJResp.StringOf("dimensions.height")
llShipping_required = loJResp.BoolOf("shipping_required")
llShipping_taxable = loJResp.BoolOf("shipping_taxable")
lcShipping_class = loJResp.StringOf("shipping_class")
lnShipping_class_id = loJResp.IntOf("shipping_class_id")
llReviews_allowed = loJResp.BoolOf("reviews_allowed")
lcAverage_rating = loJResp.StringOf("average_rating")
lnRating_count = loJResp.IntOf("rating_count")
lnParent_id = loJResp.IntOf("parent_id")
lcPurchase_note = loJResp.StringOf("purchase_note")
lnMenu_order = loJResp.IntOf("menu_order")
i = 0
lnCount_i = loJResp.SizeOfArray("downloads")
do while i < lnCount_i
    loJResp.I = i
    i = i + 1
enddo
i = 0
lnCount_i = loJResp.SizeOfArray("related_ids")
do while i < lnCount_i
    loJResp.I = i
    lnIntVal = loJResp.IntOf("related_ids[i]")
    i = i + 1
enddo
i = 0
lnCount_i = loJResp.SizeOfArray("upsell_ids")
do while i < lnCount_i
    loJResp.I = i
    i = i + 1
enddo
i = 0
lnCount_i = loJResp.SizeOfArray("cross_sell_ids")
do while i < lnCount_i
    loJResp.I = i
    i = i + 1
enddo
i = 0
lnCount_i = loJResp.SizeOfArray("categories")
do while i < lnCount_i
    loJResp.I = i
    lnId = loJResp.IntOf("categories[i].id")
    lcName = loJResp.StringOf("categories[i].name")
    lcSlug = loJResp.StringOf("categories[i].slug")
    i = i + 1
enddo
i = 0
lnCount_i = loJResp.SizeOfArray("tags")
do while i < lnCount_i
    loJResp.I = i
    i = i + 1
enddo
i = 0
lnCount_i = loJResp.SizeOfArray("images")
do while i < lnCount_i
    loJResp.I = i
    lnId = loJResp.IntOf("images[i].id")
    loJResp.DtOf("images[i].date_created",.F.,loDate_created)
    loJResp.DtOf("images[i].date_created_gmt",.F.,loDate_created_gmt)
    loJResp.DtOf("images[i].date_modified",.F.,loDate_modified)
    loJResp.DtOf("images[i].date_modified_gmt",.F.,loDate_modified_gmt)
    lcSrc = loJResp.StringOf("images[i].src")
    lcName = loJResp.StringOf("images[i].name")
    lcAlt = loJResp.StringOf("images[i].alt")
    i = i + 1
enddo
i = 0
lnCount_i = loJResp.SizeOfArray("attributes")
do while i < lnCount_i
    loJResp.I = i
    i = i + 1
enddo
i = 0
lnCount_i = loJResp.SizeOfArray("default_attributes")
do while i < lnCount_i
    loJResp.I = i
    i = i + 1
enddo
i = 0
lnCount_i = loJResp.SizeOfArray("variations")
do while i < lnCount_i
    loJResp.I = i
    i = i + 1
enddo
i = 0
lnCount_i = loJResp.SizeOfArray("grouped_products")
do while i < lnCount_i
    loJResp.I = i
    i = i + 1
enddo
i = 0
lnCount_i = loJResp.SizeOfArray("meta_data")
do while i < lnCount_i
    loJResp.I = i
    i = i + 1
enddo
i = 0
lnCount_i = loJResp.SizeOfArray("_links.self")
do while i < lnCount_i
    loJResp.I = i
    lcHref = loJResp.StringOf("_links.self[i].href")
    i = i + 1
enddo
i = 0
lnCount_i = loJResp.SizeOfArray("_links.collection")
do while i < lnCount_i
    loJResp.I = i
    lcHref = loJResp.StringOf("_links.collection[i].href")
    i = i + 1
enddo


release loHttp
release loJson
release loSbRequestBody
release loResp
release loSbResponseBody
release loJResp
release loDate_created
release loDate_created_gmt
release loDate_modified
release loDate_modified_gmt
release loDate_on_sale_from
release loDate_on_sale_from_gmt
release loDate_on_sale_to
release loDate_on_sale_to_gmt