B4X
B4X
WooCommerce Update a Product
See more WooCommerce Examples
Demonstrates how to make changes to a product in WooCommerce.Chilkat B4X Downloads
Dim success As Boolean = False
' This example assumes the Chilkat API to have been previously unlocked.
' See Global Unlock Sample for sample code.
Dim http As ChilkatHttp
http.Initialize("http")
' 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
http.BasicAuth = True
http.Login = "consumer_key"
http.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"
' }
Dim json As ChilkatJsonObject
json.Initialize
json.UpdateString("regular_price", "24.54")
http.SetRequestHeader("Content-Type", "application/json")
Dim sbRequestBody As ChilkatStringBuilder
sbRequestBody.Initialize
json.EmitSb(sbRequestBody)
Dim resp As ChilkatHttpResponse
resp.Initialize
success = http.HttpSb("PUT", "https://example.com/wp-json/wc/v3/products/794", sbRequestBody, "utf-8", "application/json", resp)
If success = False Then
Log(http.LastErrorText)
Return
End If
Dim sbResponseBody As ChilkatStringBuilder
sbResponseBody.Initialize
resp.GetBodySb(sbResponseBody)
Dim jResp As ChilkatJsonObject
jResp.Initialize
jResp.LoadSb(sbResponseBody)
jResp.EmitCompact = False
Log("Response Body:")
Log(jResp.Emit)
Dim respStatusCode As Int = resp.StatusCode
Log("Response Status Code = " & respStatusCode)
If respStatusCode >= 400 Then
Log("Response Header:")
Log(resp.Header)
Log("Failed.")
Return
End If
' 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\">$</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
Dim date_created As ChilkatDtObj
date_created.Initialize
Dim date_created_gmt As ChilkatDtObj
date_created_gmt.Initialize
Dim date_modified As ChilkatDtObj
date_modified.Initialize
Dim date_modified_gmt As ChilkatDtObj
date_modified_gmt.Initialize
Dim date_on_sale_from As ChilkatDtObj
date_on_sale_from.Initialize
Dim date_on_sale_from_gmt As ChilkatDtObj
date_on_sale_from_gmt.Initialize
Dim date_on_sale_to As ChilkatDtObj
date_on_sale_to.Initialize
Dim date_on_sale_to_gmt As ChilkatDtObj
date_on_sale_to_gmt.Initialize
Dim intVal As Int
Dim src As String
Dim alt As String
Dim href As String
Dim id As Int = jResp.IntOf("id")
Dim name As String = jResp.StringOf("name")
Dim slug As String = jResp.StringOf("slug")
Dim permalink As String = jResp.StringOf("permalink")
jResp.DtOf("date_created", False, date_created)
jResp.DtOf("date_created_gmt", False, date_created_gmt)
jResp.DtOf("date_modified", False, date_modified)
jResp.DtOf("date_modified_gmt", False, date_modified_gmt)
Dim v_type As String = jResp.StringOf("type")
Dim status As String = jResp.StringOf("status")
Dim featured As Boolean = jResp.BoolOf("featured")
Dim catalog_visibility As String = jResp.StringOf("catalog_visibility")
Dim description As String = jResp.StringOf("description")
Dim short_description As String = jResp.StringOf("short_description")
Dim sku As String = jResp.StringOf("sku")
Dim price As String = jResp.StringOf("price")
Dim regular_price As String = jResp.StringOf("regular_price")
Dim sale_price As String = jResp.StringOf("sale_price")
jResp.DtOf("date_on_sale_from", False, date_on_sale_from)
jResp.DtOf("date_on_sale_from_gmt", False, date_on_sale_from_gmt)
jResp.DtOf("date_on_sale_to", False, date_on_sale_to)
jResp.DtOf("date_on_sale_to_gmt", False, date_on_sale_to_gmt)
Dim price_html As String = jResp.StringOf("price_html")
Dim on_sale As Boolean = jResp.BoolOf("on_sale")
Dim purchasable As Boolean = jResp.BoolOf("purchasable")
Dim total_sales As Int = jResp.IntOf("total_sales")
Dim virtual As Boolean = jResp.BoolOf("virtual")
Dim downloadable As Boolean = jResp.BoolOf("downloadable")
Dim download_limit As Int = jResp.IntOf("download_limit")
Dim download_expiry As Int = jResp.IntOf("download_expiry")
Dim external_url As String = jResp.StringOf("external_url")
Dim button_text As String = jResp.StringOf("button_text")
Dim tax_status As String = jResp.StringOf("tax_status")
Dim tax_class As String = jResp.StringOf("tax_class")
Dim manage_stock As Boolean = jResp.BoolOf("manage_stock")
Dim stock_quantity As String = jResp.StringOf("stock_quantity")
Dim stock_status As String = jResp.StringOf("stock_status")
Dim backorders As String = jResp.StringOf("backorders")
Dim backorders_allowed As Boolean = jResp.BoolOf("backorders_allowed")
Dim backordered As Boolean = jResp.BoolOf("backordered")
Dim sold_individually As Boolean = jResp.BoolOf("sold_individually")
Dim weight As String = jResp.StringOf("weight")
Dim dimensionsLength As String = jResp.StringOf("dimensions.length")
Dim dimensionsWidth As String = jResp.StringOf("dimensions.width")
Dim dimensionsHeight As String = jResp.StringOf("dimensions.height")
Dim shipping_required As Boolean = jResp.BoolOf("shipping_required")
Dim shipping_taxable As Boolean = jResp.BoolOf("shipping_taxable")
Dim shipping_class As String = jResp.StringOf("shipping_class")
Dim shipping_class_id As Int = jResp.IntOf("shipping_class_id")
Dim reviews_allowed As Boolean = jResp.BoolOf("reviews_allowed")
Dim average_rating As String = jResp.StringOf("average_rating")
Dim rating_count As Int = jResp.IntOf("rating_count")
Dim parent_id As Int = jResp.IntOf("parent_id")
Dim purchase_note As String = jResp.StringOf("purchase_note")
Dim menu_order As Int = jResp.IntOf("menu_order")
Dim i As Int = 0
Dim count_i As Int = jResp.SizeOfArray("downloads")
Do While i < count_i
jResp.I = i
i = i + 1
Loop
i = 0
count_i = jResp.SizeOfArray("related_ids")
Do While i < count_i
jResp.I = i
intVal = jResp.IntOf("related_ids[i]")
i = i + 1
Loop
i = 0
count_i = jResp.SizeOfArray("upsell_ids")
Do While i < count_i
jResp.I = i
i = i + 1
Loop
i = 0
count_i = jResp.SizeOfArray("cross_sell_ids")
Do While i < count_i
jResp.I = i
i = i + 1
Loop
i = 0
count_i = jResp.SizeOfArray("categories")
Do While i < count_i
jResp.I = i
id = jResp.IntOf("categories[i].id")
name = jResp.StringOf("categories[i].name")
slug = jResp.StringOf("categories[i].slug")
i = i + 1
Loop
i = 0
count_i = jResp.SizeOfArray("tags")
Do While i < count_i
jResp.I = i
i = i + 1
Loop
i = 0
count_i = jResp.SizeOfArray("images")
Do While i < count_i
jResp.I = i
id = jResp.IntOf("images[i].id")
jResp.DtOf("images[i].date_created", False, date_created)
jResp.DtOf("images[i].date_created_gmt", False, date_created_gmt)
jResp.DtOf("images[i].date_modified", False, date_modified)
jResp.DtOf("images[i].date_modified_gmt", False, date_modified_gmt)
src = jResp.StringOf("images[i].src")
name = jResp.StringOf("images[i].name")
alt = jResp.StringOf("images[i].alt")
i = i + 1
Loop
i = 0
count_i = jResp.SizeOfArray("attributes")
Do While i < count_i
jResp.I = i
i = i + 1
Loop
i = 0
count_i = jResp.SizeOfArray("default_attributes")
Do While i < count_i
jResp.I = i
i = i + 1
Loop
i = 0
count_i = jResp.SizeOfArray("variations")
Do While i < count_i
jResp.I = i
i = i + 1
Loop
i = 0
count_i = jResp.SizeOfArray("grouped_products")
Do While i < count_i
jResp.I = i
i = i + 1
Loop
i = 0
count_i = jResp.SizeOfArray("meta_data")
Do While i < count_i
jResp.I = i
i = i + 1
Loop
i = 0
count_i = jResp.SizeOfArray("_links.self")
Do While i < count_i
jResp.I = i
href = jResp.StringOf("_links.self[i].href")
i = i + 1
Loop
i = 0
count_i = jResp.SizeOfArray("_links.collection")
Do While i < count_i
jResp.I = i
href = jResp.StringOf("_links.collection[i].href")
i = i + 1
Loop