Swift
Swift
WiX Create Product
See more WiX Examples
Creates a new product.Note: If you get a 403 error response, try refreshing the access token.
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://www.wixapis.com/stores/v1/products' \
// --data-binary '{
// "product": {
// "name": "T-shirt",
// "productType": "physical",
// "priceData": {
// "price": 10.5
// },
// "description": "nice summer t-shirt",
// "sku": "123df",
// "visible": false,
// "weight": 0.2,
// "discount": {
// "type": "AMOUNT",
// "value": 1
// },
// "manageVariants": true,
// "productOptions": [
// {
// "name": "Size",
// "choices": [
// {
// "value": "S",
// "description": "S"
// },
// {
// "value": "L",
// "description": "L"
// }
// ]
// }
// ]
// }
// }' \
// -H 'Content-Type: application/json' \
// -H 'Authorization: <AUTH>'
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "product": {
// "name": "T-shirt",
// "productType": "physical",
// "priceData": {
// "price": 10.5
// },
// "description": "nice summer t-shirt",
// "sku": "123df",
// "visible": false,
// "weight": 0.2,
// "discount": {
// "type": "AMOUNT",
// "value": 1
// },
// "manageVariants": true,
// "productOptions": [
// {
// "name": "Size",
// "choices": [
// {
// "value": "S",
// "description": "S"
// },
// {
// "value": "L",
// "description": "L"
// }
// ]
// }
// ]
// }
// }
let json = CkoJsonObject()!
json.updateString(jsonPath: "product.name", value: "T-shirt")
json.updateString(jsonPath: "product.productType", value: "physical")
json.updateNumber(jsonPath: "product.priceData.price", numericStr: "10.5")
json.updateString(jsonPath: "product.description", value: "nice summer t-shirt")
json.updateString(jsonPath: "product.sku", value: "123df")
json.updateBool(jsonPath: "product.visible", value: false)
json.updateNumber(jsonPath: "product.weight", numericStr: "0.2")
json.updateString(jsonPath: "product.discount.type", value: "AMOUNT")
json.updateInt(jsonPath: "product.discount.value", value: 1)
json.updateBool(jsonPath: "product.manageVariants", value: true)
json.updateString(jsonPath: "product.productOptions[0].name", value: "Size")
json.updateString(jsonPath: "product.productOptions[0].choices[0].value", value: "S")
json.updateString(jsonPath: "product.productOptions[0].choices[0].description", value: "S")
json.updateString(jsonPath: "product.productOptions[0].choices[1].value", value: "L")
json.updateString(jsonPath: "product.productOptions[0].choices[1].description", value: "L")
http.authToken = "ACCESS_TOKEN"
let resp = CkoHttpResponse()!
success = http.httpJson(verb: "POST", url: "https://www.wixapis.com/stores/v1/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)
// {
// "product": {
// "id": "e28e4ddd-6ed0-4098-a5e5-cf4bd9c4f7b5",
// "name": "T-shirt",
// "slug": "t-shirt-1",
// "visible": false,
// "productType": "physical",
// "description": "nice summer t-shirt",
// "stock": {
// "trackInventory": false,
// "inStock": true
// },
// "price": {
// "currency": "ILS",
// "price": 10.5,
// "discountedPrice": 9.5,
// "formatted": {
// "price": "10.50",
// "discountedPrice": "9.50"
// }
// },
// "priceData": {
// "currency": "ILS",
// "price": 10.5,
// "discountedPrice": 9.5,
// "formatted": {
// "price": "10.50",
// "discountedPrice": "9.50"
// }
// },
// "additionalInfoSections": [
// ],
// "ribbons": [
// ],
// "media": {
// "items": [
// ]
// },
// "customTextFields": [
// ],
// "manageVariants": true,
// "productOptions": [
// {
// "optionType": "drop_down",
// "name": "Size",
// "choices": [
// {
// "value": "S",
// "description": "S",
// "inStock": true,
// "visible": true
// },
// {
// "value": "L",
// "description": "L",
// "inStock": true,
// "visible": true
// }
// ]
// }
// ],
// "productPageUrl": {
// "base": "https://www.itsjusttooeasy123.com/",
// "path": "/product-page/t-shirt-1"
// },
// "numericId": "1567588455405000",
// "inventoryItemId": "1d71b222-912f-bf67-5a1a-30b4263b084a",
// "discount": {
// "type": "AMOUNT",
// "value": 1
// },
// "collectionIds": [
// ],
// "variants": [
// {
// "id": "00000000-0000-0001-0005-93fc95e0514a",
// "choices": {
// "Size": "S"
// },
// "variant": {
// "priceData": {
// "currency": "ILS",
// "price": 10.5,
// "discountedPrice": 9.5,
// "formatted": {
// "price": "10.50",
// "discountedPrice": "9.50"
// }
// },
// "weight": 10,
// "visible": true
// }
// },
// {
// "id": "00000000-0000-0002-0005-93fc95e0514a",
// "choices": {
// "Size": "L"
// },
// "variant": {
// "priceData": {
// "currency": "ILS",
// "price": 10.5,
// "discountedPrice": 9.5,
// "formatted": {
// "price": "10.50",
// "discountedPrice": "9.50"
// }
// },
// "visible": true
// }
// }
// ]
// }
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
var optionType: String?
var name: String?
var j: Int
var count_j: Int
var value: String?
var description: String?
var inStock: Bool
var visible: Bool
var id: String?
var choicesSize: String?
var variantPriceDataCurrency: String?
var variantPriceDataPrice: String?
var variantPriceDataDiscountedPrice: String?
var variantPriceDataFormattedPrice: String?
var variantPriceDataFormattedDiscountedPrice: String?
var variantWeight: Int
var variantVisible: Bool
var productId: String? = jResp.string(of: "product.id")
var productName: String? = jResp.string(of: "product.name")
var productSlug: String? = jResp.string(of: "product.slug")
var productVisible: Bool = jResp.bool(of: "product.visible")
var productProductType: String? = jResp.string(of: "product.productType")
var productDescription: String? = jResp.string(of: "product.description")
var productStockTrackInventory: Bool = jResp.bool(of: "product.stock.trackInventory")
var productStockInStock: Bool = jResp.bool(of: "product.stock.inStock")
var productPriceCurrency: String? = jResp.string(of: "product.price.currency")
var productPricePrice: String? = jResp.string(of: "product.price.price")
var productPriceDiscountedPrice: String? = jResp.string(of: "product.price.discountedPrice")
var productPriceFormattedPrice: String? = jResp.string(of: "product.price.formatted.price")
var productPriceFormattedDiscountedPrice: String? = jResp.string(of: "product.price.formatted.discountedPrice")
var productPriceDataCurrency: String? = jResp.string(of: "product.priceData.currency")
var productPriceDataPrice: String? = jResp.string(of: "product.priceData.price")
var productPriceDataDiscountedPrice: String? = jResp.string(of: "product.priceData.discountedPrice")
var productPriceDataFormattedPrice: String? = jResp.string(of: "product.priceData.formatted.price")
var productPriceDataFormattedDiscountedPrice: String? = jResp.string(of: "product.priceData.formatted.discountedPrice")
var productManageVariants: Bool = jResp.bool(of: "product.manageVariants")
var productProductPageUrlBase: String? = jResp.string(of: "product.productPageUrl.base")
var productProductPageUrlPath: String? = jResp.string(of: "product.productPageUrl.path")
var productNumericId: String? = jResp.string(of: "product.numericId")
var productInventoryItemId: String? = jResp.string(of: "product.inventoryItemId")
var productDiscountType: String? = jResp.string(of: "product.discount.type")
var productDiscountValue: Int = jResp.int(of: "product.discount.value").intValue
var i: Int = 0
var count_i: Int = jResp.size(ofArray: "product.additionalInfoSections").intValue
while i < count_i {
jResp.i = i
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "product.ribbons").intValue
while i < count_i {
jResp.i = i
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "product.media.items").intValue
while i < count_i {
jResp.i = i
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "product.customTextFields").intValue
while i < count_i {
jResp.i = i
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "product.productOptions").intValue
while i < count_i {
jResp.i = i
optionType = jResp.string(of: "product.productOptions[i].optionType")
name = jResp.string(of: "product.productOptions[i].name")
j = 0
count_j = jResp.size(ofArray: "product.productOptions[i].choices").intValue
while j < count_j {
jResp.j = j
value = jResp.string(of: "product.productOptions[i].choices[j].value")
description = jResp.string(of: "product.productOptions[i].choices[j].description")
inStock = jResp.bool(of: "product.productOptions[i].choices[j].inStock")
visible = jResp.bool(of: "product.productOptions[i].choices[j].visible")
j = j + 1
}
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "product.collectionIds").intValue
while i < count_i {
jResp.i = i
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "product.variants").intValue
while i < count_i {
jResp.i = i
id = jResp.string(of: "product.variants[i].id")
choicesSize = jResp.string(of: "product.variants[i].choices.Size")
variantPriceDataCurrency = jResp.string(of: "product.variants[i].variant.priceData.currency")
variantPriceDataPrice = jResp.string(of: "product.variants[i].variant.priceData.price")
variantPriceDataDiscountedPrice = jResp.string(of: "product.variants[i].variant.priceData.discountedPrice")
variantPriceDataFormattedPrice = jResp.string(of: "product.variants[i].variant.priceData.formatted.price")
variantPriceDataFormattedDiscountedPrice = jResp.string(of: "product.variants[i].variant.priceData.formatted.discountedPrice")
variantWeight = jResp.int(of: "product.variants[i].variant.weight").intValue
variantVisible = jResp.bool(of: "product.variants[i].variant.visible")
i = i + 1
}
}