Sample code for 30+ languages & platforms
React Native

WooCommerce Retrieve a Product

See more WooCommerce Examples

Demonstrates how to get the information for a product in JSON format.

Chilkat React Native Downloads

React Native
import { DtObj, Http, JsonObject, StringBuilder } from '@chilkat/react-native'

async function chilkatExample() {
  // This example assumes the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const http = new Http();

  // Implements the following CURL command:

  // curl https://example.com/wp-json/wc/v3/products/794 \
  //     -u consumer_key:consumer_secret

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

  http.basicAuth = true;
  http.login = 'consumer_key';
  http.password = 'consumer_secret';

  const sbResponseBody = new StringBuilder();
  try {
    await http.quickGetSbAsync('https://example.com/wp-json/wc/v3/products/794', sbResponseBody);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  const jResp = new JsonObject();
  jResp.loadSb(sbResponseBody);
  jResp.emitCompact = false;

  console.log('Response Body:');
  console.log(jResp.emit());

  const respStatusCode = http.lastStatus;
  console.log(`Response Status Code = ${respStatusCode}`);
  if (respStatusCode >= 400) {
    console.log('Response Header:');
    console.log(http.lastHeader);
    console.log('Failed.');
    return;
  }

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

  // {
  //   "id": 794,
  //   "name": "Premium Quality",
  //   "slug": "premium-quality-19",
  //   "permalink": "https://example.com/product/premium-quality-19/",
  //   "date_created": "2017-03-23T17:01:14",
  //   "date_created_gmt": "2017-03-23T20:01:14",
  //   "date_modified": "2017-03-23T17:01:14",
  //   "date_modified_gmt": "2017-03-23T20:01:14",
  //   "type": "simple",
  //   "status": "publish",
  //   "featured": false,
  //   "catalog_visibility": "visible",
  //   "description": "<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>\n",
  //   "short_description": "<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>\n",
  //   "sku": "",
  //   "price": "21.99",
  //   "regular_price": "21.99",
  //   "sale_price": "",
  //   "date_on_sale_from": null,
  //   "date_on_sale_from_gmt": null,
  //   "date_on_sale_to": null,
  //   "date_on_sale_to_gmt": null,
  //   "price_html": "<span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">&#36;</span>21.99</span>",
  //   "on_sale": false,
  //   "purchasable": true,
  //   "total_sales": 0,
  //   "virtual": false,
  //   "downloadable": false,
  //   "downloads": [
  //   ],
  //   "download_limit": -1,
  //   "download_expiry": -1,
  //   "external_url": "",
  //   "button_text": "",
  //   "tax_status": "taxable",
  //   "tax_class": "",
  //   "manage_stock": false,
  //   "stock_quantity": null,
  //   "stock_status": "instock",
  //   "backorders": "no",
  //   "backorders_allowed": false,
  //   "backordered": false,
  //   "sold_individually": false,
  //   "weight": "",
  //   "dimensions": {
  //     "length": "",
  //     "width": "",
  //     "height": ""
  //   },
  //   "shipping_required": true,
  //   "shipping_taxable": true,
  //   "shipping_class": "",
  //   "shipping_class_id": 0,
  //   "reviews_allowed": true,
  //   "average_rating": "0.00",
  //   "rating_count": 0,
  //   "related_ids": [
  //     53,
  //     40,
  //     56,
  //     479,
  //     99
  //   ],
  //   "upsell_ids": [
  //   ],
  //   "cross_sell_ids": [
  //   ],
  //   "parent_id": 0,
  //   "purchase_note": "",
  //   "categories": [
  //     {
  //       "id": 9,
  //       "name": "Clothing",
  //       "slug": "clothing"
  //     },
  //     {
  //       "id": 14,
  //       "name": "T-shirts",
  //       "slug": "t-shirts"
  //     }
  //   ],
  //   "tags": [
  //   ],
  //   "images": [
  //     {
  //       "id": 792,
  //       "date_created": "2017-03-23T14:01:13",
  //       "date_created_gmt": "2017-03-23T20:01:13",
  //       "date_modified": "2017-03-23T14:01:13",
  //       "date_modified_gmt": "2017-03-23T20:01:13",
  //       "src": "https://example.com/wp-content/uploads/2017/03/T_2_front-4.jpg",
  //       "name": "",
  //       "alt": ""
  //     },
  //     {
  //       "id": 793,
  //       "date_created": "2017-03-23T14:01:14",
  //       "date_created_gmt": "2017-03-23T20:01:14",
  //       "date_modified": "2017-03-23T14:01:14",
  //       "date_modified_gmt": "2017-03-23T20:01:14",
  //       "src": "https://example.com/wp-content/uploads/2017/03/T_2_back-2.jpg",
  //       "name": "",
  //       "alt": ""
  //     }
  //   ],
  //   "attributes": [
  //   ],
  //   "default_attributes": [
  //   ],
  //   "variations": [
  //   ],
  //   "grouped_products": [
  //   ],
  //   "menu_order": 0,
  //   "meta_data": [
  //   ],
  //   "_links": {
  //     "self": [
  //       {
  //         "href": "https://example.com/wp-json/wc/v3/products/794"
  //       }
  //     ],
  //     "collection": [
  //       {
  //         "href": "https://example.com/wp-json/wc/v3/products"
  //       }
  //     ]
  //   }
  // }

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

  const dateCreated = new DtObj();
  const dateCreatedGmt = new DtObj();
  const dateModified = new DtObj();
  const dateModifiedGmt = new DtObj();
  const dateOnSaleFrom = new DtObj();
  const dateOnSaleFromGmt = new DtObj();
  const dateOnSaleTo = new DtObj();
  const dateOnSaleToGmt = new DtObj();
  let intVal = 0;
  let src = '';
  let alt = '';
  let href = '';

  let id = jResp.intOf('id');
  let name = jResp.stringOf('name');
  let slug = jResp.stringOf('slug');
  const permalink = jResp.stringOf('permalink');
  jResp.dtOf('date_created', false, dateCreated);
  jResp.dtOf('date_created_gmt', false, dateCreatedGmt);
  jResp.dtOf('date_modified', false, dateModified);
  jResp.dtOf('date_modified_gmt', false, dateModifiedGmt);
  const vType = jResp.stringOf('type');
  const status = jResp.stringOf('status');
  const catalogVisibility = jResp.stringOf('catalog_visibility');
  const description = jResp.stringOf('description');
  const shortDescription = jResp.stringOf('short_description');
  const sku = jResp.stringOf('sku');
  const price = jResp.stringOf('price');
  const regularPrice = jResp.stringOf('regular_price');
  const salePrice = jResp.stringOf('sale_price');
  jResp.dtOf('date_on_sale_from', false, dateOnSaleFrom);
  jResp.dtOf('date_on_sale_from_gmt', false, dateOnSaleFromGmt);
  jResp.dtOf('date_on_sale_to', false, dateOnSaleTo);
  jResp.dtOf('date_on_sale_to_gmt', false, dateOnSaleToGmt);
  const priceHtml = jResp.stringOf('price_html');
  const totalSales = jResp.intOf('total_sales');
  const downloadLimit = jResp.intOf('download_limit');
  const downloadExpiry = jResp.intOf('download_expiry');
  const externalUrl = jResp.stringOf('external_url');
  const buttonText = jResp.stringOf('button_text');
  const taxStatus = jResp.stringOf('tax_status');
  const taxClass = jResp.stringOf('tax_class');
  const stockQuantity = jResp.stringOf('stock_quantity');
  const stockStatus = jResp.stringOf('stock_status');
  const backorders = jResp.stringOf('backorders');
  const weight = jResp.stringOf('weight');
  const dimensionsLength = jResp.stringOf('dimensions.length');
  const dimensionsWidth = jResp.stringOf('dimensions.width');
  const dimensionsHeight = jResp.stringOf('dimensions.height');
  const shippingClass = jResp.stringOf('shipping_class');
  const shippingClassId = jResp.intOf('shipping_class_id');
  const averageRating = jResp.stringOf('average_rating');
  const ratingCount = jResp.intOf('rating_count');
  const parentId = jResp.intOf('parent_id');
  const purchaseNote = jResp.stringOf('purchase_note');
  const menuOrder = jResp.intOf('menu_order');
  let i = 0;
  let countI = jResp.sizeOfArray('downloads');
  while (i < countI) {
    jResp.i = i;
    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('related_ids');
  while (i < countI) {
    jResp.i = i;
    intVal = jResp.intOf('related_ids[i]');
    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('upsell_ids');
  while (i < countI) {
    jResp.i = i;
    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('cross_sell_ids');
  while (i < countI) {
    jResp.i = i;
    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('categories');
  while (i < countI) {
    jResp.i = i;
    id = jResp.intOf('categories[i].id');
    name = jResp.stringOf('categories[i].name');
    slug = jResp.stringOf('categories[i].slug');
    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('tags');
  while (i < countI) {
    jResp.i = i;
    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('images');
  while (i < countI) {
    jResp.i = i;
    id = jResp.intOf('images[i].id');
    jResp.dtOf('images[i].date_created', false, dateCreated);
    jResp.dtOf('images[i].date_created_gmt', false, dateCreatedGmt);
    jResp.dtOf('images[i].date_modified', false, dateModified);
    jResp.dtOf('images[i].date_modified_gmt', false, dateModifiedGmt);
    src = jResp.stringOf('images[i].src');
    name = jResp.stringOf('images[i].name');
    alt = jResp.stringOf('images[i].alt');
    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('attributes');
  while (i < countI) {
    jResp.i = i;
    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('default_attributes');
  while (i < countI) {
    jResp.i = i;
    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('variations');
  while (i < countI) {
    jResp.i = i;
    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('grouped_products');
  while (i < countI) {
    jResp.i = i;
    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('meta_data');
  while (i < countI) {
    jResp.i = i;
    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('_links.self');
  while (i < countI) {
    jResp.i = i;
    href = jResp.stringOf('_links.self[i].href');
    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('_links.collection');
  while (i < countI) {
    jResp.i = i;
    href = jResp.stringOf('_links.collection[i].href');
    i++;
  }
}