PureBasic
PureBasic
WooCommerce Delete a Product
See more WooCommerce Examples
Demonstrates how to delete a product from WooCommerce.Chilkat PureBasic Downloads
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkDtObj.pb"
Procedure ChilkatExample()
success.i = 0
; This example assumes the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
http.i = CkHttp::ckCreate()
If http.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Implements the following CURL command:
; curl -X DELETE https://example.com/wp-json/wc/v3/products/794?force=true \
; -u consumer_key:consumer_secret
; Use the following online tool to generate HTTP code from a CURL command
; Convert a cURL Command to HTTP Source Code
CkHttp::setCkBasicAuth(http, 1)
CkHttp::setCkLogin(http, "consumer_key")
CkHttp::setCkPassword(http, "consumer_secret")
resp.i = CkHttpResponse::ckCreate()
If resp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkHttp::ckHttpNoBody(http,"DELETE","https://example.com/wp-json/wc/v3/products/794?force=true",resp)
If success = 0
Debug CkHttp::ckLastErrorText(http)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
ProcedureReturn
EndIf
sbResponseBody.i = CkStringBuilder::ckCreate()
If sbResponseBody.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttpResponse::ckGetBodySb(resp,sbResponseBody)
jResp.i = CkJsonObject::ckCreate()
If jResp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckLoadSb(jResp,sbResponseBody)
CkJsonObject::setCkEmitCompact(jResp, 0)
Debug "Response Body:"
Debug CkJsonObject::ckEmit(jResp)
respStatusCode.i = CkHttpResponse::ckStatusCode(resp)
Debug "Response Status Code = " + Str(respStatusCode)
If respStatusCode >= 400
Debug "Response Header:"
Debug CkHttpResponse::ckHeader(resp)
Debug "Failed."
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkStringBuilder::ckDispose(sbResponseBody)
CkJsonObject::ckDispose(jResp)
ProcedureReturn
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\">$</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
date_created.i = CkDtObj::ckCreate()
If date_created.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
date_created_gmt.i = CkDtObj::ckCreate()
If date_created_gmt.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
date_modified.i = CkDtObj::ckCreate()
If date_modified.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
date_modified_gmt.i = CkDtObj::ckCreate()
If date_modified_gmt.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
date_on_sale_from.i = CkDtObj::ckCreate()
If date_on_sale_from.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
date_on_sale_from_gmt.i = CkDtObj::ckCreate()
If date_on_sale_from_gmt.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
date_on_sale_to.i = CkDtObj::ckCreate()
If date_on_sale_to.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
date_on_sale_to_gmt.i = CkDtObj::ckCreate()
If date_on_sale_to_gmt.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
intVal.i
src.s
alt.s
href.s
id.i = CkJsonObject::ckIntOf(jResp,"id")
name.s = CkJsonObject::ckStringOf(jResp,"name")
slug.s = CkJsonObject::ckStringOf(jResp,"slug")
permalink.s = CkJsonObject::ckStringOf(jResp,"permalink")
CkJsonObject::ckDtOf(jResp,"date_created",0,date_created)
CkJsonObject::ckDtOf(jResp,"date_created_gmt",0,date_created_gmt)
CkJsonObject::ckDtOf(jResp,"date_modified",0,date_modified)
CkJsonObject::ckDtOf(jResp,"date_modified_gmt",0,date_modified_gmt)
v_type.s = CkJsonObject::ckStringOf(jResp,"type")
status.s = CkJsonObject::ckStringOf(jResp,"status")
featured.i = CkJsonObject::ckBoolOf(jResp,"featured")
catalog_visibility.s = CkJsonObject::ckStringOf(jResp,"catalog_visibility")
description.s = CkJsonObject::ckStringOf(jResp,"description")
short_description.s = CkJsonObject::ckStringOf(jResp,"short_description")
sku.s = CkJsonObject::ckStringOf(jResp,"sku")
price.s = CkJsonObject::ckStringOf(jResp,"price")
regular_price.s = CkJsonObject::ckStringOf(jResp,"regular_price")
sale_price.s = CkJsonObject::ckStringOf(jResp,"sale_price")
CkJsonObject::ckDtOf(jResp,"date_on_sale_from",0,date_on_sale_from)
CkJsonObject::ckDtOf(jResp,"date_on_sale_from_gmt",0,date_on_sale_from_gmt)
CkJsonObject::ckDtOf(jResp,"date_on_sale_to",0,date_on_sale_to)
CkJsonObject::ckDtOf(jResp,"date_on_sale_to_gmt",0,date_on_sale_to_gmt)
price_html.s = CkJsonObject::ckStringOf(jResp,"price_html")
on_sale.i = CkJsonObject::ckBoolOf(jResp,"on_sale")
purchasable.i = CkJsonObject::ckBoolOf(jResp,"purchasable")
total_sales.i = CkJsonObject::ckIntOf(jResp,"total_sales")
virtual.i = CkJsonObject::ckBoolOf(jResp,"virtual")
downloadable.i = CkJsonObject::ckBoolOf(jResp,"downloadable")
download_limit.i = CkJsonObject::ckIntOf(jResp,"download_limit")
download_expiry.i = CkJsonObject::ckIntOf(jResp,"download_expiry")
external_url.s = CkJsonObject::ckStringOf(jResp,"external_url")
button_text.s = CkJsonObject::ckStringOf(jResp,"button_text")
tax_status.s = CkJsonObject::ckStringOf(jResp,"tax_status")
tax_class.s = CkJsonObject::ckStringOf(jResp,"tax_class")
manage_stock.i = CkJsonObject::ckBoolOf(jResp,"manage_stock")
stock_quantity.s = CkJsonObject::ckStringOf(jResp,"stock_quantity")
stock_status.s = CkJsonObject::ckStringOf(jResp,"stock_status")
backorders.s = CkJsonObject::ckStringOf(jResp,"backorders")
backorders_allowed.i = CkJsonObject::ckBoolOf(jResp,"backorders_allowed")
backordered.i = CkJsonObject::ckBoolOf(jResp,"backordered")
sold_individually.i = CkJsonObject::ckBoolOf(jResp,"sold_individually")
weight.s = CkJsonObject::ckStringOf(jResp,"weight")
dimensionsLength.s = CkJsonObject::ckStringOf(jResp,"dimensions.length")
dimensionsWidth.s = CkJsonObject::ckStringOf(jResp,"dimensions.width")
dimensionsHeight.s = CkJsonObject::ckStringOf(jResp,"dimensions.height")
shipping_required.i = CkJsonObject::ckBoolOf(jResp,"shipping_required")
shipping_taxable.i = CkJsonObject::ckBoolOf(jResp,"shipping_taxable")
shipping_class.s = CkJsonObject::ckStringOf(jResp,"shipping_class")
shipping_class_id.i = CkJsonObject::ckIntOf(jResp,"shipping_class_id")
reviews_allowed.i = CkJsonObject::ckBoolOf(jResp,"reviews_allowed")
average_rating.s = CkJsonObject::ckStringOf(jResp,"average_rating")
rating_count.i = CkJsonObject::ckIntOf(jResp,"rating_count")
parent_id.i = CkJsonObject::ckIntOf(jResp,"parent_id")
purchase_note.s = CkJsonObject::ckStringOf(jResp,"purchase_note")
menu_order.i = CkJsonObject::ckIntOf(jResp,"menu_order")
i.i = 0
count_i.i = CkJsonObject::ckSizeOfArray(jResp,"downloads")
While i < count_i
CkJsonObject::setCkI(jResp, i)
i = i + 1
Wend
i = 0
count_i = CkJsonObject::ckSizeOfArray(jResp,"related_ids")
While i < count_i
CkJsonObject::setCkI(jResp, i)
intVal = CkJsonObject::ckIntOf(jResp,"related_ids[i]")
i = i + 1
Wend
i = 0
count_i = CkJsonObject::ckSizeOfArray(jResp,"upsell_ids")
While i < count_i
CkJsonObject::setCkI(jResp, i)
i = i + 1
Wend
i = 0
count_i = CkJsonObject::ckSizeOfArray(jResp,"cross_sell_ids")
While i < count_i
CkJsonObject::setCkI(jResp, i)
i = i + 1
Wend
i = 0
count_i = CkJsonObject::ckSizeOfArray(jResp,"categories")
While i < count_i
CkJsonObject::setCkI(jResp, i)
id = CkJsonObject::ckIntOf(jResp,"categories[i].id")
name = CkJsonObject::ckStringOf(jResp,"categories[i].name")
slug = CkJsonObject::ckStringOf(jResp,"categories[i].slug")
i = i + 1
Wend
i = 0
count_i = CkJsonObject::ckSizeOfArray(jResp,"tags")
While i < count_i
CkJsonObject::setCkI(jResp, i)
i = i + 1
Wend
i = 0
count_i = CkJsonObject::ckSizeOfArray(jResp,"images")
While i < count_i
CkJsonObject::setCkI(jResp, i)
id = CkJsonObject::ckIntOf(jResp,"images[i].id")
CkJsonObject::ckDtOf(jResp,"images[i].date_created",0,date_created)
CkJsonObject::ckDtOf(jResp,"images[i].date_created_gmt",0,date_created_gmt)
CkJsonObject::ckDtOf(jResp,"images[i].date_modified",0,date_modified)
CkJsonObject::ckDtOf(jResp,"images[i].date_modified_gmt",0,date_modified_gmt)
src = CkJsonObject::ckStringOf(jResp,"images[i].src")
name = CkJsonObject::ckStringOf(jResp,"images[i].name")
alt = CkJsonObject::ckStringOf(jResp,"images[i].alt")
i = i + 1
Wend
i = 0
count_i = CkJsonObject::ckSizeOfArray(jResp,"attributes")
While i < count_i
CkJsonObject::setCkI(jResp, i)
i = i + 1
Wend
i = 0
count_i = CkJsonObject::ckSizeOfArray(jResp,"default_attributes")
While i < count_i
CkJsonObject::setCkI(jResp, i)
i = i + 1
Wend
i = 0
count_i = CkJsonObject::ckSizeOfArray(jResp,"variations")
While i < count_i
CkJsonObject::setCkI(jResp, i)
i = i + 1
Wend
i = 0
count_i = CkJsonObject::ckSizeOfArray(jResp,"grouped_products")
While i < count_i
CkJsonObject::setCkI(jResp, i)
i = i + 1
Wend
i = 0
count_i = CkJsonObject::ckSizeOfArray(jResp,"meta_data")
While i < count_i
CkJsonObject::setCkI(jResp, i)
i = i + 1
Wend
i = 0
count_i = CkJsonObject::ckSizeOfArray(jResp,"_links.self")
While i < count_i
CkJsonObject::setCkI(jResp, i)
href = CkJsonObject::ckStringOf(jResp,"_links.self[i].href")
i = i + 1
Wend
i = 0
count_i = CkJsonObject::ckSizeOfArray(jResp,"_links.collection")
While i < count_i
CkJsonObject::setCkI(jResp, i)
href = CkJsonObject::ckStringOf(jResp,"_links.collection[i].href")
i = i + 1
Wend
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkStringBuilder::ckDispose(sbResponseBody)
CkJsonObject::ckDispose(jResp)
CkDtObj::ckDispose(date_created)
CkDtObj::ckDispose(date_created_gmt)
CkDtObj::ckDispose(date_modified)
CkDtObj::ckDispose(date_modified_gmt)
CkDtObj::ckDispose(date_on_sale_from)
CkDtObj::ckDispose(date_on_sale_from_gmt)
CkDtObj::ckDispose(date_on_sale_to)
CkDtObj::ckDispose(date_on_sale_to_gmt)
ProcedureReturn
EndProcedure