Sample code for 30+ languages & platforms
Rust

WooCommerce List All Products

See more WooCommerce Examples

Gets WooCommerce product information in JSON format.

Chilkat Rust Downloads

Rust

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

let http = chilkat::Http::new();

// 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.set_basic_auth(true);
http.set_login("consumer_key");
http.set_password("consumer_secret");

let sb_response_body = chilkat::StringBuilder::new();
if http.quick_get_sb("https://example.com/wp-json/wc/v3/products", &sb_response_body).is_err() {
    println!("{}", http.last_error_text());
    return;
}

let jarr_resp = chilkat::JsonArray::new();
let _ = jarr_resp.load_sb(&sb_response_body);
jarr_resp.set_emit_compact(false);

println!("Response Body:");
println!("{}", jarr_resp.emit().unwrap_or_default());

let resp_status_code = http.last_status();
println!("Response Status Code = {}", resp_status_code);
if resp_status_code >= 400 {
    println!("Response Header:");
    println!("{}", http.last_header());
    println!("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 = chilkat::DtObj::new();
let date_created_gmt = chilkat::DtObj::new();
let date_modified = chilkat::DtObj::new();
let date_modified_gmt = chilkat::DtObj::new();
let date_on_sale_from = chilkat::DtObj::new();
let date_on_sale_from_gmt = chilkat::DtObj::new();
let date_on_sale_to = chilkat::DtObj::new();
let date_on_sale_to_gmt = chilkat::DtObj::new();
let mut j: i32 = 0;
let mut count_j: i32 = 0;
let mut k: i32 = 0;
let mut count_k: i32 = 0;

let mut i = 0;
let count_i = jarr_resp.size();
while i < count_i {
    let json = jarr_resp.object_at(i).unwrap();
    let _ = json.int_of("id");
    let _ = json.string_of("name").unwrap_or_default();
    let _ = json.string_of("slug").unwrap_or_default();
    let _ = json.string_of("permalink").unwrap_or_default();
    let _ = json.dt_of("date_created", false, &date_created);
    let _ = json.dt_of("date_created_gmt", false, &date_created_gmt);
    let _ = json.dt_of("date_modified", false, &date_modified);
    let _ = json.dt_of("date_modified_gmt", false, &date_modified_gmt);
    let _ = json.string_of("type").unwrap_or_default();
    let _ = json.string_of("status").unwrap_or_default();
    let _ = json.bool_of("featured");
    let _ = json.string_of("catalog_visibility").unwrap_or_default();
    let _ = json.string_of("description").unwrap_or_default();
    let _ = json.string_of("short_description").unwrap_or_default();
    let _ = json.string_of("sku").unwrap_or_default();
    let _ = json.string_of("price").unwrap_or_default();
    let _ = json.string_of("regular_price").unwrap_or_default();
    let _ = json.string_of("sale_price").unwrap_or_default();
    let _ = json.dt_of("date_on_sale_from", false, &date_on_sale_from);
    let _ = json.dt_of("date_on_sale_from_gmt", false, &date_on_sale_from_gmt);
    let _ = json.dt_of("date_on_sale_to", false, &date_on_sale_to);
    let _ = json.dt_of("date_on_sale_to_gmt", false, &date_on_sale_to_gmt);
    let _ = json.string_of("price_html").unwrap_or_default();
    let _ = json.bool_of("on_sale");
    let _ = json.bool_of("purchasable");
    let _ = json.int_of("total_sales");
    let _ = json.bool_of("virtual");
    let _ = json.bool_of("downloadable");
    let _ = json.int_of("download_limit");
    let _ = json.int_of("download_expiry");
    let _ = json.string_of("external_url").unwrap_or_default();
    let _ = json.string_of("button_text").unwrap_or_default();
    let _ = json.string_of("tax_status").unwrap_or_default();
    let _ = json.string_of("tax_class").unwrap_or_default();
    let _ = json.bool_of("manage_stock");
    let _ = json.string_of("stock_quantity").unwrap_or_default();
    let _ = json.string_of("stock_status").unwrap_or_default();
    let _ = json.string_of("backorders").unwrap_or_default();
    let _ = json.bool_of("backorders_allowed");
    let _ = json.bool_of("backordered");
    let _ = json.bool_of("sold_individually");
    let _ = json.string_of("weight").unwrap_or_default();
    let _ = json.string_of("dimensions.length").unwrap_or_default();
    let _ = json.string_of("dimensions.width").unwrap_or_default();
    let _ = json.string_of("dimensions.height").unwrap_or_default();
    let _ = json.bool_of("shipping_required");
    let _ = json.bool_of("shipping_taxable");
    let _ = json.string_of("shipping_class").unwrap_or_default();
    let _ = json.int_of("shipping_class_id");
    let _ = json.bool_of("reviews_allowed");
    let _ = json.string_of("average_rating").unwrap_or_default();
    let _ = json.int_of("rating_count");
    let _ = json.int_of("parent_id");
    let _ = json.string_of("purchase_note").unwrap_or_default();
    let _ = json.int_of("menu_order");
    j = 0;
    count_j = json.size_of_array("downloads");
    while j < count_j {
        json.set_j(j);
        j = j + 1;
    }

    j = 0;
    count_j = json.size_of_array("related_ids");
    while j < count_j {
        json.set_j(j);
        let _ = json.int_of("related_ids[j]");
        j = j + 1;
    }

    j = 0;
    count_j = json.size_of_array("upsell_ids");
    while j < count_j {
        json.set_j(j);
        j = j + 1;
    }

    j = 0;
    count_j = json.size_of_array("cross_sell_ids");
    while j < count_j {
        json.set_j(j);
        j = j + 1;
    }

    j = 0;
    count_j = json.size_of_array("categories");
    while j < count_j {
        json.set_j(j);
        let _ = json.int_of("categories[j].id");
        let _ = json.string_of("categories[j].name").unwrap_or_default();
        let _ = json.string_of("categories[j].slug").unwrap_or_default();
        j = j + 1;
    }

    j = 0;
    count_j = json.size_of_array("tags");
    while j < count_j {
        json.set_j(j);
        j = j + 1;
    }

    j = 0;
    count_j = json.size_of_array("images");
    while j < count_j {
        json.set_j(j);
        let _ = json.int_of("images[j].id");
        let _ = json.dt_of("images[j].date_created", false, &date_created);
        let _ = json.dt_of("images[j].date_created_gmt", false, &date_created_gmt);
        let _ = json.dt_of("images[j].date_modified", false, &date_modified);
        let _ = json.dt_of("images[j].date_modified_gmt", false, &date_modified_gmt);
        let _ = json.string_of("images[j].src").unwrap_or_default();
        let _ = json.string_of("images[j].name").unwrap_or_default();
        let _ = json.string_of("images[j].alt").unwrap_or_default();
        j = j + 1;
    }

    j = 0;
    count_j = json.size_of_array("attributes");
    while j < count_j {
        json.set_j(j);
        let _ = json.int_of("attributes[j].id");
        let _ = json.string_of("attributes[j].name").unwrap_or_default();
        let _ = json.int_of("attributes[j].position");
        let _ = json.bool_of("attributes[j].visible");
        let _ = json.bool_of("attributes[j].variation");
        k = 0;
        count_k = json.size_of_array("attributes[j].options");
        while k < count_k {
            json.set_k(k);
            let _ = json.string_of("attributes[j].options[k]").unwrap_or_default();
            k = k + 1;
        }

        j = j + 1;
    }

    j = 0;
    count_j = json.size_of_array("default_attributes");
    while j < count_j {
        json.set_j(j);
        let _ = json.int_of("default_attributes[j].id");
        let _ = json.string_of("default_attributes[j].name").unwrap_or_default();
        let _ = json.string_of("default_attributes[j].option").unwrap_or_default();
        j = j + 1;
    }

    j = 0;
    count_j = json.size_of_array("variations");
    while j < count_j {
        json.set_j(j);
        j = j + 1;
    }

    j = 0;
    count_j = json.size_of_array("grouped_products");
    while j < count_j {
        json.set_j(j);
        j = j + 1;
    }

    j = 0;
    count_j = json.size_of_array("meta_data");
    while j < count_j {
        json.set_j(j);
        j = j + 1;
    }

    j = 0;
    count_j = json.size_of_array("_links.self");
    while j < count_j {
        json.set_j(j);
        let _ = json.string_of("_links.self[j].href").unwrap_or_default();
        j = j + 1;
    }

    j = 0;
    count_j = json.size_of_array("_links.collection");
    while j < count_j {
        json.set_j(j);
        let _ = json.string_of("_links.collection[j].href").unwrap_or_default();
        j = j + 1;
    }

    i = i + 1;
}