Zig
Zig
WooCommerce List All Products
See more WooCommerce Examples
Gets WooCommerce product information in JSON format.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = try chilkat.Http.init();
defer http.deinit();
// Implements the following CURL command:
// curl https://example.com/wp-json/wc/v3/products \
// -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
http.setBasicAuth(true);
http.setLogin("consumer_key");
http.setPassword("consumer_secret");
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
http.quickGetSb("https://example.com/wp-json/wc/v3/products", sb_response_body) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
const jarr_resp = try chilkat.JsonArray.init();
defer jarr_resp.deinit();
jarr_resp.loadSb(sb_response_body) catch {};
jarr_resp.setEmitCompact(false);
std.debug.print("Response Body:\n", .{});
std.debug.print("{s}\n", .{try jarr_resp.emit(alloc)});
const resp_status_code = http.getLastStatus();
std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
if (resp_status_code >= 400) {
std.debug.print("Response Header:\n", .{});
std.debug.print("{s}\n", .{try http.getLastHeader(alloc)});
std.debug.print("Failed.\n", .{});
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// [
// {
// "id": 799,
// "name": "Ship Your Idea",
// "slug": "ship-your-idea-22",
// "permalink": "https://example.com/product/ship-your-idea-22/",
// "date_created": "2017-03-23T17:03:12",
// "date_created_gmt": "2017-03-23T20:03:12",
// "date_modified": "2017-03-23T17:03:12",
// "date_modified_gmt": "2017-03-23T20:03:12",
// "type": "variable",
// "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": "",
// "regular_price": "",
// "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": "",
// "on_sale": false,
// "purchasable": false,
// "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": [
// 31,
// 22,
// 369,
// 414,
// 56
// ],
// "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": 795,
// "date_created": "2017-03-23T14:03:08",
// "date_created_gmt": "2017-03-23T20:03:08",
// "date_modified": "2017-03-23T14:03:08",
// "date_modified_gmt": "2017-03-23T20:03:08",
// "src": "https://example.com/wp-content/uploads/2017/03/T_4_front-11.jpg",
// "name": "",
// "alt": ""
// },
// {
// "id": 796,
// "date_created": "2017-03-23T14:03:09",
// "date_created_gmt": "2017-03-23T20:03:09",
// "date_modified": "2017-03-23T14:03:09",
// "date_modified_gmt": "2017-03-23T20:03:09",
// "src": "https://example.com/wp-content/uploads/2017/03/T_4_back-10.jpg",
// "name": "",
// "alt": ""
// },
// {
// "id": 797,
// "date_created": "2017-03-23T14:03:10",
// "date_created_gmt": "2017-03-23T20:03:10",
// "date_modified": "2017-03-23T14:03:10",
// "date_modified_gmt": "2017-03-23T20:03:10",
// "src": "https://example.com/wp-content/uploads/2017/03/T_3_front-10.jpg",
// "name": "",
// "alt": ""
// },
// {
// "id": 798,
// "date_created": "2017-03-23T14:03:11",
// "date_created_gmt": "2017-03-23T20:03:11",
// "date_modified": "2017-03-23T14:03:11",
// "date_modified_gmt": "2017-03-23T20:03:11",
// "src": "https://example.com/wp-content/uploads/2017/03/T_3_back-10.jpg",
// "name": "",
// "alt": ""
// }
// ],
// "attributes": [
// {
// "id": 6,
// "name": "Color",
// "position": 0,
// "visible": false,
// "variation": true,
// "options": [
// "Black",
// "Green"
// ]
// },
// {
// "id": 0,
// "name": "Size",
// "position": 0,
// "visible": true,
// "variation": true,
// "options": [
// "S",
// "M"
// ]
// }
// ],
// "default_attributes": [
// ],
// "variations": [
// ],
// "grouped_products": [
// ],
// "menu_order": 0,
// "meta_data": [
// ],
// "_links": {
// "self": [
// {
// "href": "https://example.com/wp-json/wc/v3/products/799"
// }
// ],
// "collection": [
// {
// "href": "https://example.com/wp-json/wc/v3/products"
// }
// ]
// }
// },
// {
// "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": [
// 463,
// 47,
// 31,
// 387,
// 458
// ],
// "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": [
// {
// "id": 6,
// "name": "Color",
// "option": "black"
// },
// {
// "id": 0,
// "name": "Size",
// "option": "S"
// }
// ],
// "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
const date_created = try chilkat.DtObj.init();
defer date_created.deinit();
const date_created_gmt = try chilkat.DtObj.init();
defer date_created_gmt.deinit();
const date_modified = try chilkat.DtObj.init();
defer date_modified.deinit();
const date_modified_gmt = try chilkat.DtObj.init();
defer date_modified_gmt.deinit();
const date_on_sale_from = try chilkat.DtObj.init();
defer date_on_sale_from.deinit();
const date_on_sale_from_gmt = try chilkat.DtObj.init();
defer date_on_sale_from_gmt.deinit();
const date_on_sale_to = try chilkat.DtObj.init();
defer date_on_sale_to.deinit();
const date_on_sale_to_gmt = try chilkat.DtObj.init();
defer date_on_sale_to_gmt.deinit();
var id: i32 = 0;
var name: [:0]const u8 = "";
var slug: [:0]const u8 = "";
var permalink: [:0]const u8 = "";
var v_type: [:0]const u8 = "";
var status: [:0]const u8 = "";
var featured: bool = false;
var catalog_visibility: [:0]const u8 = "";
var description: [:0]const u8 = "";
var short_description: [:0]const u8 = "";
var sku: [:0]const u8 = "";
var price: [:0]const u8 = "";
var regular_price: [:0]const u8 = "";
var sale_price: [:0]const u8 = "";
var price_html: [:0]const u8 = "";
var on_sale: bool = false;
var purchasable: bool = false;
var total_sales: i32 = 0;
var v_virtual: bool = false;
var downloadable: bool = false;
var download_limit: i32 = 0;
var download_expiry: i32 = 0;
var external_url: [:0]const u8 = "";
var button_text: [:0]const u8 = "";
var tax_status: [:0]const u8 = "";
var tax_class: [:0]const u8 = "";
var manage_stock: bool = false;
var stock_quantity: [:0]const u8 = "";
var stock_status: [:0]const u8 = "";
var backorders: [:0]const u8 = "";
var backorders_allowed: bool = false;
var backordered: bool = false;
var sold_individually: bool = false;
var weight: [:0]const u8 = "";
var dimensions_length: [:0]const u8 = "";
var dimensions_width: [:0]const u8 = "";
var dimensions_height: [:0]const u8 = "";
var shipping_required: bool = false;
var shipping_taxable: bool = false;
var shipping_class: [:0]const u8 = "";
var shipping_class_id: i32 = 0;
var reviews_allowed: bool = false;
var average_rating: [:0]const u8 = "";
var rating_count: i32 = 0;
var parent_id: i32 = 0;
var purchase_note: [:0]const u8 = "";
var menu_order: i32 = 0;
var j: i32 = 0;
var count_j: i32 = 0;
var int_val: i32 = 0;
var src: [:0]const u8 = "";
var alt: [:0]const u8 = "";
var position: i32 = 0;
var visible: bool = false;
var variation: bool = false;
var k: i32 = 0;
var count_k: i32 = 0;
var str_val: [:0]const u8 = "";
var v_option: [:0]const u8 = "";
var href: [:0]const u8 = "";
var i: i32 = 0;
const count_i = jarr_resp.getSize();
while (i < count_i) {
const json = try jarr_resp.objectAt(i);
defer json.deinit();
id = json.intOf("id");
name = try json.stringOf(alloc, "name");
slug = try json.stringOf(alloc, "slug");
permalink = try json.stringOf(alloc, "permalink");
json.dtOf("date_created", false, date_created) catch {};
json.dtOf("date_created_gmt", false, date_created_gmt) catch {};
json.dtOf("date_modified", false, date_modified) catch {};
json.dtOf("date_modified_gmt", false, date_modified_gmt) catch {};
v_type = try json.stringOf(alloc, "type");
status = try json.stringOf(alloc, "status");
featured = json.boolOf("featured");
catalog_visibility = try json.stringOf(alloc, "catalog_visibility");
description = try json.stringOf(alloc, "description");
short_description = try json.stringOf(alloc, "short_description");
sku = try json.stringOf(alloc, "sku");
price = try json.stringOf(alloc, "price");
regular_price = try json.stringOf(alloc, "regular_price");
sale_price = try json.stringOf(alloc, "sale_price");
json.dtOf("date_on_sale_from", false, date_on_sale_from) catch {};
json.dtOf("date_on_sale_from_gmt", false, date_on_sale_from_gmt) catch {};
json.dtOf("date_on_sale_to", false, date_on_sale_to) catch {};
json.dtOf("date_on_sale_to_gmt", false, date_on_sale_to_gmt) catch {};
price_html = try json.stringOf(alloc, "price_html");
on_sale = json.boolOf("on_sale");
purchasable = json.boolOf("purchasable");
total_sales = json.intOf("total_sales");
v_virtual = json.boolOf("virtual");
downloadable = json.boolOf("downloadable");
download_limit = json.intOf("download_limit");
download_expiry = json.intOf("download_expiry");
external_url = try json.stringOf(alloc, "external_url");
button_text = try json.stringOf(alloc, "button_text");
tax_status = try json.stringOf(alloc, "tax_status");
tax_class = try json.stringOf(alloc, "tax_class");
manage_stock = json.boolOf("manage_stock");
stock_quantity = try json.stringOf(alloc, "stock_quantity");
stock_status = try json.stringOf(alloc, "stock_status");
backorders = try json.stringOf(alloc, "backorders");
backorders_allowed = json.boolOf("backorders_allowed");
backordered = json.boolOf("backordered");
sold_individually = json.boolOf("sold_individually");
weight = try json.stringOf(alloc, "weight");
dimensions_length = try json.stringOf(alloc, "dimensions.length");
dimensions_width = try json.stringOf(alloc, "dimensions.width");
dimensions_height = try json.stringOf(alloc, "dimensions.height");
shipping_required = json.boolOf("shipping_required");
shipping_taxable = json.boolOf("shipping_taxable");
shipping_class = try json.stringOf(alloc, "shipping_class");
shipping_class_id = json.intOf("shipping_class_id");
reviews_allowed = json.boolOf("reviews_allowed");
average_rating = try json.stringOf(alloc, "average_rating");
rating_count = json.intOf("rating_count");
parent_id = json.intOf("parent_id");
purchase_note = try json.stringOf(alloc, "purchase_note");
menu_order = json.intOf("menu_order");
j = 0;
count_j = json.sizeOfArray("downloads");
while (j < count_j) {
json.setJ(j);
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("related_ids");
while (j < count_j) {
json.setJ(j);
int_val = json.intOf("related_ids[j]");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("upsell_ids");
while (j < count_j) {
json.setJ(j);
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("cross_sell_ids");
while (j < count_j) {
json.setJ(j);
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("categories");
while (j < count_j) {
json.setJ(j);
id = json.intOf("categories[j].id");
name = try json.stringOf(alloc, "categories[j].name");
slug = try json.stringOf(alloc, "categories[j].slug");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("tags");
while (j < count_j) {
json.setJ(j);
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("images");
while (j < count_j) {
json.setJ(j);
id = json.intOf("images[j].id");
json.dtOf("images[j].date_created", false, date_created) catch {};
json.dtOf("images[j].date_created_gmt", false, date_created_gmt) catch {};
json.dtOf("images[j].date_modified", false, date_modified) catch {};
json.dtOf("images[j].date_modified_gmt", false, date_modified_gmt) catch {};
src = try json.stringOf(alloc, "images[j].src");
name = try json.stringOf(alloc, "images[j].name");
alt = try json.stringOf(alloc, "images[j].alt");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("attributes");
while (j < count_j) {
json.setJ(j);
id = json.intOf("attributes[j].id");
name = try json.stringOf(alloc, "attributes[j].name");
position = json.intOf("attributes[j].position");
visible = json.boolOf("attributes[j].visible");
variation = json.boolOf("attributes[j].variation");
k = 0;
count_k = json.sizeOfArray("attributes[j].options");
while (k < count_k) {
json.setK(k);
str_val = try json.stringOf(alloc, "attributes[j].options[k]");
k = k + 1;
}
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("default_attributes");
while (j < count_j) {
json.setJ(j);
id = json.intOf("default_attributes[j].id");
name = try json.stringOf(alloc, "default_attributes[j].name");
v_option = try json.stringOf(alloc, "default_attributes[j].option");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("variations");
while (j < count_j) {
json.setJ(j);
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("grouped_products");
while (j < count_j) {
json.setJ(j);
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("meta_data");
while (j < count_j) {
json.setJ(j);
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("_links.self");
while (j < count_j) {
json.setJ(j);
href = try json.stringOf(alloc, "_links.self[j].href");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("_links.collection");
while (j < count_j) {
json.setJ(j);
href = try json.stringOf(alloc, "_links.collection[j].href");
j = j + 1;
}
i = i + 1;
}
}