Xbase++
Xbase++
Shopify Create a new product with multiple product variants
See more Shopify Examples
Create a new product with multiple product variantsChilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oRest
LOCAL oJsonReq
LOCAL oSbReq
LOCAL oSbJson
LOCAL oJson
LOCAL nProductId
LOCAL cProductTitle
LOCAL cProductBody_html
LOCAL cProductVendor
LOCAL cProductProduct_type
LOCAL cProductCreated_at
LOCAL cProductHandle
LOCAL cProductUpdated_at
LOCAL cProductPublished_at
LOCAL nProductTemplate_suffix
LOCAL cProductPublished_scope
LOCAL cProductTags
LOCAL nProductImage
LOCAL i
LOCAL nCount_i
LOCAL nId
LOCAL nProduct_id
LOCAL cTitle
LOCAL cPrice
LOCAL cSku
LOCAL nPosition
LOCAL nGrams
LOCAL cInventory_policy
LOCAL nCompare_at_price
LOCAL cFulfillment_service
LOCAL nInventory_management
LOCAL cOption1
LOCAL nOption2
LOCAL nOption3
LOCAL cCreated_at
LOCAL cUpdated_at
LOCAL nTaxable
LOCAL nBarcode
LOCAL nImage_id
LOCAL nInventory_quantity
LOCAL nWeight
LOCAL cWeight_unit
LOCAL nOld_inventory_quantity
LOCAL nRequires_shipping
LOCAL cName
LOCAL j
LOCAL nCount_j
LOCAL cStrVal
nSuccess := 0
oRest := CreateObject("Chilkat.Rest")
oRest:SetAuthBasic("SHOPIFY_PRIVATE_API_KEY", "SHOPIFY_PRIVATE_API_KEY")
nSuccess := oRest:Connect("chilkat.myshopify.com", 443, 1, 1)
IF (nSuccess != 1)
? oRest:LastErrorText
oRest:destroy()
RETURN
ENDIF
// The following code creates the JSON request body.
// The JSON created by this code is shown below.
oJsonReq := CreateObject("Chilkat.JsonObject")
oJsonReq:UpdateString("product.title", "Burton Custom Freestyle 151")
oJsonReq:UpdateString("product.body_html", "<strong>Good snowboard!</strong>")
oJsonReq:UpdateString("product.vendor", "Burton")
oJsonReq:UpdateString("product.product_type", "Snowboard")
oJsonReq:UpdateString("product.variants[0].option1", "First")
oJsonReq:UpdateString("product.variants[0].price", "10.00")
oJsonReq:UpdateString("product.variants[0].sku", "123")
oJsonReq:UpdateString("product.variants[1].option1", "Second")
oJsonReq:UpdateString("product.variants[1].price", "20.00")
oJsonReq:UpdateString("product.variants[1].sku", "123")
// The JSON request body created by the above code:
// {
// "product": {
// "title": "Burton Custom Freestyle 151",
// "body_html": "<strong>Good snowboard!<\/strong>",
// "vendor": "Burton",
// "product_type": "Snowboard",
// "variants": [
// {
// "option1": "First",
// "price": "10.00",
// "sku": "123"
// },
// {
// "option1": "Second",
// "price": "20.00",
// "sku": "123"
// }
// ]
// }
// }
oSbReq := CreateObject("Chilkat.StringBuilder")
oJsonReq:EmitSb(oSbReq)
oRest:AddHeader("Content-Type", "application/json")
oSbJson := CreateObject("Chilkat.StringBuilder")
nSuccess := oRest:FullRequestSb("POST", "/admin/products.json ", oSbReq, oSbJson)
IF (nSuccess != 1)
? oRest:LastErrorText
oRest:destroy()
oJsonReq:destroy()
oSbReq:destroy()
oSbJson:destroy()
RETURN
ENDIF
IF (oRest:ResponseStatusCode != 201)
? "Received error response code: " + Str(oRest:ResponseStatusCode)
? "Response body:"
? oSbJson:GetAsString()
oRest:destroy()
oJsonReq:destroy()
oSbReq:destroy()
oSbJson:destroy()
RETURN
ENDIF
oJson := CreateObject("Chilkat.JsonObject")
oJson:LoadSb(oSbJson)
// The following code parses the JSON response.
// A sample JSON response is shown below the sample code.
nProductId := oJson:IntOf("product.id")
cProductTitle := oJson:StringOf("product.title")
cProductBody_html := oJson:StringOf("product.body_html")
cProductVendor := oJson:StringOf("product.vendor")
cProductProduct_type := oJson:StringOf("product.product_type")
cProductCreated_at := oJson:StringOf("product.created_at")
cProductHandle := oJson:StringOf("product.handle")
cProductUpdated_at := oJson:StringOf("product.updated_at")
cProductPublished_at := oJson:StringOf("product.published_at")
nProductTemplate_suffix := oJson:IsNullOf("product.template_suffix")
cProductPublished_scope := oJson:StringOf("product.published_scope")
cProductTags := oJson:StringOf("product.tags")
nProductImage := oJson:IsNullOf("product.image")
i := 0
nCount_i := oJson:SizeOfArray("product.variants")
DO WHILE i < nCount_i
oJson:I := i
nId := oJson:IntOf("product.variants[i].id")
nProduct_id := oJson:IntOf("product.variants[i].product_id")
cTitle := oJson:StringOf("product.variants[i].title")
cPrice := oJson:StringOf("product.variants[i].price")
cSku := oJson:StringOf("product.variants[i].sku")
nPosition := oJson:IntOf("product.variants[i].position")
nGrams := oJson:IntOf("product.variants[i].grams")
cInventory_policy := oJson:StringOf("product.variants[i].inventory_policy")
nCompare_at_price := oJson:IsNullOf("product.variants[i].compare_at_price")
cFulfillment_service := oJson:StringOf("product.variants[i].fulfillment_service")
nInventory_management := oJson:IsNullOf("product.variants[i].inventory_management")
cOption1 := oJson:StringOf("product.variants[i].option1")
nOption2 := oJson:IsNullOf("product.variants[i].option2")
nOption3 := oJson:IsNullOf("product.variants[i].option3")
cCreated_at := oJson:StringOf("product.variants[i].created_at")
cUpdated_at := oJson:StringOf("product.variants[i].updated_at")
nTaxable := oJson:BoolOf("product.variants[i].taxable")
nBarcode := oJson:IsNullOf("product.variants[i].barcode")
nImage_id := oJson:IsNullOf("product.variants[i].image_id")
nInventory_quantity := oJson:IntOf("product.variants[i].inventory_quantity")
nWeight := oJson:IntOf("product.variants[i].weight")
cWeight_unit := oJson:StringOf("product.variants[i].weight_unit")
nOld_inventory_quantity := oJson:IntOf("product.variants[i].old_inventory_quantity")
nRequires_shipping := oJson:BoolOf("product.variants[i].requires_shipping")
i := i + 1
ENDDO
i := 0
nCount_i := oJson:SizeOfArray("product.options")
DO WHILE i < nCount_i
oJson:I := i
nId := oJson:IntOf("product.options[i].id")
nProduct_id := oJson:IntOf("product.options[i].product_id")
cName := oJson:StringOf("product.options[i].name")
nPosition := oJson:IntOf("product.options[i].position")
j := 0
nCount_j := oJson:SizeOfArray("product.options[i].values")
DO WHILE j < nCount_j
oJson:J := j
cStrVal := oJson:StringOf("product.options[i].values[j]")
j := j + 1
ENDDO
i := i + 1
ENDDO
i := 0
nCount_i := oJson:SizeOfArray("product.images")
DO WHILE i < nCount_i
oJson:I := i
i := i + 1
ENDDO
// A sample JSON response body that is parsed by the above code:
// {
// "product": {
// "id": 1071559755,
// "title": "Burton Custom Freestyle 151",
// "body_html": "<strong>Good snowboard!<\/strong>",
// "vendor": "Burton",
// "product_type": "Snowboard",
// "created_at": "2017-09-22T14:48:54-04:00",
// "handle": "burton-custom-freestyle-151",
// "updated_at": "2017-09-22T14:48:55-04:00",
// "published_at": "2017-09-22T14:48:54-04:00",
// "template_suffix": null,
// "published_scope": "global",
// "tags": "",
// "variants": [
// {
// "id": 1070325225,
// "product_id": 1071559755,
// "title": "First",
// "price": "10.00",
// "sku": "123",
// "position": 1,
// "grams": 0,
// "inventory_policy": "deny",
// "compare_at_price": null,
// "fulfillment_service": "manual",
// "inventory_management": null,
// "option1": "First",
// "option2": null,
// "option3": null,
// "created_at": "2017-09-22T14:48:54-04:00",
// "updated_at": "2017-09-22T14:48:54-04:00",
// "taxable": true,
// "barcode": null,
// "image_id": null,
// "inventory_quantity": 1,
// "weight": 0.0,
// "weight_unit": "lb",
// "old_inventory_quantity": 1,
// "requires_shipping": true
// },
// {
// "id": 1070325226,
// "product_id": 1071559755,
// "title": "Second",
// "price": "20.00",
// "sku": "123",
// "position": 2,
// "grams": 0,
// "inventory_policy": "deny",
// "compare_at_price": null,
// "fulfillment_service": "manual",
// "inventory_management": null,
// "option1": "Second",
// "option2": null,
// "option3": null,
// "created_at": "2017-09-22T14:48:54-04:00",
// "updated_at": "2017-09-22T14:48:54-04:00",
// "taxable": true,
// "barcode": null,
// "image_id": null,
// "inventory_quantity": 1,
// "weight": 0.0,
// "weight_unit": "lb",
// "old_inventory_quantity": 1,
// "requires_shipping": true
// }
// ],
// "options": [
// {
// "id": 1022828915,
// "product_id": 1071559755,
// "name": "Title",
// "position": 1,
// "values": [
// "First",
// "Second"
// ]
// }
// ],
// "images": [
// ],
// "image": null
// }
// }
? "Example Completed."
oRest:destroy()
oJsonReq:destroy()
oSbReq:destroy()
oSbJson:destroy()
oJson:destroy()