Sample code for 30+ languages & platforms
PureBasic

Shopify Create a new product with multiple product variants

See more Shopify Examples

Create a new product with multiple product variants

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkRest::ckSetAuthBasic(rest,"SHOPIFY_PRIVATE_API_KEY","SHOPIFY_PRIVATE_API_KEY")

    success = CkRest::ckConnect(rest,"chilkat.myshopify.com",443,1,1)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    ; The following code creates the JSON request body.
    ; The JSON created by this code is shown below.
    jsonReq.i = CkJsonObject::ckCreate()
    If jsonReq.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

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

    sbReq.i = CkStringBuilder::ckCreate()
    If sbReq.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckEmitSb(jsonReq,sbReq)

    CkRest::ckAddHeader(rest,"Content-Type","application/json")

    sbJson.i = CkStringBuilder::ckCreate()
    If sbJson.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRest::ckFullRequestSb(rest,"POST","/admin/products.json ",sbReq,sbJson)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkJsonObject::ckDispose(jsonReq)
        CkStringBuilder::ckDispose(sbReq)
        CkStringBuilder::ckDispose(sbJson)
        ProcedureReturn
    EndIf

    If CkRest::ckResponseStatusCode(rest) <> 201
        Debug "Received error response code: " + Str(CkRest::ckResponseStatusCode(rest))
        Debug "Response body:"
        Debug CkStringBuilder::ckGetAsString(sbJson)
        CkRest::ckDispose(rest)
        CkJsonObject::ckDispose(jsonReq)
        CkStringBuilder::ckDispose(sbReq)
        CkStringBuilder::ckDispose(sbJson)
        ProcedureReturn
    EndIf

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(json,sbJson)

    ; The following code parses the JSON response.
    ; A sample JSON response is shown below the sample code.
    productId.i
    productTitle.s
    productBody_html.s
    productVendor.s
    productProduct_type.s
    productCreated_at.s
    productHandle.s
    productUpdated_at.s
    productPublished_at.s
    productTemplate_suffix.i
    productPublished_scope.s
    productTags.s
    productImage.i
    i.i
    count_i.i
    id.i
    product_id.i
    title.s
    price.s
    sku.s
    position.i
    grams.i
    inventory_policy.s
    compare_at_price.i
    fulfillment_service.s
    inventory_management.i
    option1.s
    option2.i
    option3.i
    created_at.s
    updated_at.s
    taxable.i
    barcode.i
    image_id.i
    inventory_quantity.i
    weight.i
    weight_unit.s
    old_inventory_quantity.i
    requires_shipping.i
    name.s
    j.i
    count_j.i
    strVal.s

    productId = CkJsonObject::ckIntOf(json,"product.id")
    productTitle = CkJsonObject::ckStringOf(json,"product.title")
    productBody_html = CkJsonObject::ckStringOf(json,"product.body_html")
    productVendor = CkJsonObject::ckStringOf(json,"product.vendor")
    productProduct_type = CkJsonObject::ckStringOf(json,"product.product_type")
    productCreated_at = CkJsonObject::ckStringOf(json,"product.created_at")
    productHandle = CkJsonObject::ckStringOf(json,"product.handle")
    productUpdated_at = CkJsonObject::ckStringOf(json,"product.updated_at")
    productPublished_at = CkJsonObject::ckStringOf(json,"product.published_at")
    productTemplate_suffix = CkJsonObject::ckIsNullOf(json,"product.template_suffix")
    productPublished_scope = CkJsonObject::ckStringOf(json,"product.published_scope")
    productTags = CkJsonObject::ckStringOf(json,"product.tags")
    productImage = CkJsonObject::ckIsNullOf(json,"product.image")
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(json,"product.variants")
    While i < count_i
        CkJsonObject::setCkI(json, i)
        id = CkJsonObject::ckIntOf(json,"product.variants[i].id")
        product_id = CkJsonObject::ckIntOf(json,"product.variants[i].product_id")
        title = CkJsonObject::ckStringOf(json,"product.variants[i].title")
        price = CkJsonObject::ckStringOf(json,"product.variants[i].price")
        sku = CkJsonObject::ckStringOf(json,"product.variants[i].sku")
        position = CkJsonObject::ckIntOf(json,"product.variants[i].position")
        grams = CkJsonObject::ckIntOf(json,"product.variants[i].grams")
        inventory_policy = CkJsonObject::ckStringOf(json,"product.variants[i].inventory_policy")
        compare_at_price = CkJsonObject::ckIsNullOf(json,"product.variants[i].compare_at_price")
        fulfillment_service = CkJsonObject::ckStringOf(json,"product.variants[i].fulfillment_service")
        inventory_management = CkJsonObject::ckIsNullOf(json,"product.variants[i].inventory_management")
        option1 = CkJsonObject::ckStringOf(json,"product.variants[i].option1")
        option2 = CkJsonObject::ckIsNullOf(json,"product.variants[i].option2")
        option3 = CkJsonObject::ckIsNullOf(json,"product.variants[i].option3")
        created_at = CkJsonObject::ckStringOf(json,"product.variants[i].created_at")
        updated_at = CkJsonObject::ckStringOf(json,"product.variants[i].updated_at")
        taxable = CkJsonObject::ckBoolOf(json,"product.variants[i].taxable")
        barcode = CkJsonObject::ckIsNullOf(json,"product.variants[i].barcode")
        image_id = CkJsonObject::ckIsNullOf(json,"product.variants[i].image_id")
        inventory_quantity = CkJsonObject::ckIntOf(json,"product.variants[i].inventory_quantity")
        weight = CkJsonObject::ckIntOf(json,"product.variants[i].weight")
        weight_unit = CkJsonObject::ckStringOf(json,"product.variants[i].weight_unit")
        old_inventory_quantity = CkJsonObject::ckIntOf(json,"product.variants[i].old_inventory_quantity")
        requires_shipping = CkJsonObject::ckBoolOf(json,"product.variants[i].requires_shipping")
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(json,"product.options")
    While i < count_i
        CkJsonObject::setCkI(json, i)
        id = CkJsonObject::ckIntOf(json,"product.options[i].id")
        product_id = CkJsonObject::ckIntOf(json,"product.options[i].product_id")
        name = CkJsonObject::ckStringOf(json,"product.options[i].name")
        position = CkJsonObject::ckIntOf(json,"product.options[i].position")
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(json,"product.options[i].values")
        While j < count_j
            CkJsonObject::setCkJ(json, j)
            strVal = CkJsonObject::ckStringOf(json,"product.options[i].values[j]")
            j = j + 1
        Wend
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(json,"product.images")
    While i < count_i
        CkJsonObject::setCkI(json, i)
        i = i + 1
    Wend

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

    Debug "Example Completed."


    CkRest::ckDispose(rest)
    CkJsonObject::ckDispose(jsonReq)
    CkStringBuilder::ckDispose(sbReq)
    CkStringBuilder::ckDispose(sbJson)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure