Sample code for 30+ languages & platforms
Swift

WooCommerce List All Products

See more WooCommerce Examples

Gets WooCommerce product information in JSON format.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let http = CkoHttp()!

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

    let sbResponseBody = CkoStringBuilder()!
    success = http.quickGetSb(url: "https://example.com/wp-json/wc/v3/products", sbContent: sbResponseBody)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    let jarrResp = CkoJsonArray()!
    jarrResp.loadSb(sb: sbResponseBody)
    jarrResp.emitCompact = false

    print("Response Body:")
    print("\(jarrResp.emit()!)")

    var respStatusCode: Int = http.lastStatus.intValue
    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

    let date_created = CkoDtObj()!
    let date_created_gmt = CkoDtObj()!
    let date_modified = CkoDtObj()!
    let date_modified_gmt = CkoDtObj()!
    let date_on_sale_from = CkoDtObj()!
    let date_on_sale_from_gmt = CkoDtObj()!
    let date_on_sale_to = CkoDtObj()!
    let date_on_sale_to_gmt = CkoDtObj()!
    var json: CkoJsonObject?
    var id: Int
    var name: String?
    var slug: String?
    var permalink: String?
    var v_type: String?
    var status: String?
    var featured: Bool
    var catalog_visibility: String?
    var description: String?
    var short_description: String?
    var sku: String?
    var price: String?
    var regular_price: String?
    var sale_price: String?
    var price_html: String?
    var on_sale: Bool
    var purchasable: Bool
    var total_sales: Int
    var v_virtual: Bool
    var downloadable: Bool
    var download_limit: Int
    var download_expiry: Int
    var external_url: String?
    var button_text: String?
    var tax_status: String?
    var tax_class: String?
    var manage_stock: Bool
    var stock_quantity: String?
    var stock_status: String?
    var backorders: String?
    var backorders_allowed: Bool
    var backordered: Bool
    var sold_individually: Bool
    var weight: String?
    var dimensionsLength: String?
    var dimensionsWidth: String?
    var dimensionsHeight: String?
    var shipping_required: Bool
    var shipping_taxable: Bool
    var shipping_class: String?
    var shipping_class_id: Int
    var reviews_allowed: Bool
    var average_rating: String?
    var rating_count: Int
    var parent_id: Int
    var purchase_note: String?
    var menu_order: Int
    var j: Int
    var count_j: Int
    var intVal: Int
    var src: String?
    var alt: String?
    var position: Int
    var visible: Bool
    var variation: Bool
    var k: Int
    var count_k: Int
    var strVal: String?
    var v_option: String?
    var href: String?

    var i: Int = 0
    var count_i: Int = jarrResp.size.intValue
    while i < count_i {
        json = jarrResp.object(at: i)
        id = json!.int(of: "id").intValue
        name = json!.string(of: "name")
        slug = json!.string(of: "slug")
        permalink = json!.string(of: "permalink")
        json!.dt(of: "date_created", bLocal: false, dt: date_created)
        json!.dt(of: "date_created_gmt", bLocal: false, dt: date_created_gmt)
        json!.dt(of: "date_modified", bLocal: false, dt: date_modified)
        json!.dt(of: "date_modified_gmt", bLocal: false, dt: date_modified_gmt)
        v_type = json!.string(of: "type")
        status = json!.string(of: "status")
        featured = json!.bool(of: "featured")
        catalog_visibility = json!.string(of: "catalog_visibility")
        description = json!.string(of: "description")
        short_description = json!.string(of: "short_description")
        sku = json!.string(of: "sku")
        price = json!.string(of: "price")
        regular_price = json!.string(of: "regular_price")
        sale_price = json!.string(of: "sale_price")
        json!.dt(of: "date_on_sale_from", bLocal: false, dt: date_on_sale_from)
        json!.dt(of: "date_on_sale_from_gmt", bLocal: false, dt: date_on_sale_from_gmt)
        json!.dt(of: "date_on_sale_to", bLocal: false, dt: date_on_sale_to)
        json!.dt(of: "date_on_sale_to_gmt", bLocal: false, dt: date_on_sale_to_gmt)
        price_html = json!.string(of: "price_html")
        on_sale = json!.bool(of: "on_sale")
        purchasable = json!.bool(of: "purchasable")
        total_sales = json!.int(of: "total_sales").intValue
        v_virtual = json!.bool(of: "virtual")
        downloadable = json!.bool(of: "downloadable")
        download_limit = json!.int(of: "download_limit").intValue
        download_expiry = json!.int(of: "download_expiry").intValue
        external_url = json!.string(of: "external_url")
        button_text = json!.string(of: "button_text")
        tax_status = json!.string(of: "tax_status")
        tax_class = json!.string(of: "tax_class")
        manage_stock = json!.bool(of: "manage_stock")
        stock_quantity = json!.string(of: "stock_quantity")
        stock_status = json!.string(of: "stock_status")
        backorders = json!.string(of: "backorders")
        backorders_allowed = json!.bool(of: "backorders_allowed")
        backordered = json!.bool(of: "backordered")
        sold_individually = json!.bool(of: "sold_individually")
        weight = json!.string(of: "weight")
        dimensionsLength = json!.string(of: "dimensions.length")
        dimensionsWidth = json!.string(of: "dimensions.width")
        dimensionsHeight = json!.string(of: "dimensions.height")
        shipping_required = json!.bool(of: "shipping_required")
        shipping_taxable = json!.bool(of: "shipping_taxable")
        shipping_class = json!.string(of: "shipping_class")
        shipping_class_id = json!.int(of: "shipping_class_id").intValue
        reviews_allowed = json!.bool(of: "reviews_allowed")
        average_rating = json!.string(of: "average_rating")
        rating_count = json!.int(of: "rating_count").intValue
        parent_id = json!.int(of: "parent_id").intValue
        purchase_note = json!.string(of: "purchase_note")
        menu_order = json!.int(of: "menu_order").intValue
        j = 0
        count_j = json!.size(ofArray: "downloads").intValue
        while j < count_j {
            json.j = j
            j = j + 1
        }

        j = 0
        count_j = json!.size(ofArray: "related_ids").intValue
        while j < count_j {
            json.j = j
            intVal = json!.int(of: "related_ids[j]").intValue
            j = j + 1
        }

        j = 0
        count_j = json!.size(ofArray: "upsell_ids").intValue
        while j < count_j {
            json.j = j
            j = j + 1
        }

        j = 0
        count_j = json!.size(ofArray: "cross_sell_ids").intValue
        while j < count_j {
            json.j = j
            j = j + 1
        }

        j = 0
        count_j = json!.size(ofArray: "categories").intValue
        while j < count_j {
            json.j = j
            id = json!.int(of: "categories[j].id").intValue
            name = json!.string(of: "categories[j].name")
            slug = json!.string(of: "categories[j].slug")
            j = j + 1
        }

        j = 0
        count_j = json!.size(ofArray: "tags").intValue
        while j < count_j {
            json.j = j
            j = j + 1
        }

        j = 0
        count_j = json!.size(ofArray: "images").intValue
        while j < count_j {
            json.j = j
            id = json!.int(of: "images[j].id").intValue
            json!.dt(of: "images[j].date_created", bLocal: false, dt: date_created)
            json!.dt(of: "images[j].date_created_gmt", bLocal: false, dt: date_created_gmt)
            json!.dt(of: "images[j].date_modified", bLocal: false, dt: date_modified)
            json!.dt(of: "images[j].date_modified_gmt", bLocal: false, dt: date_modified_gmt)
            src = json!.string(of: "images[j].src")
            name = json!.string(of: "images[j].name")
            alt = json!.string(of: "images[j].alt")
            j = j + 1
        }

        j = 0
        count_j = json!.size(ofArray: "attributes").intValue
        while j < count_j {
            json.j = j
            id = json!.int(of: "attributes[j].id").intValue
            name = json!.string(of: "attributes[j].name")
            position = json!.int(of: "attributes[j].position").intValue
            visible = json!.bool(of: "attributes[j].visible")
            variation = json!.bool(of: "attributes[j].variation")
            k = 0
            count_k = json!.size(ofArray: "attributes[j].options").intValue
            while k < count_k {
                json.k = k
                strVal = json!.string(of: "attributes[j].options[k]")
                k = k + 1
            }

            j = j + 1
        }

        j = 0
        count_j = json!.size(ofArray: "default_attributes").intValue
        while j < count_j {
            json.j = j
            id = json!.int(of: "default_attributes[j].id").intValue
            name = json!.string(of: "default_attributes[j].name")
            v_option = json!.string(of: "default_attributes[j].option")
            j = j + 1
        }

        j = 0
        count_j = json!.size(ofArray: "variations").intValue
        while j < count_j {
            json.j = j
            j = j + 1
        }

        j = 0
        count_j = json!.size(ofArray: "grouped_products").intValue
        while j < count_j {
            json.j = j
            j = j + 1
        }

        j = 0
        count_j = json!.size(ofArray: "meta_data").intValue
        while j < count_j {
            json.j = j
            j = j + 1
        }

        j = 0
        count_j = json!.size(ofArray: "_links.self").intValue
        while j < count_j {
            json.j = j
            href = json!.string(of: "_links.self[j].href")
            j = j + 1
        }

        j = 0
        count_j = json!.size(ofArray: "_links.collection").intValue
        while j < count_j {
            json.j = j
            href = json!.string(of: "_links.collection[j].href")
            j = j + 1
        }

        json = nil
        i = i + 1
    }


}