Swift
Swift
WooCommerce Create a Product
See more WooCommerce Examples
Demonstrates how to create a new product in WooCommerce.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = CkoHttp()!
// 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.basicAuth = 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.
// {
// "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"
// }
// ]
// }
let json = CkoJsonObject()!
json.updateString(jsonPath: "name", value: "Premium Quality")
json.updateString(jsonPath: "type", value: "simple")
json.updateString(jsonPath: "regular_price", value: "21.99")
json.updateString(jsonPath: "description", value: "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.")
json.updateString(jsonPath: "short_description", value: "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.")
json.updateInt(jsonPath: "categories[0].id", value: 9)
json.updateInt(jsonPath: "categories[1].id", value: 14)
json.updateString(jsonPath: "images[0].src", value: "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg")
json.updateString(jsonPath: "images[1].src", value: "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg")
let resp = CkoHttpResponse()!
success = http.httpJson(verb: "POST", url: "https://example.com/wp-json/wc/v3/products", json: json, contentType: "application/json", response: resp)
if success == false {
print("\(http.lastErrorText!)")
return
}
let sbResponseBody = CkoStringBuilder()!
resp.getBodySb(sb: sbResponseBody)
let jResp = CkoJsonObject()!
jResp.loadSb(sb: sbResponseBody)
jResp.emitCompact = false
print("Response Body:")
print("\(jResp.emit()!)")
var respStatusCode: Int = resp.statusCode.intValue
print("Response Status Code = \(respStatusCode)")
if respStatusCode >= 400 {
print("Response Header:")
print("\(resp.header!)")
print("Failed.")
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
let date_created = CkoDtObj()!
let date_created_gmt = CkoDtObj()!
let date_modified = CkoDtObj()!
let date_modified_gmt = CkoDtObj()!
let date_on_sale_from = CkoDtObj()!
let date_on_sale_from_gmt = CkoDtObj()!
let date_on_sale_to = CkoDtObj()!
let date_on_sale_to_gmt = CkoDtObj()!
var intVal: Int
var src: String?
var alt: String?
var href: String?
var id: Int = jResp.int(of: "id").intValue
var name: String? = jResp.string(of: "name")
var slug: String? = jResp.string(of: "slug")
var permalink: String? = jResp.string(of: "permalink")
jResp.dt(of: "date_created", bLocal: false, dt: date_created)
jResp.dt(of: "date_created_gmt", bLocal: false, dt: date_created_gmt)
jResp.dt(of: "date_modified", bLocal: false, dt: date_modified)
jResp.dt(of: "date_modified_gmt", bLocal: false, dt: date_modified_gmt)
var v_type: String? = jResp.string(of: "type")
var status: String? = jResp.string(of: "status")
var featured: Bool = jResp.bool(of: "featured")
var catalog_visibility: String? = jResp.string(of: "catalog_visibility")
var description: String? = jResp.string(of: "description")
var short_description: String? = jResp.string(of: "short_description")
var sku: String? = jResp.string(of: "sku")
var price: String? = jResp.string(of: "price")
var regular_price: String? = jResp.string(of: "regular_price")
var sale_price: String? = jResp.string(of: "sale_price")
jResp.dt(of: "date_on_sale_from", bLocal: false, dt: date_on_sale_from)
jResp.dt(of: "date_on_sale_from_gmt", bLocal: false, dt: date_on_sale_from_gmt)
jResp.dt(of: "date_on_sale_to", bLocal: false, dt: date_on_sale_to)
jResp.dt(of: "date_on_sale_to_gmt", bLocal: false, dt: date_on_sale_to_gmt)
var price_html: String? = jResp.string(of: "price_html")
var on_sale: Bool = jResp.bool(of: "on_sale")
var purchasable: Bool = jResp.bool(of: "purchasable")
var total_sales: Int = jResp.int(of: "total_sales").intValue
var virtual: Bool = jResp.bool(of: "virtual")
var downloadable: Bool = jResp.bool(of: "downloadable")
var download_limit: Int = jResp.int(of: "download_limit").intValue
var download_expiry: Int = jResp.int(of: "download_expiry").intValue
var external_url: String? = jResp.string(of: "external_url")
var button_text: String? = jResp.string(of: "button_text")
var tax_status: String? = jResp.string(of: "tax_status")
var tax_class: String? = jResp.string(of: "tax_class")
var manage_stock: Bool = jResp.bool(of: "manage_stock")
var stock_quantity: String? = jResp.string(of: "stock_quantity")
var stock_status: String? = jResp.string(of: "stock_status")
var backorders: String? = jResp.string(of: "backorders")
var backorders_allowed: Bool = jResp.bool(of: "backorders_allowed")
var backordered: Bool = jResp.bool(of: "backordered")
var sold_individually: Bool = jResp.bool(of: "sold_individually")
var weight: String? = jResp.string(of: "weight")
var dimensionsLength: String? = jResp.string(of: "dimensions.length")
var dimensionsWidth: String? = jResp.string(of: "dimensions.width")
var dimensionsHeight: String? = jResp.string(of: "dimensions.height")
var shipping_required: Bool = jResp.bool(of: "shipping_required")
var shipping_taxable: Bool = jResp.bool(of: "shipping_taxable")
var shipping_class: String? = jResp.string(of: "shipping_class")
var shipping_class_id: Int = jResp.int(of: "shipping_class_id").intValue
var reviews_allowed: Bool = jResp.bool(of: "reviews_allowed")
var average_rating: String? = jResp.string(of: "average_rating")
var rating_count: Int = jResp.int(of: "rating_count").intValue
var parent_id: Int = jResp.int(of: "parent_id").intValue
var purchase_note: String? = jResp.string(of: "purchase_note")
var menu_order: Int = jResp.int(of: "menu_order").intValue
var i: Int = 0
var count_i: Int = jResp.size(ofArray: "downloads").intValue
while i < count_i {
jResp.i = i
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "related_ids").intValue
while i < count_i {
jResp.i = i
intVal = jResp.int(of: "related_ids[i]").intValue
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "upsell_ids").intValue
while i < count_i {
jResp.i = i
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "cross_sell_ids").intValue
while i < count_i {
jResp.i = i
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "categories").intValue
while i < count_i {
jResp.i = i
id = jResp.int(of: "categories[i].id").intValue
name = jResp.string(of: "categories[i].name")
slug = jResp.string(of: "categories[i].slug")
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "tags").intValue
while i < count_i {
jResp.i = i
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "images").intValue
while i < count_i {
jResp.i = i
id = jResp.int(of: "images[i].id").intValue
jResp.dt(of: "images[i].date_created", bLocal: false, dt: date_created)
jResp.dt(of: "images[i].date_created_gmt", bLocal: false, dt: date_created_gmt)
jResp.dt(of: "images[i].date_modified", bLocal: false, dt: date_modified)
jResp.dt(of: "images[i].date_modified_gmt", bLocal: false, dt: 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
}
i = 0
count_i = jResp.size(ofArray: "attributes").intValue
while i < count_i {
jResp.i = i
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "default_attributes").intValue
while i < count_i {
jResp.i = i
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "variations").intValue
while i < count_i {
jResp.i = i
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "grouped_products").intValue
while i < count_i {
jResp.i = i
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "meta_data").intValue
while i < count_i {
jResp.i = i
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "_links.self").intValue
while i < count_i {
jResp.i = i
href = jResp.string(of: "_links.self[i].href")
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "_links.collection").intValue
while i < count_i {
jResp.i = i
href = jResp.string(of: "_links.collection[i].href")
i = i + 1
}
}