Sample code for 30+ languages & platforms
PowerBuilder

Shopify Create a new product with multiple product variants

See more Shopify Examples

Create a new product with multiple product variants

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Rest
oleobject loo_JsonReq
oleobject loo_SbReq
oleobject loo_SbJson
oleobject loo_Json
integer li_ProductId
string ls_ProductTitle
string ls_ProductBody_html
string ls_ProductVendor
string ls_ProductProduct_type
string ls_ProductCreated_at
string ls_ProductHandle
string ls_ProductUpdated_at
string ls_ProductPublished_at
integer li_ProductTemplate_suffix
string ls_ProductPublished_scope
string ls_ProductTags
integer li_ProductImage
integer i
integer li_Count_i
integer li_Id
integer li_Product_id
string ls_Title
string ls_Price
string ls_Sku
integer li_Position
integer li_Grams
string ls_Inventory_policy
integer li_Compare_at_price
string ls_Fulfillment_service
integer li_Inventory_management
string ls_Option1
integer li_Option2
integer li_Option3
string ls_Created_at
string ls_Updated_at
integer li_Taxable
integer li_Barcode
integer li_Image_id
integer li_Inventory_quantity
integer li_Weight
string ls_Weight_unit
integer li_Old_inventory_quantity
integer li_Requires_shipping
string ls_Name
integer j
integer li_Count_j
string ls_StrVal

li_Success = 0

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
if li_rc < 0 then
    destroy loo_Rest
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Rest.SetAuthBasic("SHOPIFY_PRIVATE_API_KEY","SHOPIFY_PRIVATE_API_KEY")

li_Success = loo_Rest.Connect("chilkat.myshopify.com",443,1,1)
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    return
end if

// The following code creates the JSON request body.
// The JSON created by this code is shown below.
loo_JsonReq = create oleobject
li_rc = loo_JsonReq.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonReq.UpdateString("product.title","Burton Custom Freestyle 151")
loo_JsonReq.UpdateString("product.body_html","<strong>Good snowboard!</strong>")
loo_JsonReq.UpdateString("product.vendor","Burton")
loo_JsonReq.UpdateString("product.product_type","Snowboard")
loo_JsonReq.UpdateString("product.variants[0].option1","First")
loo_JsonReq.UpdateString("product.variants[0].price","10.00")
loo_JsonReq.UpdateString("product.variants[0].sku","123")
loo_JsonReq.UpdateString("product.variants[1].option1","Second")
loo_JsonReq.UpdateString("product.variants[1].price","20.00")
loo_JsonReq.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"
//       }
//     ]
//   }
// }

loo_SbReq = create oleobject
li_rc = loo_SbReq.ConnectToNewObject("Chilkat.StringBuilder")

loo_JsonReq.EmitSb(loo_SbReq)

loo_Rest.AddHeader("Content-Type","application/json")

loo_SbJson = create oleobject
li_rc = loo_SbJson.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Rest.FullRequestSb("POST","/admin/products.json ",loo_SbReq,loo_SbJson)
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_JsonReq
    destroy loo_SbReq
    destroy loo_SbJson
    return
end if

if loo_Rest.ResponseStatusCode <> 201 then
    Write-Debug "Received error response code: " + string(loo_Rest.ResponseStatusCode)
    Write-Debug "Response body:"
    Write-Debug loo_SbJson.GetAsString()
    destroy loo_Rest
    destroy loo_JsonReq
    destroy loo_SbReq
    destroy loo_SbJson
    return
end if

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.LoadSb(loo_SbJson)

// The following code parses the JSON response.
// A sample JSON response is shown below the sample code.

