Sample code for 30+ languages & platforms
Android™

WiX Create Product

See more WiX Examples

Creates a new product.

Note: If you get a 403 error response, try refreshing the access token.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

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

    CkHttp http = new CkHttp();

    // Implements the following CURL command:

    // curl -X POST \
    //    'https://www.wixapis.com/stores/v1/products' \
    //     --data-binary '{
    //                      "product": {
    //                        "name": "T-shirt",
    //                        "productType": "physical",
    //                        "priceData": {
    //                          "price": 10.5
    //                        },
    //                        "description": "nice summer t-shirt",
    //                        "sku": "123df",
    //                        "visible": false,
    //                        "weight": 0.2,
    //                        "discount": {
    //                        	"type": "AMOUNT",
    //                        	"value": 1
    //                        },
    //                        "manageVariants": true,
    //                        "productOptions": [
    //                          {
    //                            "name": "Size",
    //                            "choices": [
    //                              {
    //                                "value": "S",
    //                                "description": "S"
    //                              },
    //                              {
    //                                "value": "L",
    //                                "description": "L"
    //                              }
    //                            ]
    //                          }
    //                        ]
    //                      }
    //                    }' \
    //    -H 'Content-Type: application/json' \
    //    -H 'Authorization: <AUTH>'

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

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

    // The following JSON is sent in the request body.

    // {
    //   "product": {
    //     "name": "T-shirt",
    //     "productType": "physical",
    //     "priceData": {
    //       "price": 10.5
    //     },
    //     "description": "nice summer t-shirt",
    //     "sku": "123df",
    //     "visible": false,
    //     "weight": 0.2,
    //     "discount": {
    //       "type": "AMOUNT",
    //       "value": 1
    //     },
    //     "manageVariants": true,
    //     "productOptions": [
    //       {
    //         "name": "Size",
    //         "choices": [
    //           {
    //             "value": "S",
    //             "description": "S"
    //           },
    //           {
    //             "value": "L",
    //             "description": "L"
    //           }
    //         ]
    //       }
    //     ]
    //   }
    // }

    CkJsonObject json = new CkJsonObject();
    json.UpdateString("product.name","T-shirt");
    json.UpdateString("product.productType","physical");
    json.UpdateNumber("product.priceData.price","10.5");
    json.UpdateString("product.description","nice summer t-shirt");
    json.UpdateString("product.sku","123df");
    json.UpdateBool("product.visible",false);
    json.UpdateNumber("product.weight","0.2");
    json.UpdateString("product.discount.type","AMOUNT");
    json.UpdateInt("product.discount.value",1);
    json.UpdateBool("product.manageVariants",true);
    json.UpdateString("product.productOptions[0].name","Size");
    json.UpdateString("product.productOptions[0].choices[0].value","S");
    json.UpdateString("product.productOptions[0].choices[0].description","S");
    json.UpdateString("product.productOptions[0].choices[1].value","L");
    json.UpdateString("product.productOptions[0].choices[1].description","L");

    http.put_AuthToken("ACCESS_TOKEN");

    CkHttpResponse resp = new CkHttpResponse();
    success = http.HttpJson("POST","https://www.wixapis.com/stores/v1/products",json,"application/json",resp);
    if (success == false) {
        Log.i(TAG, http.lastErrorText());
        return;
        }

    CkStringBuilder sbResponseBody = new CkStringBuilder();
    resp.GetBodySb(sbResponseBody);
    CkJsonObject jResp = new CkJsonObject();
    jResp.LoadSb(sbResponseBody);
    jResp.put_EmitCompact(false);

    Log.i(TAG, "Response Body:");
    Log.i(TAG, jResp.emit());

    int respStatusCode = resp.get_StatusCode();
    Log.i(TAG, "Response Status Code = " + String.valueOf(respStatusCode));
    if (respStatusCode >= 400) {
        Log.i(TAG, "Response Header:");
        Log.i(TAG, resp.header());
        Log.i(TAG, "Failed.");
        return;
        }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "product": {
    //     "id": "e28e4ddd-6ed0-4098-a5e5-cf4bd9c4f7b5",
    //     "name": "T-shirt",
    //     "slug": "t-shirt-1",
    //     "visible": false,
    //     "productType": "physical",
    //     "description": "nice summer t-shirt",
    //     "stock": {
    //       "trackInventory": false,
    //       "inStock": true
    //     },
    //     "price": {
    //       "currency": "ILS",
    //       "price": 10.5,
    //       "discountedPrice": 9.5,
    //       "formatted": {
    //         "price": "10.50",
    //         "discountedPrice": "9.50"
    //       }
    //     },
    //     "priceData": {
    //       "currency": "ILS",
    //       "price": 10.5,
    //       "discountedPrice": 9.5,
    //       "formatted": {
    //         "price": "10.50",
    //         "discountedPrice": "9.50"
    //       }
    //     },
    //     "additionalInfoSections": [
    //     ],
    //     "ribbons": [
    //     ],
    //     "media": {
    //       "items": [
    //       ]
    //     },
    //     "customTextFields": [
    //     ],
    //     "manageVariants": true,
    //     "productOptions": [
    //       {
    //         "optionType": "drop_down",
    //         "name": "Size",
    //         "choices": [
    //           {
    //             "value": "S",
    //             "description": "S",
    //             "inStock": true,
    //             "visible": true
    //           },
    //           {
    //             "value": "L",
    //             "description": "L",
    //             "inStock": true,
    //             "visible": true
    //           }
    //         ]
    //       }
    //     ],
    //     "productPageUrl": {
    //       "base": "https://www.itsjusttooeasy123.com/",
    //       "path": "/product-page/t-shirt-1"
    //     },
    //     "numericId": "1567588455405000",
    //     "inventoryItemId": "1d71b222-912f-bf67-5a1a-30b4263b084a",
    //     "discount": {
    //       "type": "AMOUNT",
    //       "value": 1
    //     },
    //     "collectionIds": [
    //     ],
    //     "variants": [
    //       {
    //         "id": "00000000-0000-0001-0005-93fc95e0514a",
    //         "choices": {
    //           "Size": "S"
    //         },
    //         "variant": {
    //           "priceData": {
    //             "currency": "ILS",
    //             "price": 10.5,
    //             "discountedPrice": 9.5,
    //             "formatted": {
    //               "price": "10.50",
    //               "discountedPrice": "9.50"
    //             }
    //           },
    //           "weight": 10,
    //           "visible": true
    //         }
    //       },
    //       {
    //         "id": "00000000-0000-0002-0005-93fc95e0514a",
    //         "choices": {
    //           "Size": "L"
    //         },
    //         "variant": {
    //           "priceData": {
    //             "currency": "ILS",
    //             "price": 10.5,
    //             "discountedPrice": 9.5,
    //             "formatted": {
    //               "price": "10.50",
    //               "discountedPrice": "9.50"
    //             }
    //           },
    //           "visible": true
    //         }
    //       }
    //     ]
    //   }
    // }

    // Sample code for parsing the JSON response...
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    String optionType;
    String name;
    int j;
    int count_j;
    String value;
    String description;
    boolean inStock;
    boolean visible;
    String id;
    String choicesSize;
    String variantPriceDataCurrency;
    String variantPriceDataPrice;
    String variantPriceDataDiscountedPrice;
    String variantPriceDataFormattedPrice;
    String variantPriceDataFormattedDiscountedPrice;
    int variantWeight;
    boolean variantVisible;

    String productId = jResp.stringOf("product.id");
    String productName = jResp.stringOf("product.name");
    String productSlug = jResp.stringOf("product.slug");
    boolean productVisible = jResp.BoolOf("product.visible");
    String productProductType = jResp.stringOf("product.productType");
    String productDescription = jResp.stringOf("product.description");
    boolean productStockTrackInventory = jResp.BoolOf("product.stock.trackInventory");
    boolean productStockInStock = jResp.BoolOf("product.stock.inStock");
    String productPriceCurrency = jResp.stringOf("product.price.currency");
    String productPricePrice = jResp.stringOf("product.price.price");
    String productPriceDiscountedPrice = jResp.stringOf("product.price.discountedPrice");
    String productPriceFormattedPrice = jResp.stringOf("product.price.formatted.price");
    String productPriceFormattedDiscountedPrice = jResp.stringOf("product.price.formatted.discountedPrice");
    String productPriceDataCurrency = jResp.stringOf("product.priceData.currency");
    String productPriceDataPrice = jResp.stringOf("product.priceData.price");
    String productPriceDataDiscountedPrice = jResp.stringOf("product.priceData.discountedPrice");
    String productPriceDataFormattedPrice = jResp.stringOf("product.priceData.formatted.price");
    String productPriceDataFormattedDiscountedPrice = jResp.stringOf("product.priceData.formatted.discountedPrice");
    boolean productManageVariants = jResp.BoolOf("product.manageVariants");
    String productProductPageUrlBase = jResp.stringOf("product.productPageUrl.base");
    String productProductPageUrlPath = jResp.stringOf("product.productPageUrl.path");
    String productNumericId = jResp.stringOf("product.numericId");
    String productInventoryItemId = jResp.stringOf("product.inventoryItemId");
    String productDiscountType = jResp.stringOf("product.discount.type");
    int productDiscountValue = jResp.IntOf("product.discount.value");
    int i = 0;
    int count_i = jResp.SizeOfArray("product.additionalInfoSections");
    while (i < count_i) {
        jResp.put_I(i);
        i = i + 1;
        }

    i = 0;
    count_i = jResp.SizeOfArray("product.ribbons");
    while (i < count_i) {
        jResp.put_I(i);
        i = i + 1;
        }

    i = 0;
    count_i = jResp.SizeOfArray("product.media.items");
    while (i < count_i) {
        jResp.put_I(i);
        i = i + 1;
        }

    i = 0;
    count_i = jResp.SizeOfArray("product.customTextFields");
    while (i < count_i) {
        jResp.put_I(i);
        i = i + 1;
        }

    i = 0;
    count_i = jResp.SizeOfArray("product.productOptions");
    while (i < count_i) {
        jResp.put_I(i);
        optionType = jResp.stringOf("product.productOptions[i].optionType");
        name = jResp.stringOf("product.productOptions[i].name");
        j = 0;
        count_j = jResp.SizeOfArray("product.productOptions[i].choices");
        while (j < count_j) {
            jResp.put_J(j);
            value = jResp.stringOf("product.productOptions[i].choices[j].value");
            description = jResp.stringOf("product.productOptions[i].choices[j].description");
            inStock = jResp.BoolOf("product.productOptions[i].choices[j].inStock");
            visible = jResp.BoolOf("product.productOptions[i].choices[j].visible");
            j = j + 1;
            }

        i = i + 1;
        }

    i = 0;
    count_i = jResp.SizeOfArray("product.collectionIds");
    while (i < count_i) {
        jResp.put_I(i);
        i = i + 1;
        }

    i = 0;
    count_i = jResp.SizeOfArray("product.variants");
    while (i < count_i) {
        jResp.put_I(i);
        id = jResp.stringOf("product.variants[i].id");
        choicesSize = jResp.stringOf("product.variants[i].choices.Size");
        variantPriceDataCurrency = jResp.stringOf("product.variants[i].variant.priceData.currency");
        variantPriceDataPrice = jResp.stringOf("product.variants[i].variant.priceData.price");
        variantPriceDataDiscountedPrice = jResp.stringOf("product.variants[i].variant.priceData.discountedPrice");
        variantPriceDataFormattedPrice = jResp.stringOf("product.variants[i].variant.priceData.formatted.price");
        variantPriceDataFormattedDiscountedPrice = jResp.stringOf("product.variants[i].variant.priceData.formatted.discountedPrice");
        variantWeight = jResp.IntOf("product.variants[i].variant.weight");
        variantVisible = jResp.BoolOf("product.variants[i].variant.visible");
        i = i + 1;
        }


  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}