Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) WooCommerce Create a ProductSee more WooCommerce ExamplesDemonstrates how to create a new product in WooCommerce. For more information, see https://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-product
#include <C_CkHttpW.h> #include <C_CkJsonObjectW.h> #include <C_CkHttpResponseW.h> #include <C_CkStringBuilderW.h> #include <C_CkDtObjW.h> void ChilkatSample(void) { HCkHttpW http; BOOL success; HCkJsonObjectW json; HCkHttpResponseW resp; HCkStringBuilderW sbResponseBody; HCkJsonObjectW jResp; int respStatusCode; HCkDtObjW date_created; HCkDtObjW date_created_gmt; HCkDtObjW date_modified; HCkDtObjW date_modified_gmt; HCkDtObjW date_on_sale_from; HCkDtObjW date_on_sale_from_gmt; HCkDtObjW date_on_sale_to; HCkDtObjW date_on_sale_to_gmt; int intVal; const wchar_t *src; const wchar_t *alt; const wchar_t *href; int id; const wchar_t *name; const wchar_t *slug; const wchar_t *permalink; const wchar_t *v_type; const wchar_t *status; BOOL featured; const wchar_t *catalog_visibility; const wchar_t *description; const wchar_t *short_description; const wchar_t *sku; const wchar_t *price; const wchar_t *regular_price; const wchar_t *sale_price; const wchar_t *price_html; BOOL on_sale; BOOL purchasable; int total_sales; BOOL virtual; BOOL downloadable; int download_limit; int download_expiry; const wchar_t *external_url; const wchar_t *button_text; const wchar_t *tax_status; const wchar_t *tax_class; BOOL manage_stock; const wchar_t *stock_quantity; const wchar_t *stock_status; const wchar_t *backorders; BOOL backorders_allowed; BOOL backordered; BOOL sold_individually; const wchar_t *weight; const wchar_t *dimensionsLength; const wchar_t *dimensionsWidth; const wchar_t *dimensionsHeight; BOOL shipping_required; BOOL shipping_taxable; const wchar_t *shipping_class; int shipping_class_id; BOOL reviews_allowed; const wchar_t *average_rating; int rating_count; int parent_id; const wchar_t *purchase_note; int menu_order; int i; int count_i; // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. http = CkHttpW_Create(); // 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 CkHttpW_putBasicAuth(http,TRUE); CkHttpW_putLogin(http,L"consumer_key"); CkHttpW_putPassword(http,L"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" // } // ] // } json = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(json,L"name",L"Premium Quality"); CkJsonObjectW_UpdateString(json,L"type",L"simple"); CkJsonObjectW_UpdateString(json,L"regular_price",L"21.99"); CkJsonObjectW_UpdateString(json,L"description",L"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."); CkJsonObjectW_UpdateString(json,L"short_description",L"Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas."); CkJsonObjectW_UpdateInt(json,L"categories[0].id",9); CkJsonObjectW_UpdateInt(json,L"categories[1].id",14); CkJsonObjectW_UpdateString(json,L"images[0].src",L"http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg"); CkJsonObjectW_UpdateString(json,L"images[1].src",L"http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg"); CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json"); resp = CkHttpW_PostJson3(http,L"https://example.com/wp-json/wc/v3/products",L"application/json",json); if (CkHttpW_getLastMethodSuccess(http) == FALSE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); return; } sbResponseBody = CkStringBuilderW_Create(); CkHttpResponseW_GetBodySb(resp,sbResponseBody); jResp = CkJsonObjectW_Create(); CkJsonObjectW_LoadSb(jResp,sbResponseBody); CkJsonObjectW_putEmitCompact(jResp,FALSE); wprintf(L"Response Body:\n"); wprintf(L"%s\n",CkJsonObjectW_emit(jResp)); respStatusCode = CkHttpResponseW_getStatusCode(resp); wprintf(L"Response Status Code = %d\n",respStatusCode); if (respStatusCode >= 400) { wprintf(L"Response Header:\n"); wprintf(L"%s\n",CkHttpResponseW_header(resp)); wprintf(L"Failed.\n"); CkHttpResponseW_Dispose(resp); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); return; } CkHttpResponseW_Dispose(resp); // 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 // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat. // See this example explaining how this memory should be used: const char * functions. date_created = CkDtObjW_Create(); date_created_gmt = CkDtObjW_Create(); date_modified = CkDtObjW_Create(); date_modified_gmt = CkDtObjW_Create(); date_on_sale_from = CkDtObjW_Create(); date_on_sale_from_gmt = CkDtObjW_Create(); date_on_sale_to = CkDtObjW_Create(); date_on_sale_to_gmt = CkDtObjW_Create(); id = CkJsonObjectW_IntOf(jResp,L"id"); name = CkJsonObjectW_stringOf(jResp,L"name"); slug = CkJsonObjectW_stringOf(jResp,L"slug"); permalink = CkJsonObjectW_stringOf(jResp,L"permalink"); CkJsonObjectW_DtOf(jResp,L"date_created",FALSE,date_created); CkJsonObjectW_DtOf(jResp,L"date_created_gmt",FALSE,date_created_gmt); CkJsonObjectW_DtOf(jResp,L"date_modified",FALSE,date_modified); CkJsonObjectW_DtOf(jResp,L"date_modified_gmt",FALSE,date_modified_gmt); v_type = CkJsonObjectW_stringOf(jResp,L"type"); status = CkJsonObjectW_stringOf(jResp,L"status"); featured = CkJsonObjectW_BoolOf(jResp,L"featured"); catalog_visibility = CkJsonObjectW_stringOf(jResp,L"catalog_visibility"); description = CkJsonObjectW_stringOf(jResp,L"description"); short_description = CkJsonObjectW_stringOf(jResp,L"short_description"); sku = CkJsonObjectW_stringOf(jResp,L"sku"); price = CkJsonObjectW_stringOf(jResp,L"price"); regular_price = CkJsonObjectW_stringOf(jResp,L"regular_price"); sale_price = CkJsonObjectW_stringOf(jResp,L"sale_price"); CkJsonObjectW_DtOf(jResp,L"date_on_sale_from",FALSE,date_on_sale_from); CkJsonObjectW_DtOf(jResp,L"date_on_sale_from_gmt",FALSE,date_on_sale_from_gmt); CkJsonObjectW_DtOf(jResp,L"date_on_sale_to",FALSE,date_on_sale_to); CkJsonObjectW_DtOf(jResp,L"date_on_sale_to_gmt",FALSE,date_on_sale_to_gmt); price_html = CkJsonObjectW_stringOf(jResp,L"price_html"); on_sale = CkJsonObjectW_BoolOf(jResp,L"on_sale"); purchasable = CkJsonObjectW_BoolOf(jResp,L"purchasable"); total_sales = CkJsonObjectW_IntOf(jResp,L"total_sales"); virtual = CkJsonObjectW_BoolOf(jResp,L"virtual"); downloadable = CkJsonObjectW_BoolOf(jResp,L"downloadable"); download_limit = CkJsonObjectW_IntOf(jResp,L"download_limit"); download_expiry = CkJsonObjectW_IntOf(jResp,L"download_expiry"); external_url = CkJsonObjectW_stringOf(jResp,L"external_url"); button_text = CkJsonObjectW_stringOf(jResp,L"button_text"); tax_status = CkJsonObjectW_stringOf(jResp,L"tax_status"); tax_class = CkJsonObjectW_stringOf(jResp,L"tax_class"); manage_stock = CkJsonObjectW_BoolOf(jResp,L"manage_stock"); stock_quantity = CkJsonObjectW_stringOf(jResp,L"stock_quantity"); stock_status = CkJsonObjectW_stringOf(jResp,L"stock_status"); backorders = CkJsonObjectW_stringOf(jResp,L"backorders"); backorders_allowed = CkJsonObjectW_BoolOf(jResp,L"backorders_allowed"); backordered = CkJsonObjectW_BoolOf(jResp,L"backordered"); sold_individually = CkJsonObjectW_BoolOf(jResp,L"sold_individually"); weight = CkJsonObjectW_stringOf(jResp,L"weight"); dimensionsLength = CkJsonObjectW_stringOf(jResp,L"dimensions.length"); dimensionsWidth = CkJsonObjectW_stringOf(jResp,L"dimensions.width"); dimensionsHeight = CkJsonObjectW_stringOf(jResp,L"dimensions.height"); shipping_required = CkJsonObjectW_BoolOf(jResp,L"shipping_required"); shipping_taxable = CkJsonObjectW_BoolOf(jResp,L"shipping_taxable"); shipping_class = CkJsonObjectW_stringOf(jResp,L"shipping_class"); shipping_class_id = CkJsonObjectW_IntOf(jResp,L"shipping_class_id"); reviews_allowed = CkJsonObjectW_BoolOf(jResp,L"reviews_allowed"); average_rating = CkJsonObjectW_stringOf(jResp,L"average_rating"); rating_count = CkJsonObjectW_IntOf(jResp,L"rating_count"); parent_id = CkJsonObjectW_IntOf(jResp,L"parent_id"); purchase_note = CkJsonObjectW_stringOf(jResp,L"purchase_note"); menu_order = CkJsonObjectW_IntOf(jResp,L"menu_order"); i = 0; count_i = CkJsonObjectW_SizeOfArray(jResp,L"downloads"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(jResp,L"related_ids"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); intVal = CkJsonObjectW_IntOf(jResp,L"related_ids[i]"); i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(jResp,L"upsell_ids"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(jResp,L"cross_sell_ids"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(jResp,L"categories"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); id = CkJsonObjectW_IntOf(jResp,L"categories[i].id"); name = CkJsonObjectW_stringOf(jResp,L"categories[i].name"); slug = CkJsonObjectW_stringOf(jResp,L"categories[i].slug"); i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(jResp,L"tags"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(jResp,L"images"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); id = CkJsonObjectW_IntOf(jResp,L"images[i].id"); CkJsonObjectW_DtOf(jResp,L"images[i].date_created",FALSE,date_created); CkJsonObjectW_DtOf(jResp,L"images[i].date_created_gmt",FALSE,date_created_gmt); CkJsonObjectW_DtOf(jResp,L"images[i].date_modified",FALSE,date_modified); CkJsonObjectW_DtOf(jResp,L"images[i].date_modified_gmt",FALSE,date_modified_gmt); src = CkJsonObjectW_stringOf(jResp,L"images[i].src"); name = CkJsonObjectW_stringOf(jResp,L"images[i].name"); alt = CkJsonObjectW_stringOf(jResp,L"images[i].alt"); i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(jResp,L"attributes"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(jResp,L"default_attributes"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(jResp,L"variations"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(jResp,L"grouped_products"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(jResp,L"meta_data"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(jResp,L"_links.self"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); href = CkJsonObjectW_stringOf(jResp,L"_links.self[i].href"); i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(jResp,L"_links.collection"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); href = CkJsonObjectW_stringOf(jResp,L"_links.collection[i].href"); i = i + 1; } CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); CkDtObjW_Dispose(date_created); CkDtObjW_Dispose(date_created_gmt); CkDtObjW_Dispose(date_modified); CkDtObjW_Dispose(date_modified_gmt); CkDtObjW_Dispose(date_on_sale_from); CkDtObjW_Dispose(date_on_sale_from_gmt); CkDtObjW_Dispose(date_on_sale_to); CkDtObjW_Dispose(date_on_sale_to_gmt); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.