Zig
Zig
WooCommerce Create a Product
See more WooCommerce Examples
Demonstrates how to create a new product in WooCommerce.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 -X POST https://example.com/wp-json/wc/v3/products \
// -u consumer_key:consumer_secret \
// -H "Content-Type: application/json" \
// -d '{
// "name": "Premium Quality",
// "type": "simple",
// "regular_price": "21.99",
// "description": "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.",
// "short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
// "categories": [
// {
// "id": 9
// },
// {
// "id": 14
// }
// ],
// "images": [
// {
// "src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg"
// },
// {
// "src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg"
// }
// ]
// }'
// 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");
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "name": "Premium Quality",
// "type": "simple",
// "regular_price": "21.99",
// "description": "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.",
// "short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
// "categories": [
// {
// "id": 9
// },
// {
// "id": 14
// }
// ],
// "images": [
// {
// "src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg"
// },
// {
// "src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg"
// }
// ]
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("name", "Premium Quality") catch {};
json.updateString("type", "simple") catch {};
json.updateString("regular_price", "21.99") catch {};
json.updateString("description", "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.") catch {};
json.updateString("short_description", "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.") catch {};
json.updateInt("categories[0].id", 9) catch {};
json.updateInt("categories[1].id", 14) catch {};
json.updateString("images[0].src", "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg") catch {};
json.updateString("images[1].src", "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg") catch {};
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("POST", "https://example.com/wp-json/wc/v3/products", json, "application/json", resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
resp.getBodySb(sb_response_body) catch {};
const j_resp = try chilkat.JsonObject.init();
defer j_resp.deinit();
j_resp.loadSb(sb_response_body) catch {};
j_resp.setEmitCompact(false);
std.debug.print("Response Body:\n", .{});
std.debug.print("{s}\n", .{try j_resp.emit(alloc)});
const resp_status_code = resp.getStatusCode();
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 resp.getHeader(alloc)});
std.debug.print("Failed.\n", .{});
return;
}
// 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
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 int_val: i32 = 0;
var src: [:0]const u8 = "";
var alt: [:0]const u8 = "";
var href: [:0]const u8 = "";
var id: i32 = j_resp.intOf("id");
var name: [:0]const u8 = try j_resp.stringOf(alloc, "name");
var slug: [:0]const u8 = try j_resp.stringOf(alloc, "slug");
_ = try j_resp.stringOf(alloc, "permalink");
j_resp.dtOf("date_created", false, date_created) catch {};
j_resp.dtOf("date_created_gmt", false, date_created_gmt) catch {};
j_resp.dtOf("date_modified", false, date_modified) catch {};
j_resp.dtOf("date_modified_gmt", false, date_modified_gmt) catch {};
_ = try j_resp.stringOf(alloc, "type");
_ = try j_resp.stringOf(alloc, "status");
_ = j_resp.boolOf("featured");
_ = try j_resp.stringOf(alloc, "catalog_visibility");
_ = try j_resp.stringOf(alloc, "description");
_ = try j_resp.stringOf(alloc, "short_description");
_ = try j_resp.stringOf(alloc, "sku");
_ = try j_resp.stringOf(alloc, "price");
_ = try j_resp.stringOf(alloc, "regular_price");
_ = try j_resp.stringOf(alloc, "sale_price");
j_resp.dtOf("date_on_sale_from", false, date_on_sale_from) catch {};
j_resp.dtOf("date_on_sale_from_gmt", false, date_on_sale_from_gmt) catch {};
j_resp.dtOf("date_on_sale_to", false, date_on_sale_to) catch {};
j_resp.dtOf("date_on_sale_to_gmt", false, date_on_sale_to_gmt) catch {};
_ = try j_resp.stringOf(alloc, "price_html");
_ = j_resp.boolOf("on_sale");
_ = j_resp.boolOf("purchasable");
_ = j_resp.intOf("total_sales");
_ = j_resp.boolOf("virtual");
_ = j_resp.boolOf("downloadable");
_ = j_resp.intOf("download_limit");
_ = j_resp.intOf("download_expiry");
_ = try j_resp.stringOf(alloc, "external_url");
_ = try j_resp.stringOf(alloc, "button_text");
_ = try j_resp.stringOf(alloc, "tax_status");
_ = try j_resp.stringOf(alloc, "tax_class");
_ = j_resp.boolOf("manage_stock");
_ = try j_resp.stringOf(alloc, "stock_quantity");
_ = try j_resp.stringOf(alloc, "stock_status");
_ = try j_resp.stringOf(alloc, "backorders");
_ = j_resp.boolOf("backorders_allowed");
_ = j_resp.boolOf("backordered");
_ = j_resp.boolOf("sold_individually");
_ = try j_resp.stringOf(alloc, "weight");
_ = try j_resp.stringOf(alloc, "dimensions.length");
_ = try j_resp.stringOf(alloc, "dimensions.width");
_ = try j_resp.stringOf(alloc, "dimensions.height");
_ = j_resp.boolOf("shipping_required");
_ = j_resp.boolOf("shipping_taxable");
_ = try j_resp.stringOf(alloc, "shipping_class");
_ = j_resp.intOf("shipping_class_id");
_ = j_resp.boolOf("reviews_allowed");
_ = try j_resp.stringOf(alloc, "average_rating");
_ = j_resp.intOf("rating_count");
_ = j_resp.intOf("parent_id");
_ = try j_resp.stringOf(alloc, "purchase_note");
_ = j_resp.intOf("menu_order");
var i: i32 = 0;
var count_i: i32 = j_resp.sizeOfArray("downloads");
while (i < count_i) {
j_resp.setI(i);
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("related_ids");
while (i < count_i) {
j_resp.setI(i);
int_val = j_resp.intOf("related_ids[i]");
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("upsell_ids");
while (i < count_i) {
j_resp.setI(i);
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("cross_sell_ids");
while (i < count_i) {
j_resp.setI(i);
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("categories");
while (i < count_i) {
j_resp.setI(i);
id = j_resp.intOf("categories[i].id");
name = try j_resp.stringOf(alloc, "categories[i].name");
slug = try j_resp.stringOf(alloc, "categories[i].slug");
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("tags");
while (i < count_i) {
j_resp.setI(i);
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("images");
while (i < count_i) {
j_resp.setI(i);
id = j_resp.intOf("images[i].id");
j_resp.dtOf("images[i].date_created", false, date_created) catch {};
j_resp.dtOf("images[i].date_created_gmt", false, date_created_gmt) catch {};
j_resp.dtOf("images[i].date_modified", false, date_modified) catch {};
j_resp.dtOf("images[i].date_modified_gmt", false, date_modified_gmt) catch {};
src = try j_resp.stringOf(alloc, "images[i].src");
name = try j_resp.stringOf(alloc, "images[i].name");
alt = try j_resp.stringOf(alloc, "images[i].alt");
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("attributes");
while (i < count_i) {
j_resp.setI(i);
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("default_attributes");
while (i < count_i) {
j_resp.setI(i);
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("variations");
while (i < count_i) {
j_resp.setI(i);
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("grouped_products");
while (i < count_i) {
j_resp.setI(i);
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("meta_data");
while (i < count_i) {
j_resp.setI(i);
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("_links.self");
while (i < count_i) {
j_resp.setI(i);
href = try j_resp.stringOf(alloc, "_links.self[i].href");
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("_links.collection");
while (i < count_i) {
j_resp.setI(i);
href = try j_resp.stringOf(alloc, "_links.collection[i].href");
i = i + 1;
}
}