Rust
Rust
WooCommerce Create a Product
See more WooCommerce Examples
Demonstrates how to create a new product in WooCommerce.Chilkat Rust Downloads
// 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 -X POST https://example.com/wp-json/wc/v3/products \
// -u consumer_key:consumer_secret \
// -H "Content-Type: application/json" \
// -d '{
// "name": "Premium Quality",
// "type": "simple",
// "regular_price": "21.99",
// "description": "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.",
// "short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
// "categories": [
// {
// "id": 9
// },
// {
// "id": 14
// }
// ],
// "images": [
// {
// "src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg"
// },
// {
// "src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg"
// }
// ]
// }'
// 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");
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "name": "Premium Quality",
// "type": "simple",
// "regular_price": "21.99",
// "description": "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.",
// "short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
// "categories": [
// {
// "id": 9
// },
// {
// "id": 14
// }
// ],
// "images": [
// {
// "src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg"
// },
// {
// "src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg"
// }
// ]
// }
let json = chilkat::JsonObject::new();
let _ = json.update_string("name", "Premium Quality");
let _ = json.update_string("type", "simple");
let _ = json.update_string("regular_price", "21.99");
let _ = json.update_string("description", "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.");
let _ = json.update_string("short_description", "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.");
let _ = json.update_int("categories[0].id", 9);
let _ = json.update_int("categories[1].id", 14);
let _ = json.update_string("images[0].src", "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg");
let _ = json.update_string("images[1].src", "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg");
let resp = chilkat::HttpResponse::new();
if http.http_json("POST", "https://example.com/wp-json/wc/v3/products", &json, "application/json", &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
let sb_response_body = chilkat::StringBuilder::new();
let _ = resp.get_body_sb(&sb_response_body);
let j_resp = chilkat::JsonObject::new();
let _ = j_resp.load_sb(&sb_response_body);
j_resp.set_emit_compact(false);
println!("Response Body:");
println!("{}", j_resp.emit().unwrap_or_default());
let resp_status_code = resp.status_code();
println!("Response Status Code = {}", resp_status_code);
if resp_status_code >= 400 {
println!("Response Header:");
println!("{}", resp.header());
println!("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\">$</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
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 id = j_resp.int_of("id");
let mut name = j_resp.string_of("name").unwrap_or_default();
let mut slug = j_resp.string_of("slug").unwrap_or_default();
let permalink = j_resp.string_of("permalink").unwrap_or_default();
let _ = j_resp.dt_of("date_created", false, &date_created);
let _ = j_resp.dt_of("date_created_gmt", false, &date_created_gmt);
let _ = j_resp.dt_of("date_modified", false, &date_modified);
let _ = j_resp.dt_of("date_modified_gmt", false, &date_modified_gmt);
let v_type = j_resp.string_of("type").unwrap_or_default();
let status = j_resp.string_of("status").unwrap_or_default();
let featured = j_resp.bool_of("featured");
let catalog_visibility = j_resp.string_of("catalog_visibility").unwrap_or_default();
let description = j_resp.string_of("description").unwrap_or_default();
let short_description = j_resp.string_of("short_description").unwrap_or_default();
let sku = j_resp.string_of("sku").unwrap_or_default();
let price = j_resp.string_of("price").unwrap_or_default();
let regular_price = j_resp.string_of("regular_price").unwrap_or_default();
let sale_price = j_resp.string_of("sale_price").unwrap_or_default();
let _ = j_resp.dt_of("date_on_sale_from", false, &date_on_sale_from);
let _ = j_resp.dt_of("date_on_sale_from_gmt", false, &date_on_sale_from_gmt);
let _ = j_resp.dt_of("date_on_sale_to", false, &date_on_sale_to);
let _ = j_resp.dt_of("date_on_sale_to_gmt", false, &date_on_sale_to_gmt);
let price_html = j_resp.string_of("price_html").unwrap_or_default();
let on_sale = j_resp.bool_of("on_sale");
let purchasable = j_resp.bool_of("purchasable");
let total_sales = j_resp.int_of("total_sales");
let virtual_ = j_resp.bool_of("virtual");
let downloadable = j_resp.bool_of("downloadable");
let download_limit = j_resp.int_of("download_limit");
let download_expiry = j_resp.int_of("download_expiry");
let external_url = j_resp.string_of("external_url").unwrap_or_default();
let button_text = j_resp.string_of("button_text").unwrap_or_default();
let tax_status = j_resp.string_of("tax_status").unwrap_or_default();
let tax_class = j_resp.string_of("tax_class").unwrap_or_default();
let manage_stock = j_resp.bool_of("manage_stock");
let stock_quantity = j_resp.string_of("stock_quantity").unwrap_or_default();
let stock_status = j_resp.string_of("stock_status").unwrap_or_default();
let backorders = j_resp.string_of("backorders").unwrap_or_default();
let backorders_allowed = j_resp.bool_of("backorders_allowed");
let backordered = j_resp.bool_of("backordered");
let sold_individually = j_resp.bool_of("sold_individually");
let weight = j_resp.string_of("weight").unwrap_or_default();
let dimensions_length = j_resp.string_of("dimensions.length").unwrap_or_default();
let dimensions_width = j_resp.string_of("dimensions.width").unwrap_or_default();
let dimensions_height = j_resp.string_of("dimensions.height").unwrap_or_default();
let shipping_required = j_resp.bool_of("shipping_required");
let shipping_taxable = j_resp.bool_of("shipping_taxable");
let shipping_class = j_resp.string_of("shipping_class").unwrap_or_default();
let shipping_class_id = j_resp.int_of("shipping_class_id");
let reviews_allowed = j_resp.bool_of("reviews_allowed");
let average_rating = j_resp.string_of("average_rating").unwrap_or_default();
let rating_count = j_resp.int_of("rating_count");
let parent_id = j_resp.int_of("parent_id");
let purchase_note = j_resp.string_of("purchase_note").unwrap_or_default();
let menu_order = j_resp.int_of("menu_order");
let mut i = 0;
let mut count_i = j_resp.size_of_array("downloads");
while i < count_i {
j_resp.set_i(i);
i = i + 1;
}
i = 0;
count_i = j_resp.size_of_array("related_ids");
while i < count_i {
j_resp.set_i(i);
let _ = j_resp.int_of("related_ids[i]");
i = i + 1;
}
i = 0;
count_i = j_resp.size_of_array("upsell_ids");
while i < count_i {
j_resp.set_i(i);
i = i + 1;
}
i = 0;
count_i = j_resp.size_of_array("cross_sell_ids");
while i < count_i {
j_resp.set_i(i);
i = i + 1;
}
i = 0;
count_i = j_resp.size_of_array("categories");
while i < count_i {
j_resp.set_i(i);
id = j_resp.int_of("categories[i].id");
name = j_resp.string_of("categories[i].name").unwrap_or_default();
slug = j_resp.string_of("categories[i].slug").unwrap_or_default();
i = i + 1;
}
i = 0;
count_i = j_resp.size_of_array("tags");
while i < count_i {
j_resp.set_i(i);
i = i + 1;
}
i = 0;
count_i = j_resp.size_of_array("images");
while i < count_i {
j_resp.set_i(i);
id = j_resp.int_of("images[i].id");
let _ = j_resp.dt_of("images[i].date_created", false, &date_created);
let _ = j_resp.dt_of("images[i].date_created_gmt", false, &date_created_gmt);
let _ = j_resp.dt_of("images[i].date_modified", false, &date_modified);
let _ = j_resp.dt_of("images[i].date_modified_gmt", false, &date_modified_gmt);
let _ = j_resp.string_of("images[i].src").unwrap_or_default();
name = j_resp.string_of("images[i].name").unwrap_or_default();
let _ = j_resp.string_of("images[i].alt").unwrap_or_default();
i = i + 1;
}
i = 0;
count_i = j_resp.size_of_array("attributes");
while i < count_i {
j_resp.set_i(i);
i = i + 1;
}
i = 0;
count_i = j_resp.size_of_array("default_attributes");
while i < count_i {
j_resp.set_i(i);
i = i + 1;
}
i = 0;
count_i = j_resp.size_of_array("variations");
while i < count_i {
j_resp.set_i(i);
i = i + 1;
}
i = 0;
count_i = j_resp.size_of_array("grouped_products");
while i < count_i {
j_resp.set_i(i);
i = i + 1;
}
i = 0;
count_i = j_resp.size_of_array("meta_data");
while i < count_i {
j_resp.set_i(i);
i = i + 1;
}
i = 0;
count_i = j_resp.size_of_array("_links.self");
while i < count_i {
j_resp.set_i(i);
let _ = j_resp.string_of("_links.self[i].href").unwrap_or_default();
i = i + 1;
}
i = 0;
count_i = j_resp.size_of_array("_links.collection");
while i < count_i {
j_resp.set_i(i);
let _ = j_resp.string_of("_links.collection[i].href").unwrap_or_default();
i = i + 1;
}