Delphi DLL
Delphi DLL
WooCommerce Retrieve a Product
See more WooCommerce Examples
Demonstrates how to get the information for a product in JSON format.Chilkat Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, StringBuilder, DtObj, JsonObject;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
http: HCkHttp;
sbResponseBody: HCkStringBuilder;
jResp: HCkJsonObject;
respStatusCode: Integer;
date_created: HCkDtObj;
date_created_gmt: HCkDtObj;
date_modified: HCkDtObj;
date_modified_gmt: HCkDtObj;
date_on_sale_from: HCkDtObj;
date_on_sale_from_gmt: HCkDtObj;
date_on_sale_to: HCkDtObj;
date_on_sale_to_gmt: HCkDtObj;
intVal: Integer;
src: PWideChar;
alt: PWideChar;
href: PWideChar;
id: Integer;
name: PWideChar;
slug: PWideChar;
permalink: PWideChar;
v_type: PWideChar;
status: PWideChar;
featured: Boolean;
catalog_visibility: PWideChar;
description: PWideChar;
short_description: PWideChar;
sku: PWideChar;
price: PWideChar;
regular_price: PWideChar;
sale_price: PWideChar;
price_html: PWideChar;
on_sale: Boolean;
purchasable: Boolean;
total_sales: Integer;
virtual: Boolean;
downloadable: Boolean;
download_limit: Integer;
download_expiry: Integer;
external_url: PWideChar;
button_text: PWideChar;
tax_status: PWideChar;
tax_class: PWideChar;
manage_stock: Boolean;
stock_quantity: PWideChar;
stock_status: PWideChar;
backorders: PWideChar;
backorders_allowed: Boolean;
backordered: Boolean;
sold_individually: Boolean;
weight: PWideChar;
dimensionsLength: PWideChar;
dimensionsWidth: PWideChar;
dimensionsHeight: PWideChar;
shipping_required: Boolean;
shipping_taxable: Boolean;
shipping_class: PWideChar;
shipping_class_id: Integer;
reviews_allowed: Boolean;
average_rating: PWideChar;
rating_count: Integer;
parent_id: Integer;
purchase_note: PWideChar;
menu_order: Integer;
i: Integer;
count_i: Integer;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := CkHttp_Create();
// Implements the following CURL command:
// curl https://example.com/wp-json/wc/v3/products/794 \
// -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_putBasicAuth(http,True);
CkHttp_putLogin(http,'consumer_key');
CkHttp_putPassword(http,'consumer_secret');
sbResponseBody := CkStringBuilder_Create();
success := CkHttp_QuickGetSb(http,'https://example.com/wp-json/wc/v3/products/794',sbResponseBody);
if (success = False) then
begin
Memo1.Lines.Add(CkHttp__lastErrorText(http));
Exit;
end;
jResp := CkJsonObject_Create();
CkJsonObject_LoadSb(jResp,sbResponseBody);
CkJsonObject_putEmitCompact(jResp,False);
Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(CkJsonObject__emit(jResp));
respStatusCode := CkHttp_getLastStatus(http);
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
if (respStatusCode >= 400) then
begin
Memo1.Lines.Add('Response Header:');
Memo1.Lines.Add(CkHttp__lastHeader(http));
Memo1.Lines.Add('Failed.');
Exit;
end;
// 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": "21.99",
// "regular_price": "21.99",
// "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>21.99</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": [
// 53,
// 40,
// 56,
// 479,
// 99
// ],
// "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 := CkDtObj_Create();
date_created_gmt := CkDtObj_Create();
date_modified := CkDtObj_Create();
date_modified_gmt := CkDtObj_Create();
date_on_sale_from := CkDtObj_Create();
date_on_sale_from_gmt := CkDtObj_Create();
date_on_sale_to := CkDtObj_Create();
date_on_sale_to_gmt := CkDtObj_Create();
id := CkJsonObject_IntOf(jResp,'id');
name := CkJsonObject__stringOf(jResp,'name');
slug := CkJsonObject__stringOf(jResp,'slug');
permalink := CkJsonObject__stringOf(jResp,'permalink');
CkJsonObject_DtOf(jResp,'date_created',False,date_created);
CkJsonObject_DtOf(jResp,'date_created_gmt',False,date_created_gmt);
CkJsonObject_DtOf(jResp,'date_modified',False,date_modified);
CkJsonObject_DtOf(jResp,'date_modified_gmt',False,date_modified_gmt);
v_type := CkJsonObject__stringOf(jResp,'type');
status := CkJsonObject__stringOf(jResp,'status');
featured := CkJsonObject_BoolOf(jResp,'featured');
catalog_visibility := CkJsonObject__stringOf(jResp,'catalog_visibility');
description := CkJsonObject__stringOf(jResp,'description');
short_description := CkJsonObject__stringOf(jResp,'short_description');
sku := CkJsonObject__stringOf(jResp,'sku');
price := CkJsonObject__stringOf(jResp,'price');
regular_price := CkJsonObject__stringOf(jResp,'regular_price');
sale_price := CkJsonObject__stringOf(jResp,'sale_price');
CkJsonObject_DtOf(jResp,'date_on_sale_from',False,date_on_sale_from);
CkJsonObject_DtOf(jResp,'date_on_sale_from_gmt',False,date_on_sale_from_gmt);
CkJsonObject_DtOf(jResp,'date_on_sale_to',False,date_on_sale_to);
CkJsonObject_DtOf(jResp,'date_on_sale_to_gmt',False,date_on_sale_to_gmt);
price_html := CkJsonObject__stringOf(jResp,'price_html');
on_sale := CkJsonObject_BoolOf(jResp,'on_sale');
purchasable := CkJsonObject_BoolOf(jResp,'purchasable');
total_sales := CkJsonObject_IntOf(jResp,'total_sales');
virtual := CkJsonObject_BoolOf(jResp,'virtual');
downloadable := CkJsonObject_BoolOf(jResp,'downloadable');
download_limit := CkJsonObject_IntOf(jResp,'download_limit');
download_expiry := CkJsonObject_IntOf(jResp,'download_expiry');
external_url := CkJsonObject__stringOf(jResp,'external_url');
button_text := CkJsonObject__stringOf(jResp,'button_text');
tax_status := CkJsonObject__stringOf(jResp,'tax_status');
tax_class := CkJsonObject__stringOf(jResp,'tax_class');
manage_stock := CkJsonObject_BoolOf(jResp,'manage_stock');
stock_quantity := CkJsonObject__stringOf(jResp,'stock_quantity');
stock_status := CkJsonObject__stringOf(jResp,'stock_status');
backorders := CkJsonObject__stringOf(jResp,'backorders');
backorders_allowed := CkJsonObject_BoolOf(jResp,'backorders_allowed');
backordered := CkJsonObject_BoolOf(jResp,'backordered');
sold_individually := CkJsonObject_BoolOf(jResp,'sold_individually');
weight := CkJsonObject__stringOf(jResp,'weight');
dimensionsLength := CkJsonObject__stringOf(jResp,'dimensions.length');
dimensionsWidth := CkJsonObject__stringOf(jResp,'dimensions.width');
dimensionsHeight := CkJsonObject__stringOf(jResp,'dimensions.height');
shipping_required := CkJsonObject_BoolOf(jResp,'shipping_required');
shipping_taxable := CkJsonObject_BoolOf(jResp,'shipping_taxable');
shipping_class := CkJsonObject__stringOf(jResp,'shipping_class');
shipping_class_id := CkJsonObject_IntOf(jResp,'shipping_class_id');
reviews_allowed := CkJsonObject_BoolOf(jResp,'reviews_allowed');
average_rating := CkJsonObject__stringOf(jResp,'average_rating');
rating_count := CkJsonObject_IntOf(jResp,'rating_count');
parent_id := CkJsonObject_IntOf(jResp,'parent_id');
purchase_note := CkJsonObject__stringOf(jResp,'purchase_note');
menu_order := CkJsonObject_IntOf(jResp,'menu_order');
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'downloads');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
i := i + 1;
end;
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'related_ids');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
intVal := CkJsonObject_IntOf(jResp,'related_ids[i]');
i := i + 1;
end;
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'upsell_ids');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
i := i + 1;
end;
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'cross_sell_ids');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
i := i + 1;
end;
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'categories');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
id := CkJsonObject_IntOf(jResp,'categories[i].id');
name := CkJsonObject__stringOf(jResp,'categories[i].name');
slug := CkJsonObject__stringOf(jResp,'categories[i].slug');
i := i + 1;
end;
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'tags');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
i := i + 1;
end;
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'images');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
id := CkJsonObject_IntOf(jResp,'images[i].id');
CkJsonObject_DtOf(jResp,'images[i].date_created',False,date_created);
CkJsonObject_DtOf(jResp,'images[i].date_created_gmt',False,date_created_gmt);
CkJsonObject_DtOf(jResp,'images[i].date_modified',False,date_modified);
CkJsonObject_DtOf(jResp,'images[i].date_modified_gmt',False,date_modified_gmt);
src := CkJsonObject__stringOf(jResp,'images[i].src');
name := CkJsonObject__stringOf(jResp,'images[i].name');
alt := CkJsonObject__stringOf(jResp,'images[i].alt');
i := i + 1;
end;
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'attributes');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
i := i + 1;
end;
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'default_attributes');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
i := i + 1;
end;
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'variations');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
i := i + 1;
end;
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'grouped_products');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
i := i + 1;
end;
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'meta_data');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
i := i + 1;
end;
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'_links.self');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
href := CkJsonObject__stringOf(jResp,'_links.self[i].href');
i := i + 1;
end;
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'_links.collection');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
href := CkJsonObject__stringOf(jResp,'_links.collection[i].href');
i := i + 1;
end;
CkHttp_Dispose(http);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);
CkDtObj_Dispose(date_created);
CkDtObj_Dispose(date_created_gmt);
CkDtObj_Dispose(date_modified);
CkDtObj_Dispose(date_modified_gmt);
CkDtObj_Dispose(date_on_sale_from);
CkDtObj_Dispose(date_on_sale_from_gmt);
CkDtObj_Dispose(date_on_sale_to);
CkDtObj_Dispose(date_on_sale_to_gmt);
end;