Java
Chilkat2 Java
Shopify Create a new product with multiple product variants
See more Shopify Examples
Create a new product with multiple product variantsChilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Rest;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
Rest rest = new Rest();
rest.setAuthBasic("SHOPIFY_PRIVATE_API_KEY", "SHOPIFY_PRIVATE_API_KEY");
try {
rest.connect("chilkat.myshopify.com", 443, true, true);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// The following code creates the JSON request body.
// The JSON created by this code is shown below.
JsonObject jsonReq = new JsonObject();
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"
// }
// ]
// }
// }
StringBuilder sbReq = new StringBuilder();
jsonReq.emitSb(sbReq);
rest.addHeader("Content-Type", "application/json");
StringBuilder sbJson = new StringBuilder();
try {
rest.fullRequestSb("POST", "/admin/products.json ", sbReq, sbJson);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
if (rest.getResponseStatusCode() != 201) {
System.out.println("Received error response code: " + rest.getResponseStatusCode());
System.out.println("Response body:");
System.out.println(sbJson.getAsString());
return;
}
JsonObject json = new JsonObject();
json.loadSb(sbJson);
// The following code parses the JSON response.
// A sample JSON response is shown below the sample code.
int id = 0;
int product_id = 0;
String title = "";
String price = "";
String sku = "";
int position = 0;
int grams = 0;
String inventory_policy = "";
boolean compare_at_price = false;
String fulfillment_service = "";
boolean inventory_management = false;
String option1 = "";
boolean option2 = false;
boolean option3 = false;
String created_at = "";
String updated_at = "";
boolean taxable = false;
boolean barcode = false;
boolean image_id = false;
int inventory_quantity = 0;
int weight = 0;
String weight_unit = "";
int old_inventory_quantity = 0;
boolean requires_shipping = false;
String name = "";
int j = 0;
int count_j = 0;
String strVal = "";
int productId = json.intOf("product.id");
String productTitle = json.stringOf("product.title");
String productBody_html = json.stringOf("product.body_html");
String productVendor = json.stringOf("product.vendor");
String productProduct_type = json.stringOf("product.product_type");
String productCreated_at = json.stringOf("product.created_at");
String productHandle = json.stringOf("product.handle");
String productUpdated_at = json.stringOf("product.updated_at");
String productPublished_at = json.stringOf("product.published_at");
boolean productTemplate_suffix = json.isNullOf("product.template_suffix");
String productPublished_scope = json.stringOf("product.published_scope");
String productTags = json.stringOf("product.tags");
boolean productImage = json.isNullOf("product.image");
int i = 0;
int 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 = 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");
inventory_policy = json.stringOf("product.variants[i].inventory_policy");
compare_at_price = json.isNullOf("product.variants[i].compare_at_price");
fulfillment_service = json.stringOf("product.variants[i].fulfillment_service");
inventory_management = json.isNullOf("product.variants[i].inventory_management");
option1 = json.stringOf("product.variants[i].option1");
option2 = json.isNullOf("product.variants[i].option2");
option3 = json.isNullOf("product.variants[i].option3");
created_at = json.stringOf("product.variants[i].created_at");
updated_at = json.stringOf("product.variants[i].updated_at");
taxable = json.boolOf("product.variants[i].taxable");
barcode = json.isNullOf("product.variants[i].barcode");
image_id = json.isNullOf("product.variants[i].image_id");
inventory_quantity = json.intOf("product.variants[i].inventory_quantity");
weight = json.intOf("product.variants[i].weight");
weight_unit = json.stringOf("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 = json.stringOf("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);
strVal = json.stringOf("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);
i = i + 1;
}
// 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
// }
// }
System.out.println("Example Completed.");
}
}