Lua
Lua
WooCommerce Update a Product
See more WooCommerce Examples
Demonstrates how to make changes to a product in WooCommerce.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 -X PUT https://example.com/wp-json/wc/v3/products/794 \
-- -u consumer_key:consumer_secret \
-- -H "Content-Type: application/json" \
-- -d '{
-- "regular_price": "24.54"
-- }'
-- 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"
-- Use this online tool to generate code from sample JSON:
-- Generate Code to Create JSON
-- The following JSON is sent in the request body.
-- {
-- "regular_price": "24.54"
-- }
local json = chilkat.JsonObject.new()
json:update_string("regular_price", "24.54")
http:set_request_header("Content-Type", "application/json")
local sbRequestBody = chilkat.StringBuilder.new()
json:emit_sb(sbRequestBody)
local resp = chilkat.HttpResponse.new()
local success = http:http_sb("PUT", "https://example.com/wp-json/wc/v3/products/794", sbRequestBody, "utf-8", "application/json", resp)
if not success then
print(http.last_error_text)
return
end
local sbResponseBody = chilkat.StringBuilder.new()
resp:get_body_sb(sbResponseBody)
local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false
print("Response Body:")
print(jResp:emit())
local respStatusCode = resp.status_code
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
print("Response Header:")
print(resp.header)
print("Failed.")
return
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": "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
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 intVal = 0
local src = ""
local alt = ""
local href = ""
local id = jResp:int_of("id")
local name = jResp:string_of("name")
local slug = jResp:string_of("slug")
local permalink = jResp:string_of("permalink")
jResp:dt_of("date_created", false, date_created)
jResp:dt_of("date_created_gmt", false, date_created_gmt)
jResp:dt_of("date_modified", false, date_modified)
jResp:dt_of("date_modified_gmt", false, date_modified_gmt)
local v_type = jResp:string_of("type")
local status = jResp:string_of("status")
local featured = jResp:bool_of("featured")
local catalog_visibility = jResp:string_of("catalog_visibility")
local description = jResp:string_of("description")
local short_description = jResp:string_of("short_description")
local sku = jResp:string_of("sku")
local price = jResp:string_of("price")
local regular_price = jResp:string_of("regular_price")
local sale_price = jResp:string_of("sale_price")
jResp:dt_of("date_on_sale_from", false, date_on_sale_from)
jResp:dt_of("date_on_sale_from_gmt", false, date_on_sale_from_gmt)
jResp:dt_of("date_on_sale_to", false, date_on_sale_to)
jResp:dt_of("date_on_sale_to_gmt", false, date_on_sale_to_gmt)
local price_html = jResp:string_of("price_html")
local on_sale = jResp:bool_of("on_sale")
local purchasable = jResp:bool_of("purchasable")
local total_sales = jResp:int_of("total_sales")
local virtual = jResp:bool_of("virtual")
local downloadable = jResp:bool_of("downloadable")
local download_limit = jResp:int_of("download_limit")
local download_expiry = jResp:int_of("download_expiry")
local external_url = jResp:string_of("external_url")
local button_text = jResp:string_of("button_text")
local tax_status = jResp:string_of("tax_status")
local tax_class = jResp:string_of("tax_class")
local manage_stock = jResp:bool_of("manage_stock")
local stock_quantity = jResp:string_of("stock_quantity")
local stock_status = jResp:string_of("stock_status")
local backorders = jResp:string_of("backorders")
local backorders_allowed = jResp:bool_of("backorders_allowed")
local backordered = jResp:bool_of("backordered")
local sold_individually = jResp:bool_of("sold_individually")
local weight = jResp:string_of("weight")
local dimensionsLength = jResp:string_of("dimensions.length")
local dimensionsWidth = jResp:string_of("dimensions.width")
local dimensionsHeight = jResp:string_of("dimensions.height")
local shipping_required = jResp:bool_of("shipping_required")
local shipping_taxable = jResp:bool_of("shipping_taxable")
local shipping_class = jResp:string_of("shipping_class")
local shipping_class_id = jResp:int_of("shipping_class_id")
local reviews_allowed = jResp:bool_of("reviews_allowed")
local average_rating = jResp:string_of("average_rating")
local rating_count = jResp:int_of("rating_count")
local parent_id = jResp:int_of("parent_id")
local purchase_note = jResp:string_of("purchase_note")
local menu_order = jResp:int_of("menu_order")
local i = 0
local count_i = jResp:size_of_array("downloads")
while i < count_i do
jResp.i = i
i = i + 1
end
i = 0
count_i = jResp:size_of_array("related_ids")
while i < count_i do
jResp.i = i
intVal = jResp:int_of("related_ids[i]")
i = i + 1
end
i = 0
count_i = jResp:size_of_array("upsell_ids")
while i < count_i do
jResp.i = i
i = i + 1
end
i = 0
count_i = jResp:size_of_array("cross_sell_ids")
while i < count_i do
jResp.i = i
i = i + 1
end
i = 0
count_i = jResp:size_of_array("categories")
while i < count_i do
jResp.i = i
id = jResp:int_of("categories[i].id")
name = jResp:string_of("categories[i].name")
slug = jResp:string_of("categories[i].slug")
i = i + 1
end
i = 0
count_i = jResp:size_of_array("tags")
while i < count_i do
jResp.i = i
i = i + 1
end
i = 0
count_i = jResp:size_of_array("images")
while i < count_i do
jResp.i = i
id = jResp:int_of("images[i].id")
jResp:dt_of("images[i].date_created", false, date_created)
jResp:dt_of("images[i].date_created_gmt", false, date_created_gmt)
jResp:dt_of("images[i].date_modified", false, date_modified)
jResp:dt_of("images[i].date_modified_gmt", false, date_modified_gmt)
src = jResp:string_of("images[i].src")
name = jResp:string_of("images[i].name")
alt = jResp:string_of("images[i].alt")
i = i + 1
end
i = 0
count_i = jResp:size_of_array("attributes")
while i < count_i do
jResp.i = i
i = i + 1
end
i = 0
count_i = jResp:size_of_array("default_attributes")
while i < count_i do
jResp.i = i
i = i + 1
end
i = 0
count_i = jResp:size_of_array("variations")
while i < count_i do
jResp.i = i
i = i + 1
end
i = 0
count_i = jResp:size_of_array("grouped_products")
while i < count_i do
jResp.i = i
i = i + 1
end
i = 0
count_i = jResp:size_of_array("meta_data")
while i < count_i do
jResp.i = i
i = i + 1
end
i = 0
count_i = jResp:size_of_array("_links.self")
while i < count_i do
jResp.i = i
href = jResp:string_of("_links.self[i].href")
i = i + 1
end
i = 0
count_i = jResp:size_of_array("_links.collection")
while i < count_i do
jResp.i = i
href = jResp:string_of("_links.collection[i].href")
i = i + 1
end