Sample code for 30+ languages & platforms
Dart

Shopify Create a new product with multiple product variants

See more Shopify Examples

Create a new product with multiple product variants

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  final rest = CkRest();

  rest.setAuthBasic('SHOPIFY_PRIVATE_API_KEY', 'SHOPIFY_PRIVATE_API_KEY');

  try {
    rest.connect('chilkat.myshopify.com', 443, true, true);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // The following code creates the JSON request body.
  // The JSON created by this code is shown below.
  final jsonReq = CkJsonObject();
  jsonReq.updateString('product.title', 'Burton Custom Freestyle 151');
  jsonReq.updateString('product.body_html', '<strong>Good snowboard!</strong>');
  jsonReq.updateString('product.vendor', 'Burton');
  jsonReq.updateString('product.product_type', 'Snowboard');
  jsonReq.updateString('product.variants[0].option1', 'First');
  jsonReq.updateString('product.variants[0].price', '10.00');
  jsonReq.updateString('product.variants[0].sku', '123');
  jsonReq.updateString('product.variants[1].option1', 'Second');
  jsonReq.updateString('product.variants[1].price', '20.00');
  jsonReq.updateString('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"
  //       }
  //     ]
  //   }
  // }

  final sbReq = CkStringBuilder();
  jsonReq.emitSb(sbReq);

  rest.addHeader('Content-Type', 'application/json');

  final sbJson = CkStringBuilder();
  try {
    rest.fullRequestSb('POST', '/admin/products.json ', sbReq, sbJson);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  if (rest.responseStatusCode != 201) {
    print('Received error response code: ${rest.responseStatusCode}');
    print('Response body:');
    print(sbJson.getAsString());
    return;
  }

  final json = CkJsonObject();
  json.loadSb(sbJson);

  // The following code parses the JSON response.
  // A sample JSON response is shown below the sample code.
  var productId = 0;
  var i = 0;
  var countI = 0;
  var id = 0;

  var title = '';
  var price = '';
  var sku = '';
  var position = 0;
  var grams = 0;
  var inventoryPolicy = '';
  var fulfillmentService = '';
  var option1 = '';
  var createdAt = '';
  var updatedAt = '';
  var inventoryQuantity = 0;
  var weight = 0;
  var weightUnit = '';
  var oldInventoryQuantity = 0;
  var name = '';
  var j = 0;
  var countJ = 0;
  var strVal = '';

  productId = json.intOf('product.id');
  final productTitle = json.stringOf('product.title');
  final productBodyHtml = json.stringOf('product.body_html');
  final productVendor = json.stringOf('product.vendor');
  final productProductType = json.stringOf('product.product_type');
  final productCreatedAt = json.stringOf('product.created_at');
  final productHandle = json.stringOf('product.handle');
  final productUpdatedAt = json.stringOf('product.updated_at');
  final productPublishedAt = json.stringOf('product.published_at');
  final productPublishedScope = json.stringOf('product.published_scope');
  final productTags = json.stringOf('product.tags');
  i = 0;
  countI = json.sizeOfArray('product.variants');
  while (i < countI) {
    json.i = i;
    id = json.intOf('product.variants[i].id');
    productId = json.intOf('product.variants[i].product_id');
    title = json.stringOf('product.variants[i].title');
    price = json.stringOf('product.variants[i].price');
    sku = json.stringOf('product.variants[i].sku');
    position = json.intOf('product.variants[i].position');
    grams = json.intOf('product.variants[i].grams');
    inventoryPolicy = json.stringOf('product.variants[i].inventory_policy');
    json.isNullOf('product.variants[i].compare_at_price');
    fulfillmentService = json.stringOf('product.variants[i].fulfillment_service');
    json.isNullOf('product.variants[i].inventory_management');
    option1 = json.stringOf('product.variants[i].option1');
    json.isNullOf('product.variants[i].option2');
    json.isNullOf('product.variants[i].option3');
    createdAt = json.stringOf('product.variants[i].created_at');
    updatedAt = json.stringOf('product.variants[i].updated_at');
    json.boolOf('product.variants[i].taxable');
    json.isNullOf('product.variants[i].barcode');
    json.isNullOf('product.variants[i].image_id');
    inventoryQuantity = json.intOf('product.variants[i].inventory_quantity');
    weight = json.intOf('product.variants[i].weight');
    weightUnit = json.stringOf('product.variants[i].weight_unit');
    oldInventoryQuantity = json.intOf('product.variants[i].old_inventory_quantity');
    json.boolOf('product.variants[i].requires_shipping');
    i++;
  }

  i = 0;
  countI = json.sizeOfArray('product.options');
  while (i < countI) {
    json.i = i;
    id = json.intOf('product.options[i].id');
    productId = json.intOf('product.options[i].product_id');
    name = json.stringOf('product.options[i].name');
    position = json.intOf('product.options[i].position');
    j = 0;
    countJ = json.sizeOfArray('product.options[i].values');
    while (j < countJ) {
      json.j = j;
      strVal = json.stringOf('product.options[i].values[j]');
      j++;
    }

    i++;
  }

  i = 0;
  countI = json.sizeOfArray('product.images');
  while (i < countI) {
    json.i = i;
    i++;
  }

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

  print('Example Completed.');
}