Sample code for 30+ languages & platforms
Zig

Shopify Update a product and associated variants and images

See more Shopify Examples

Update a product and associated variants and images

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    const rest = try chilkat.Rest.init();
    defer rest.deinit();

    rest.setAuthBasic("SHOPIFY_PRIVATE_API_KEY", "SHOPIFY_PRIVATE_API_KEY") catch {};

    rest.connect("chilkat.myshopify.com", 443, true, true) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // The following code creates the JSON request body.
    // The JSON created by this code is shown below.
    const json_req = try chilkat.JsonObject.init();
    defer json_req.deinit();
    json_req.updateNumber("product.id", "632910392") catch {};
    json_req.updateBool("product.published", false) catch {};

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

    // {
    //   "product": {
    //     "id": 632910392,
    //     "published": false
    //   }
    // }

    const sb_req = try chilkat.StringBuilder.init();
    defer sb_req.deinit();
    json_req.emitSb(sb_req) catch {};

    rest.addHeader("Content-Type", "application/json") catch {};

    const sb_json = try chilkat.StringBuilder.init();
    defer sb_json.deinit();
    rest.fullRequestSb("PUT", "/admin/products/#{id}.json", sb_req, sb_json) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    if (rest.getResponseStatusCode() != 200) {
        std.debug.print("Received error response code: {d}\n", .{rest.getResponseStatusCode()});
        std.debug.print("Response body:\n", .{});
        std.debug.print("{s}\n", .{try sb_json.getAsString(alloc)});
        return;
    }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.loadSb(sb_json) catch {};

    // The following code parses the JSON response.
    // A sample JSON response is shown below the sample code.
    var product_id: i32 = 0;
    var i: i32 = 0;
    var count_i: i32 = 0;
    var id: i32 = 0;

    var title: [:0]const u8 = "";
    var price: [:0]const u8 = "";
    var sku: [:0]const u8 = "";
    var position: i32 = 0;
    var grams: i32 = 0;
    var inventory_policy: [:0]const u8 = "";
    var compare_at_price: bool = false;
    var fulfillment_service: [:0]const u8 = "";
    var inventory_management: [:0]const u8 = "";
    var option1: [:0]const u8 = "";
    var option2: bool = false;
    var option3: bool = false;
    var created_at: [:0]const u8 = "";
    var updated_at: [:0]const u8 = "";
    var taxable: bool = false;
    var barcode: [:0]const u8 = "";
    var image_id: i32 = 0;
    var inventory_quantity: i32 = 0;
    var weight: i32 = 0;
    var weight_unit: [:0]const u8 = "";
    var old_inventory_quantity: i32 = 0;
    var requires_shipping: bool = false;
    var name: [:0]const u8 = "";
    var j: i32 = 0;
    var count_j: i32 = 0;
    var str_val: [:0]const u8 = "";
    var width: i32 = 0;
    var height: i32 = 0;
    var src: [:0]const u8 = "";
    var int_val: i32 = 0;

    product_id = json.intOf("product.id");
    _ = try json.stringOf(alloc, "product.title");
    _ = try json.stringOf(alloc, "product.body_html");
    _ = try json.stringOf(alloc, "product.vendor");
    _ = try json.stringOf(alloc, "product.product_type");
    _ = try json.stringOf(alloc, "product.created_at");
    _ = try json.stringOf(alloc, "product.handle");
    _ = try json.stringOf(alloc, "product.updated_at");
    _ = json.isNullOf("product.published_at");
    _ = json.isNullOf("product.template_suffix");
    _ = try json.stringOf(alloc, "product.published_scope");
    _ = try json.stringOf(alloc, "product.tags");
    _ = json.intOf("product.image.id");
    _ = json.intOf("product.image.product_id");
    _ = json.intOf("product.image.position");
    _ = try json.stringOf(alloc, "product.image.created_at");
    _ = try json.stringOf(alloc, "product.image.updated_at");
    _ = json.intOf("product.image.width");
    _ = json.intOf("product.image.height");
    _ = try json.stringOf(alloc, "product.image.src");
    i = 0;
    count_i = json.sizeOfArray("product.variants");
    while (i < count_i) {
        json.setI(i);
        id = json.intOf("product.variants[i].id");
        product_id = json.intOf("product.variants[i].product_id");
        title = try json.stringOf(alloc, "product.variants[i].title");
        price = try json.stringOf(alloc, "product.variants[i].price");
        sku = try json.stringOf(alloc, "product.variants[i].sku");
        position = json.intOf("product.variants[i].position");
        grams = json.intOf("product.variants[i].grams");
        inventory_policy = try json.stringOf(alloc, "product.variants[i].inventory_policy");
        compare_at_price = json.isNullOf("product.variants[i].compare_at_price");
        fulfillment_service = try json.stringOf(alloc, "product.variants[i].fulfillment_service");
        inventory_management = try json.stringOf(alloc, "product.variants[i].inventory_management");
        option1 = try json.stringOf(alloc, "product.variants[i].option1");
        option2 = json.isNullOf("product.variants[i].option2");
        option3 = json.isNullOf("product.variants[i].option3");
        created_at = try json.stringOf(alloc, "product.variants[i].created_at");
        updated_at = try json.stringOf(alloc, "product.variants[i].updated_at");
        taxable = json.boolOf("product.variants[i].taxable");
        barcode = try json.stringOf(alloc, "product.variants[i].barcode");
        image_id = json.intOf("product.variants[i].image_id");
        inventory_quantity = json.intOf("product.variants[i].inventory_quantity");
        weight = json.intOf("product.variants[i].weight");
        weight_unit = try json.stringOf(alloc, "product.variants[i].weight_unit");
        old_inventory_quantity = json.intOf("product.variants[i].old_inventory_quantity");
        requires_shipping = json.boolOf("product.variants[i].requires_shipping");
        i = i + 1;
    }

    i = 0;
    count_i = json.sizeOfArray("product.options");
    while (i < count_i) {
        json.setI(i);
        id = json.intOf("product.options[i].id");
        product_id = json.intOf("product.options[i].product_id");
        name = try json.stringOf(alloc, "product.options[i].name");
        position = json.intOf("product.options[i].position");
        j = 0;
        count_j = json.sizeOfArray("product.options[i].values");
        while (j < count_j) {
            json.setJ(j);
            str_val = try json.stringOf(alloc, "product.options[i].values[j]");
            j = j + 1;
        }

        i = i + 1;
    }

    i = 0;
    count_i = json.sizeOfArray("product.images");
    while (i < count_i) {
        json.setI(i);
        id = json.intOf("product.images[i].id");
        product_id = json.intOf("product.images[i].product_id");
        position = json.intOf("product.images[i].position");
        created_at = try json.stringOf(alloc, "product.images[i].created_at");
        updated_at = try json.stringOf(alloc, "product.images[i].updated_at");
        width = json.intOf("product.images[i].width");
        height = json.intOf("product.images[i].height");
        src = try json.stringOf(alloc, "product.images[i].src");
        j = 0;
        count_j = json.sizeOfArray("product.images[i].variant_ids");
        while (j < count_j) {
            json.setJ(j);
            int_val = json.intOf("product.images[i].variant_ids[j]");
            j = j + 1;
        }

        i = i + 1;
    }

    i = 0;
    count_i = json.sizeOfArray("image.product.variant_ids");
    while (i < count_i) {
        json.setI(i);
        i = i + 1;
    }

    // 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:36-04:00",
    //     "published_at": null,
    //     "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:37-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": [
    //       ]
    //     }
    //   }
    // }

    std.debug.print("Example Completed.\n", .{});
}