Xbase++
Xbase++
Shopify Add a metafield to an existing product
See more Shopify Examples
Add a metafield to an existing productChilkat 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 nProductImageId
LOCAL nProductImageProduct_id
LOCAL nProductImagePosition
LOCAL cProductImageCreated_at
LOCAL cProductImageUpdated_at
LOCAL nProductImageWidth
LOCAL nProductImageHeight
LOCAL cProductImageSrc
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 cInventory_management
LOCAL cOption1
LOCAL nOption2
LOCAL nOption3
LOCAL cCreated_at
LOCAL cUpdated_at
LOCAL nTaxable
LOCAL cBarcode
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
LOCAL nWidth
LOCAL nHeight
LOCAL cSrc
LOCAL nIntVal
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:UpdateNumber("product.id", "632910392")
oJsonReq:UpdateString("product.metafields[0].key", "new")
oJsonReq:UpdateString("product.metafields[0].value", "newvalue")
oJsonReq:UpdateString("product.metafields[0].value_type", "string")
oJsonReq: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"
// }
// ]
// }
// }
oSbReq := CreateObject("Chilkat.StringBuilder")
oJsonReq:EmitSb(oSbReq)
oRest:AddHeader("Content-Type", "application/json")
oSbJson := CreateObject("Chilkat.StringBuilder")
nSuccess := oRest:FullRequestSb("PUT", "/admin/products/#{id}.json", oSbReq, oSbJson)
IF (nSuccess != 1)
? oRest:LastErrorText
oRest:destroy()
oJsonReq:destroy()
oSbReq:destroy()
oSbJson:destroy()
RETURN
ENDIF
IF (oRest:ResponseStatusCode != 200)
? "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")
nProductImageId := oJson:IntOf("product.image.id")
nProductImageProduct_id := oJson:IntOf("product.image.product_id")
nProductImagePosition := oJson:IntOf("product.image.position")
cProductImageCreated_at := oJson:StringOf("product.image.created_at")
cProductImageUpdated_at := oJson:StringOf("product.image.updated_at")
nProductImageWidth := oJson:IntOf("product.image.width")
nProductImageHeight := oJson:IntOf("product.image.height")
cProductImageSrc := oJson:StringOf("product.image.src")
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")
cInventory_management := oJson:StringOf("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")
cBarcode := oJson:StringOf("product.variants[i].barcode")
nImage_id := oJson:IntOf("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
nId := oJson:IntOf("product.images[i].id")
nProduct_id := oJson:IntOf("product.images[i].product_id")
nPosition := oJson:IntOf("product.images[i].position")
cCreated_at := oJson:StringOf("product.images[i].created_at")
cUpdated_at := oJson:StringOf("product.images[i].updated_at")
nWidth := oJson:IntOf("product.images[i].width")
nHeight := oJson:IntOf("product.images[i].height")
cSrc := oJson:StringOf("product.images[i].src")
j := 0
nCount_j := oJson:SizeOfArray("product.images[i].variant_ids")
DO WHILE j < nCount_j
oJson:J := j
nIntVal := oJson:IntOf("product.images[i].variant_ids[j]")
j := j + 1
ENDDO
i := i + 1
ENDDO
i := 0
nCount_i := oJson:SizeOfArray("image.product.variant_ids")
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": 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": [
// ]
// }
// }
// }
? "Example Completed."
oRest:destroy()
oJsonReq:destroy()
oSbReq:destroy()
oSbJson:destroy()
oJson:destroy()