Lua
Lua
WooCommerce List All Products
See more WooCommerce Examples
Gets WooCommerce product information in JSON format.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
-- 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.basic_auth = true
http.login = "consumer_key"
http.password = "consumer_secret"
local sbResponseBody = chilkat.StringBuilder.new()
local success = http:quick_get_sb("https://example.com/wp-json/wc/v3/products", sbResponseBody)
if not success then
print(http.last_error_text)
return
end
local jarrResp = chilkat.JsonArray.new()
jarrResp:load_sb(sbResponseBody)
jarrResp.emit_compact = false
print("Response Body:")
print(jarrResp:emit())
local respStatusCode = http.last_status
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
print("Response Header:")
print(http.last_header)
print("Failed.")
return
end
-- 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
local date_created = chilkat.DtObj.new()
local date_created_gmt = chilkat.DtObj.new()
local date_modified = chilkat.DtObj.new()
local date_modified_gmt = chilkat.DtObj.new()
local date_on_sale_from = chilkat.DtObj.new()
local date_on_sale_from_gmt = chilkat.DtObj.new()
local date_on_sale_to = chilkat.DtObj.new()
local date_on_sale_to_gmt = chilkat.DtObj.new()
local json
local id = 0
local name = ""
local slug = ""
local permalink = ""
local v_type = ""
local status = ""
local featured = false
local catalog_visibility = ""
local description = ""
local short_description = ""
local sku = ""
local price = ""
local regular_price = ""
local sale_price = ""
local price_html = ""
local on_sale = false
local purchasable = false
local total_sales = 0
local v_virtual = false
local downloadable = false
local download_limit = 0
local download_expiry = 0
local external_url = ""
local button_text = ""
local tax_status = ""
local tax_class = ""
local manage_stock = false
local stock_quantity = ""
local stock_status = ""
local backorders = ""
local backorders_allowed = false
local backordered = false
local sold_individually = false
local weight = ""
local dimensionsLength = ""
local dimensionsWidth = ""
local dimensionsHeight = ""
local shipping_required = false
local shipping_taxable = false
local shipping_class = ""
local shipping_class_id = 0
local reviews_allowed = false
local average_rating = ""
local rating_count = 0
local parent_id = 0
local purchase_note = ""
local menu_order = 0
local j = 0
local count_j = 0
local intVal = 0
local src = ""
local alt = ""
local position = 0
local visible = false
local variation = false
local k = 0
local count_k = 0
local strVal = ""
local v_option = ""
local href = ""
local i = 0
local count_i = jarrResp.size
while i < count_i do
json = jarrResp:object_at(i)
id = json:int_of("id")
name = json:string_of("name")
slug = json:string_of("slug")
permalink = json:string_of("permalink")
json:dt_of("date_created", false, date_created)
json:dt_of("date_created_gmt", false, date_created_gmt)
json:dt_of("date_modified", false, date_modified)
json:dt_of("date_modified_gmt", false, date_modified_gmt)
v_type = json:string_of("type")
status = json:string_of("status")
featured = json:bool_of("featured")
catalog_visibility = json:string_of("catalog_visibility")
description = json:string_of("description")
short_description = json:string_of("short_description")
sku = json:string_of("sku")
price = json:string_of("price")
regular_price = json:string_of("regular_price")
sale_price = json:string_of("sale_price")
json:dt_of("date_on_sale_from", false, date_on_sale_from)
json:dt_of("date_on_sale_from_gmt", false, date_on_sale_from_gmt)
json:dt_of("date_on_sale_to", false, date_on_sale_to)
json:dt_of("date_on_sale_to_gmt", false, date_on_sale_to_gmt)
price_html = json:string_of("price_html")
on_sale = json:bool_of("on_sale")
purchasable = json:bool_of("purchasable")
total_sales = json:int_of("total_sales")
v_virtual = json:bool_of("virtual")
downloadable = json:bool_of("downloadable")
download_limit = json:int_of("download_limit")
download_expiry = json:int_of("download_expiry")
external_url = json:string_of("external_url")
button_text = json:string_of("button_text")
tax_status = json:string_of("tax_status")
tax_class = json:string_of("tax_class")
manage_stock = json:bool_of("manage_stock")
stock_quantity = json:string_of("stock_quantity")
stock_status = json:string_of("stock_status")
backorders = json:string_of("backorders")
backorders_allowed = json:bool_of("backorders_allowed")
backordered = json:bool_of("backordered")
sold_individually = json:bool_of("sold_individually")
weight = json:string_of("weight")
dimensionsLength = json:string_of("dimensions.length")
dimensionsWidth = json:string_of("dimensions.width")
dimensionsHeight = json:string_of("dimensions.height")
shipping_required = json:bool_of("shipping_required")
shipping_taxable = json:bool_of("shipping_taxable")
shipping_class = json:string_of("shipping_class")
shipping_class_id = json:int_of("shipping_class_id")
reviews_allowed = json:bool_of("reviews_allowed")
average_rating = json:string_of("average_rating")
rating_count = json:int_of("rating_count")
parent_id = json:int_of("parent_id")
purchase_note = json:string_of("purchase_note")
menu_order = json:int_of("menu_order")
j = 0
count_j = json:size_of_array("downloads")
while j < count_j do
json.j = j
j = j + 1
end
j = 0
count_j = json:size_of_array("related_ids")
while j < count_j do
json.j = j
intVal = json:int_of("related_ids[j]")
j = j + 1
end
j = 0
count_j = json:size_of_array("upsell_ids")
while j < count_j do
json.j = j
j = j + 1
end
j = 0
count_j = json:size_of_array("cross_sell_ids")
while j < count_j do
json.j = j
j = j + 1
end
j = 0
count_j = json:size_of_array("categories")
while j < count_j do
json.j = j
id = json:int_of("categories[j].id")
name = json:string_of("categories[j].name")
slug = json:string_of("categories[j].slug")
j = j + 1
end
j = 0
count_j = json:size_of_array("tags")
while j < count_j do
json.j = j
j = j + 1
end
j = 0
count_j = json:size_of_array("images")
while j < count_j do
json.j = j
id = json:int_of("images[j].id")
json:dt_of("images[j].date_created", false, date_created)
json:dt_of("images[j].date_created_gmt", false, date_created_gmt)
json:dt_of("images[j].date_modified", false, date_modified)
json:dt_of("images[j].date_modified_gmt", false, date_modified_gmt)
src = json:string_of("images[j].src")
name = json:string_of("images[j].name")
alt = json:string_of("images[j].alt")
j = j + 1
end
j = 0
count_j = json:size_of_array("attributes")
while j < count_j do
json.j = j
id = json:int_of("attributes[j].id")
name = json:string_of("attributes[j].name")
position = json:int_of("attributes[j].position")
visible = json:bool_of("attributes[j].visible")
variation = json:bool_of("attributes[j].variation")
k = 0
count_k = json:size_of_array("attributes[j].options")
while k < count_k do
json.k = k
strVal = json:string_of("attributes[j].options[k]")
k = k + 1
end
j = j + 1
end
j = 0
count_j = json:size_of_array("default_attributes")
while j < count_j do
json.j = j
id = json:int_of("default_attributes[j].id")
name = json:string_of("default_attributes[j].name")
v_option = json:string_of("default_attributes[j].option")
j = j + 1
end
j = 0
count_j = json:size_of_array("variations")
while j < count_j do
json.j = j
j = j + 1
end
j = 0
count_j = json:size_of_array("grouped_products")
while j < count_j do
json.j = j
j = j + 1
end
j = 0
count_j = json:size_of_array("meta_data")
while j < count_j do
json.j = j
j = j + 1
end
j = 0
count_j = json:size_of_array("_links.self")
while j < count_j do
json.j = j
href = json:string_of("_links.self[j].href")
j = j + 1
end
j = 0
count_j = json:size_of_array("_links.collection")
while j < count_j do
json.j = j
href = json:string_of("_links.collection[j].href")
j = j + 1
end
i = i + 1
end