Sample code for 30+ languages & platforms
Dart

WooCommerce List All Products

See more WooCommerce Examples

Gets WooCommerce product information in JSON format.

Chilkat Dart Downloads

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

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

  final http = CkHttp();

  // Implements the following CURL command:

  // curl https://example.com/wp-json/wc/v3/products \
  //     -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';

  final sbResponseBody = CkStringBuilder();
  try {
    http.quickGetSb('https://example.com/wp-json/wc/v3/products', sbResponseBody);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final jarrResp = CkJsonArray();
  jarrResp.loadSb(sbResponseBody);
  jarrResp.emitCompact = false;

  print('Response Body:');
  print(jarrResp.emit());

  final respStatusCode = http.lastStatus;
  print('Response Status Code = $respStatusCode');
  if (respStatusCode >= 400) {
    print('Response Header:');
    print(http.lastHeader);
    print('Failed.');
    return;
  }

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

  // [
  //   {
  //     "id": 799,
  //     "name": "Ship Your Idea",
  //     "slug": "ship-your-idea-22",
  //     "permalink": "https://example.com/product/ship-your-idea-22/",
  //     "date_created": "2017-03-23T17:03:12",
  //     "date_created_gmt": "2017-03-23T20:03:12",
  //     "date_modified": "2017-03-23T17:03:12",
  //     "date_modified_gmt": "2017-03-23T20:03:12",
  //     "type": "variable",
  //     "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": "",
  //     "regular_price": "",
  //     "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": "",
  //     "on_sale": false,
  //     "purchasable": false,
  //     "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": [
  //       31,
  //       22,
  //       369,
  //       414,
  //       56
  //     ],
  //     "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": 795,
  //         "date_created": "2017-03-23T14:03:08",
  //         "date_created_gmt": "2017-03-23T20:03:08",
  //         "date_modified": "2017-03-23T14:03:08",
  //         "date_modified_gmt": "2017-03-23T20:03:08",
  //         "src": "https://example.com/wp-content/uploads/2017/03/T_4_front-11.jpg",
  //         "name": "",
  //         "alt": ""
  //       },
  //       {
  //         "id": 796,
  //         "date_created": "2017-03-23T14:03:09",
  //         "date_created_gmt": "2017-03-23T20:03:09",
  //         "date_modified": "2017-03-23T14:03:09",
  //         "date_modified_gmt": "2017-03-23T20:03:09",
  //         "src": "https://example.com/wp-content/uploads/2017/03/T_4_back-10.jpg",
  //         "name": "",
  //         "alt": ""
  //       },
  //       {
  //         "id": 797,
  //         "date_created": "2017-03-23T14:03:10",
  //         "date_created_gmt": "2017-03-23T20:03:10",
  //         "date_modified": "2017-03-23T14:03:10",
  //         "date_modified_gmt": "2017-03-23T20:03:10",
  //         "src": "https://example.com/wp-content/uploads/2017/03/T_3_front-10.jpg",
  //         "name": "",
  //         "alt": ""
  //       },
  //       {
  //         "id": 798,
  //         "date_created": "2017-03-23T14:03:11",
  //         "date_created_gmt": "2017-03-23T20:03:11",
  //         "date_modified": "2017-03-23T14:03:11",
  //         "date_modified_gmt": "2017-03-23T20:03:11",
  //         "src": "https://example.com/wp-content/uploads/2017/03/T_3_back-10.jpg",
  //         "name": "",
  //         "alt": ""
  //       }
  //     ],
  //     "attributes": [
  //       {
  //         "id": 6,
  //         "name": "Color",
  //         "position": 0,
  //         "visible": false,
  //         "variation": true,
  //         "options": [
  //           "Black",
  //           "Green"
  //         ]
  //       },
  //       {
  //         "id": 0,
  //         "name": "Size",
  //         "position": 0,
  //         "visible": true,
  //         "variation": true,
  //         "options": [
  //           "S",
  //           "M"
  //         ]
  //       }
  //     ],
  //     "default_attributes": [
  //     ],
  //     "variations": [
  //     ],
  //     "grouped_products": [
  //     ],
  //     "menu_order": 0,
  //     "meta_data": [
  //     ],
  //     "_links": {
  //       "self": [
  //         {
  //           "href": "https://example.com/wp-json/wc/v3/products/799"
  //         }
  //       ],
  //       "collection": [
  //         {
  //           "href": "https://example.com/wp-json/wc/v3/products"
  //         }
  //       ]
  //     }
  //   },
  //   {
  //     "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": [
  //       463,
  //       47,
  //       31,
  //       387,
  //       458
  //     ],
  //     "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": [
  //       {
  //         "id": 6,
  //         "name": "Color",
  //         "option": "black"
  //       },
  //       {
  //         "id": 0,
  //         "name": "Size",
  //         "option": "S"
  //       }
  //     ],
  //     "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

  final dateCreated = CkDtObj();
  final dateCreatedGmt = CkDtObj();
  final dateModified = CkDtObj();
  final dateModifiedGmt = CkDtObj();
  final dateOnSaleFrom = CkDtObj();
  final dateOnSaleFromGmt = CkDtObj();
  final dateOnSaleTo = CkDtObj();
  final dateOnSaleToGmt = CkDtObj();
  var id = 0;
  var name = '';
  var slug = '';
  var permalink = '';
  var vType = '';
  var status = '';
  var catalogVisibility = '';
  var description = '';
  var shortDescription = '';
  var sku = '';
  var price = '';
  var regularPrice = '';
  var salePrice = '';
  var priceHtml = '';
  var totalSales = 0;
  var downloadLimit = 0;
  var downloadExpiry = 0;
  var externalUrl = '';
  var buttonText = '';
  var taxStatus = '';
  var taxClass = '';
  var stockQuantity = '';
  var stockStatus = '';
  var backorders = '';
  var weight = '';
  var dimensionsLength = '';
  var dimensionsWidth = '';
  var dimensionsHeight = '';
  var shippingClass = '';
  var shippingClassId = 0;
  var averageRating = '';
  var ratingCount = 0;
  var parentId = 0;
  var purchaseNote = '';
  var menuOrder = 0;
  var j = 0;
  var countJ = 0;
  var intVal = 0;
  var src = '';
  var alt = '';
  var position = 0;
  var k = 0;
  var countK = 0;
  var strVal = '';
  var vOption = '';
  var href = '';

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

    j = 0;
    countJ = json.sizeOfArray('related_ids');
    while (j < countJ) {
      json.j = j;
      intVal = json.intOf('related_ids[j]');
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('upsell_ids');
    while (j < countJ) {
      json.j = j;
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('cross_sell_ids');
    while (j < countJ) {
      json.j = j;
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('categories');
    while (j < countJ) {
      json.j = j;
      id = json.intOf('categories[j].id');
      name = json.stringOf('categories[j].name');
      slug = json.stringOf('categories[j].slug');
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('tags');
    while (j < countJ) {
      json.j = j;
      j++;
    }

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

    j = 0;
    countJ = json.sizeOfArray('attributes');
    while (j < countJ) {
      json.j = j;
      id = json.intOf('attributes[j].id');
      name = json.stringOf('attributes[j].name');
      position = json.intOf('attributes[j].position');
      json.boolOf('attributes[j].visible');
      json.boolOf('attributes[j].variation');
      k = 0;
      countK = json.sizeOfArray('attributes[j].options');
      while (k < countK) {
        json.k = k;
        strVal = json.stringOf('attributes[j].options[k]');
        k++;
      }

      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('default_attributes');
    while (j < countJ) {
      json.j = j;
      id = json.intOf('default_attributes[j].id');
      name = json.stringOf('default_attributes[j].name');
      vOption = json.stringOf('default_attributes[j].option');
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('variations');
    while (j < countJ) {
      json.j = j;
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('grouped_products');
    while (j < countJ) {
      json.j = j;
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('meta_data');
    while (j < countJ) {
      json.j = j;
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('_links.self');
    while (j < countJ) {
      json.j = j;
      href = json.stringOf('_links.self[j].href');
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('_links.collection');
    while (j < countJ) {
      json.j = j;
      href = json.stringOf('_links.collection[j].href');
      j++;
    }

    i++;
  }
}