li_ProductId = loo_Json.IntOf("product.id")
ls_ProductTitle = loo_Json.StringOf("product.title")
ls_ProductBody_html = loo_Json.StringOf("product.body_html")
ls_ProductVendor = loo_Json.StringOf("product.vendor")
ls_ProductProduct_type = loo_Json.StringOf("product.product_type")
ls_ProductCreated_at = loo_Json.StringOf("product.created_at")
ls_ProductHandle = loo_Json.StringOf("product.handle")
ls_ProductUpdated_at = loo_Json.StringOf("product.updated_at")
ls_ProductPublished_at = loo_Json.StringOf("product.published_at")
li_ProductTemplate_suffix = loo_Json.IsNullOf("product.template_suffix")
ls_ProductPublished_scope = loo_Json.StringOf("product.published_scope")
ls_ProductTags = loo_Json.StringOf("product.tags")
li_ProductImage = loo_Json.IsNullOf("product.image")
i = 0
li_Count_i = loo_Json.SizeOfArray("product.variants")
do while i < li_Count_i
    loo_Json.I = i
    li_Id = loo_Json.IntOf("product.variants[i].id")
    li_Product_id = loo_Json.IntOf("product.variants[i].product_id")
    ls_Title = loo_Json.StringOf("product.variants[i].title")
    ls_Price = loo_Json.StringOf("product.variants[i].price")
    ls_Sku = loo_Json.StringOf("product.variants[i].sku")
    li_Position = loo_Json.IntOf("product.variants[i].position")
    li_Grams = loo_Json.IntOf("product.variants[i].grams")
    ls_Inventory_policy = loo_Json.StringOf("product.variants[i].inventory_policy")
    li_Compare_at_price = loo_Json.IsNullOf("product.variants[i].compare_at_price")
    ls_Fulfillment_service = loo_Json.StringOf("product.variants[i].fulfillment_service")
    li_Inventory_management = loo_Json.IsNullOf("product.variants[i].inventory_management")
    ls_Option1 = loo_Json.StringOf("product.variants[i].option1")
    li_Option2 = loo_Json.IsNullOf("product.variants[i].option2")
    li_Option3 = loo_Json.IsNullOf("product.variants[i].option3")
    ls_Created_at = loo_Json.StringOf("product.variants[i].created_at")
    ls_Updated_at = loo_Json.StringOf("product.variants[i].updated_at")
    li_Taxable = loo_Json.BoolOf("product.variants[i].taxable")
    li_Barcode = loo_Json.IsNullOf("product.variants[i].barcode")
    li_Image_id = loo_Json.IsNullOf("product.variants[i].image_id")
    li_Inventory_quantity = loo_Json.IntOf("product.variants[i].inventory_quantity")
    li_Weight = loo_Json.IntOf("product.variants[i].weight")
    ls_Weight_unit = loo_Json.StringOf("product.variants[i].weight_unit")
    li_Old_inventory_quantity = loo_Json.IntOf("product.variants[i].old_inventory_quantity")
    li_Requires_shipping = loo_Json.BoolOf("product.variants[i].requires_shipping")
    i = i + 1
loop
i = 0
li_Count_i = loo_Json.SizeOfArray("product.options")
do while i < li_Count_i
    loo_Json.I = i
    li_Id = loo_Json.IntOf("product.options[i].id")
    li_Product_id = loo_Json.IntOf("product.options[i].product_id")
    ls_Name = loo_Json.StringOf("product.options[i].name")
    li_Position = loo_Json.IntOf("product.options[i].position")
    j = 0
    li_Count_j = loo_Json.SizeOfArray("product.options[i].values")
    do while j < li_Count_j
        loo_Json.J = j
        ls_StrVal = loo_Json.StringOf("product.options[i].values[j]")
        j = j + 1
    loop
    i = i + 1
loop
i = 0
li_Count_i = loo_Json.SizeOfArray("product.images")
do while i < li_Count_i
    loo_Json.I = i
    i = i + 1
loop

// 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
//   }
// }

Write-Debug "Example Completed."


destroy loo_Rest
destroy loo_JsonReq
destroy loo_SbReq
destroy loo_SbJson
destroy loo_Json