Sample code for 30+ languages & platforms
Zig

Shopware 6 - Find Product by Name

See more Shopware 6 Examples

Find a product with a given name and get the id.

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();

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const http = try chilkat.Http.init();
    defer http.deinit();

    // Sends the following POST to find the product named "Test123"

    // POST /api/v3/search/product
    // {
    //     "filter": [
    //         { "type": "equals", "field": "name", "value": "Test123" }
    //     ],
    //     "includes": {
    //         "product": ["id", "name"]
    //     }
    // }

    // Create the above JSON.
    // Use this online tool to generate code from sample JSON:
    // Generate Code to Create JSON

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("filter[0].type", "equals") catch {};
    json.updateString("filter[0].field", "name") catch {};
    json.updateString("filter[0].value", "Test123") catch {};
    json.updateString("includes.product[0]", "id") catch {};
    json.updateString("includes.product[1]", "name") catch {};
    //

    // Load the access token previously obtained in Shopware 6 OAuth2 Client Credentials
    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    json_token.loadFile("qa_data/tokens/shopware6.json") catch {};

    // This causes the "Authorization: Bearer <access_token>" header to be added.
    http.setAuthToken(try json_token.stringOf(alloc, "access_token"));

    // Note: If you get a 401 response status code, then fetch a new access token and retry.
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://my-shopware-6-shop.de/api/v3/search/product", json, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    resp.getBodySb(sb_response_body) catch {};
    const j_resp = try chilkat.JsonObject.init();
    defer j_resp.deinit();
    j_resp.loadSb(sb_response_body) catch {};
    j_resp.setEmitCompact(false);

    std.debug.print("Response Body:\n", .{});
    std.debug.print("{s}\n", .{try j_resp.emit(alloc)});

    // If we get a 401 response, it may be that our access token expired and we need to fetch a new one.
    const resp_status_code = resp.getStatusCode();
    std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
    if (resp_status_code >= 400) {
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try resp.getHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    // The response looks like this:
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    // {
    //   "data": [
    //     {
    //       "id": "d195dabb53cf46d096bd9a8726d82558",
    //       "type": "product",
    //       "attributes": {
    //         "name": "Test123",
    //         "apiAlias": null
    //       },
    //       "links": {
    //         "self": "https:\/\/***.de\/api\/v3\/product\/d195dabb53cf46d096bd9a8726d82558"
    //       },
    //       "relationships": [
    //       ],
    //       "meta": null
    //     }
    //   ],
    //   "included": [
    //   ],
    //   "links": {
    //     "self": "https:\/\/***.de\/api\/v3\/search\/product"
    //   },
    //   "meta": {
    //     "totalCountMode": 0,
    //     "total": 1
    //   },
    //   "aggregations": [
    //   ]
    // }

    const num_search_results = j_resp.sizeOfArray("data");
    if (num_search_results != 1) {
        std.debug.print("numSearchResults = {d}\n", .{num_search_results});
        std.debug.print("Did not find the product, or unexpectedly found more than one with the same name.\n", .{});
        return;
    }

    const product_id = try j_resp.stringOf(alloc, "data[0].id");

    std.debug.print("Product ID = {s}\n", .{product_id});
}