Sample code for 30+ languages & platforms
PowerBuilder

Shopify Add a metafield to an existing product

See more Shopify Examples

Add a metafield to an existing product

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_ProductImageId
integer li_ProductImageProduct_id
integer li_ProductImagePosition
string ls_ProductImageCreated_at
string ls_ProductImageUpdated_at
integer li_ProductImageWidth
integer li_ProductImageHeight
string ls_ProductImageSrc
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
string ls_Inventory_management
string ls_Option1
integer li_Option2
integer li_Option3
string ls_Created_at
string ls_Updated_at
integer li_Taxable
string ls_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
integer li_Width
integer li_Height
string ls_Src
integer li_IntVal

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.UpdateNumber("product.id","632910392")
loo_JsonReq.UpdateString("product.metafields[0].key","new")
loo_JsonReq.UpdateString("product.metafields[0].value","newvalue")
loo_JsonReq.UpdateString("product.metafields[0].value_type","string")
loo_JsonReq.UpdateString("product.metafields[0].namespace","global")

// The JSON request body created by the above code:

// {
//   "product": {
//     "id": 632910392,
//     "metafields": [
//       {
//         "key": "new",
//         "value": "newvalue",
//         "value_type": "string",
//         "namespace": "global"
//       }
//     ]
//   }
// }

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("PUT","/admin/products/#{id}.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 <> 200 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_ProductImageId = loo_Json.IntOf("product.image.id")
li_ProductImageProduct_id = loo_Json.IntOf("product.image.product_id")
li_ProductImagePosition = loo_Json.IntOf("product.image.position")
ls_ProductImageCreated_at = loo_Json.StringOf("product.image.created_at")
ls_ProductImageUpdated_at = loo_Json.StringOf("product.image.updated_at")
li_ProductImageWidth = loo_Json.IntOf("product.image.width")
li_ProductImageHeight = loo_Json.IntOf("product.image.height")
ls_ProductImageSrc = loo_Json.StringOf("product.image.src")
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")
    ls_Inventory_management = loo_Json.StringOf("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")
    ls_Barcode = loo_Json.StringOf("product.variants[i].barcode")
    li_Image_id = loo_Json.IntOf("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
    li_Id = loo_Json.IntOf("product.images[i].id")
    li_Product_id = loo_Json.IntOf("product.images[i].product_id")
    li_Position = loo_Json.IntOf("product.images[i].position")
    ls_Created_at = loo_Json.StringOf("product.images[i].created_at")
    ls_Updated_at = loo_Json.StringOf("product.images[i].updated_at")
    li_Width = loo_Json.IntOf("product.images[i].width")
    li_Height = loo_Json.IntOf("product.images[i].height")
    ls_Src = loo_Json.StringOf("product.images[i].src")
    j = 0
    li_Count_j = loo_Json.SizeOfArray("product.images[i].variant_ids")
    do while j < li_Count_j
        loo_Json.J = j
        li_IntVal = loo_Json.IntOf("product.images[i].variant_ids[j]")
        j = j + 1
    loop
    i = i + 1
loop
i = 0
li_Count_i = loo_Json.SizeOfArray("image.product.variant_ids")
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": 632910392,
//     "title": "IPod Nano - 8GB",
//     "body_html": "<p>It's the small iPod with one very big idea: Video. Now the world's most popular music player, available in 4GB and 8GB models, lets you enjoy TV shows, movies, video podcasts, and more. The larger, brighter display means amazing picture quality. In six eye-catching colors, iPod nano is stunning all around. And with models starting at just $149, little speaks volumes.<\/p>",
//     "vendor": "Apple",
//     "product_type": "Cult Products",
//     "created_at": "2017-09-22T14:08:02-04:00",
//     "handle": "ipod-nano",
//     "updated_at": "2017-09-22T14:48:57-04:00",
//     "published_at": "2007-12-31T19:00:00-05:00",
//     "template_suffix": null,
//     "published_scope": "web",
//     "tags": "Emotive, Flash Memory, MP3, Music",
//     "variants": [
//       {
//         "id": 808950810,
//         "product_id": 632910392,
//         "title": "Pink",
//         "price": "199.00",
//         "sku": "IPOD2008PINK",
//         "position": 1,
//         "grams": 567,
//         "inventory_policy": "continue",
//         "compare_at_price": null,
//         "fulfillment_service": "manual",
//         "inventory_management": "shopify",
//         "option1": "Pink",
//         "option2": null,
//         "option3": null,
//         "created_at": "2017-09-22T14:08:02-04:00",
//         "updated_at": "2017-09-22T14:48:57-04:00",
//         "taxable": true,
//         "barcode": "1234_pink",
//         "image_id": 562641783,
//         "inventory_quantity": 10,
//         "weight": 1.25,
//         "weight_unit": "lb",
//         "old_inventory_quantity": 10,
//         "requires_shipping": true
//       },
//       {
//         "id": 49148385,
//         "product_id": 632910392,
//         "title": "Red",
//         "price": "199.00",
//         "sku": "IPOD2008RED",
//         "position": 2,
//         "grams": 567,
//         "inventory_policy": "continue",
//         "compare_at_price": null,
//         "fulfillment_service": "manual",
//         "inventory_management": "shopify",
//         "option1": "Red",
//         "option2": null,
//         "option3": null,
//         "created_at": "2017-09-22T14:08:02-04:00",
//         "updated_at": "2017-09-22T14:08:02-04:00",
//         "taxable": true,
//         "barcode": "1234_red",
//         "image_id": null,
//         "inventory_quantity": 20,
//         "weight": 1.25,
//         "weight_unit": "lb",
//         "old_inventory_quantity": 20,
//         "requires_shipping": true
//       },
//       {
//         "id": 39072856,
//         "product_id": 632910392,
//         "title": "Green",
//         "price": "199.00",
//         "sku": "IPOD2008GREEN",
//         "position": 3,
//         "grams": 567,
//         "inventory_policy": "continue",
//         "compare_at_price": null,
//         "fulfillment_service": "manual",
//         "inventory_management": "shopify",
//         "option1": "Green",
//         "option2": null,
//         "option3": null,
//         "created_at": "2017-09-22T14:08:02-04:00",
//         "updated_at": "2017-09-22T14:08:02-04:00",
//         "taxable": true,
//         "barcode": "1234_green",
//         "image_id": null,
//         "inventory_quantity": 30,
//         "weight": 1.25,
//         "weight_unit": "lb",
//         "old_inventory_quantity": 30,
//         "requires_shipping": true
//       },
//       {
//         "id": 457924702,
//         "product_id": 632910392,
//         "title": "Black",
//         "price": "199.00",
//         "sku": "IPOD2008BLACK",
//         "position": 4,
//         "grams": 567,
//         "inventory_policy": "continue",
//         "compare_at_price": null,
//         "fulfillment_service": "manual",
//         "inventory_management": "shopify",
//         "option1": "Black",
//         "option2": null,
//         "option3": null,
//         "created_at": "2017-09-22T14:08:02-04:00",
//         "updated_at": "2017-09-22T14:08:02-04:00",
//         "taxable": true,
//         "barcode": "1234_black",
//         "image_id": null,
//         "inventory_quantity": 40,
//         "weight": 1.25,
//         "weight_unit": "lb",
//         "old_inventory_quantity": 40,
//         "requires_shipping": true
//       }
//     ],
//     "options": [
//       {
//         "id": 594680422,
//         "product_id": 632910392,
//         "name": "Color",
//         "position": 1,
//         "values": [
//           "Pink",
//           "Red",
//           "Green",
//           "Black"
//         ]
//       }
//     ],
//     "images": [
//       {
//         "id": 850703190,
//         "product_id": 632910392,
//         "position": 1,
//         "created_at": "2017-09-22T14:08:02-04:00",
//         "updated_at": "2017-09-22T14:08:02-04:00",
//         "width": 123,
//         "height": 456,
//         "src": "https:\/\/cdn.shopify.com\/s\/files\/1\/0006\/9093\/3842\/products\/ipod-nano.png?v=1506103682",
//         "variant_ids": [
//         ]
//       },
//       {
//         "id": 562641783,
//         "product_id": 632910392,
//         "position": 2,
//         "created_at": "2017-09-22T14:08:02-04:00",
//         "updated_at": "2017-09-22T14:08:02-04:00",
//         "width": 123,
//         "height": 456,
//         "src": "https:\/\/cdn.shopify.com\/s\/files\/1\/0006\/9093\/3842\/products\/ipod-nano-2.png?v=1506103682",
//         "variant_ids": [
//           808950810
//         ]
//       }
//     ],
//     "image": {
//       "id": 850703190,
//       "product_id": 632910392,
//       "position": 1,
//       "created_at": "2017-09-22T14:08:02-04:00",
//       "updated_at": "2017-09-22T14:08:02-04:00",
//       "width": 123,
//       "height": 456,
//       "src": "https:\/\/cdn.shopify.com\/s\/files\/1\/0006\/9093\/3842\/products\/ipod-nano.png?v=1506103682",
//       "variant_ids": [
//       ]
//     }
//   }
// }

Write-Debug "Example Completed."


destroy loo_Rest
destroy loo_JsonReq
destroy loo_SbReq
destroy loo_SbJson
destroy loo_Json