Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

WooCommerce Delete a Product

See more WooCommerce Examples

Demonstrates how to delete a product from WooCommerce.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.StringBuilder,
  Chilkat.DtObj,
  Chilkat.HttpResponse,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  resp: THttpResponse;
  sbResponseBody: TStringBuilder;
  jResp: TJsonObject;
  respStatusCode: Integer;
  date_created: TDtObj;
  date_created_gmt: TDtObj;
  date_modified: TDtObj;
  date_modified_gmt: TDtObj;
  date_on_sale_from: TDtObj;
  date_on_sale_from_gmt: TDtObj;
  date_on_sale_to: TDtObj;
  date_on_sale_to_gmt: TDtObj;
  intVal: Integer;
  src: string;
  alt: string;
  href: string;
  id: Integer;
  name: string;
  slug: string;
  permalink: string;
  v_type: string;
  status: string;
  featured: Boolean;
  catalog_visibility: string;
  description: string;
  short_description: string;
  sku: string;
  price: string;
  regular_price: string;
  sale_price: string;
  price_html: string;
  on_sale: Boolean;
  purchasable: Boolean;
  total_sales: Integer;
  virtual: Boolean;
  downloadable: Boolean;
  download_limit: Integer;
  download_expiry: Integer;
  external_url: string;
  button_text: string;
  tax_status: string;
  tax_class: string;
  manage_stock: Boolean;
  stock_quantity: string;
  stock_status: string;
  backorders: string;
  backorders_allowed: Boolean;
  backordered: Boolean;
  sold_individually: Boolean;
  weight: string;
  dimensionsLength: string;
  dimensionsWidth: string;
  dimensionsHeight: string;
  shipping_required: Boolean;
  shipping_taxable: Boolean;
  shipping_class: string;
  shipping_class_id: Integer;
  reviews_allowed: Boolean;
  average_rating: string;
  rating_count: Integer;
  parent_id: Integer;
  purchase_note: string;
  menu_order: Integer;
  i: Integer;
  count_i: Integer;

begin
  success := False;

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

  http := THttp.Create;

  //  Implements the following CURL command:

  //  curl -X DELETE https://example.com/wp-json/wc/v3/products/794?force=true \
  //      -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';

  resp := THttpResponse.Create;
  success := http.HttpNoBody('DELETE','https://example.com/wp-json/wc/v3/products/794?force=true',resp);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  sbResponseBody := TStringBuilder.Create;
  resp.GetBodySb(sbResponseBody);
  jResp := TJsonObject.Create;
  jResp.LoadSb(sbResponseBody);
  jResp.EmitCompact := False;

  WriteLn('Response Body:');
  WriteLn(jResp.Emit());

  respStatusCode := resp.StatusCode;
  WriteLn('Response Status Code = ' + respStatusCode);
  if (respStatusCode >= 400) then
    begin
      WriteLn('Response Header:');
      WriteLn(resp.Header);
      WriteLn('Failed.');
      Exit;
    end;

  //  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": "24.54",
  //    "regular_price": "24.54",
  //    "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>24.54</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": [
  //      479,
  //      387,
  //      22,
  //      463,
  //      396
  //    ],
  //    "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

  date_created := TDtObj.Create;
  date_created_gmt := TDtObj.Create;
  date_modified := TDtObj.Create;
  date_modified_gmt := TDtObj.Create;
  date_on_sale_from := TDtObj.Create;
  date_on_sale_from_gmt := TDtObj.Create;
  date_on_sale_to := TDtObj.Create;
  date_on_sale_to_gmt := TDtObj.Create;

  id := jResp.IntOf('id');
  name := jResp.StringOf('name');
  slug := jResp.StringOf('slug');
  permalink := jResp.StringOf('permalink');
  jResp.DtOf('date_created',False,date_created);
  jResp.DtOf('date_created_gmt',False,date_created_gmt);
  jResp.DtOf('date_modified',False,date_modified);
  jResp.DtOf('date_modified_gmt',False,date_modified_gmt);
  v_type := jResp.StringOf('type');
  status := jResp.StringOf('status');
  featured := jResp.BoolOf('featured');
  catalog_visibility := jResp.StringOf('catalog_visibility');
  description := jResp.StringOf('description');
  short_description := jResp.StringOf('short_description');
  sku := jResp.StringOf('sku');
  price := jResp.StringOf('price');
  regular_price := jResp.StringOf('regular_price');
  sale_price := jResp.StringOf('sale_price');
  jResp.DtOf('date_on_sale_from',False,date_on_sale_from);
  jResp.DtOf('date_on_sale_from_gmt',False,date_on_sale_from_gmt);
  jResp.DtOf('date_on_sale_to',False,date_on_sale_to);
  jResp.DtOf('date_on_sale_to_gmt',False,date_on_sale_to_gmt);
  price_html := jResp.StringOf('price_html');
  on_sale := jResp.BoolOf('on_sale');
  purchasable := jResp.BoolOf('purchasable');
  total_sales := jResp.IntOf('total_sales');
  virtual := jResp.BoolOf('virtual');
  downloadable := jResp.BoolOf('downloadable');
  download_limit := jResp.IntOf('download_limit');
  download_expiry := jResp.IntOf('download_expiry');
  external_url := jResp.StringOf('external_url');
  button_text := jResp.StringOf('button_text');
  tax_status := jResp.StringOf('tax_status');
  tax_class := jResp.StringOf('tax_class');
  manage_stock := jResp.BoolOf('manage_stock');
  stock_quantity := jResp.StringOf('stock_quantity');
  stock_status := jResp.StringOf('stock_status');
  backorders := jResp.StringOf('backorders');
  backorders_allowed := jResp.BoolOf('backorders_allowed');
  backordered := jResp.BoolOf('backordered');
  sold_individually := jResp.BoolOf('sold_individually');
  weight := jResp.StringOf('weight');
  dimensionsLength := jResp.StringOf('dimensions.length');
  dimensionsWidth := jResp.StringOf('dimensions.width');
  dimensionsHeight := jResp.StringOf('dimensions.height');
  shipping_required := jResp.BoolOf('shipping_required');
  shipping_taxable := jResp.BoolOf('shipping_taxable');
  shipping_class := jResp.StringOf('shipping_class');
  shipping_class_id := jResp.IntOf('shipping_class_id');
  reviews_allowed := jResp.BoolOf('reviews_allowed');
  average_rating := jResp.StringOf('average_rating');
  rating_count := jResp.IntOf('rating_count');
  parent_id := jResp.IntOf('parent_id');
  purchase_note := jResp.StringOf('purchase_note');
  menu_order := jResp.IntOf('menu_order');
  i := 0;
  count_i := jResp.SizeOfArray('downloads');
  while i < count_i do
    begin
      jResp.I := i;
      i := i + 1;
    end;

  i := 0;
  count_i := jResp.SizeOfArray('related_ids');
  while i < count_i do
    begin
      jResp.I := i;
      intVal := jResp.IntOf('related_ids[i]');
      i := i + 1;
    end;

  i := 0;
  count_i := jResp.SizeOfArray('upsell_ids');
  while i < count_i do
    begin
      jResp.I := i;
      i := i + 1;
    end;

  i := 0;
  count_i := jResp.SizeOfArray('cross_sell_ids');
  while i < count_i do
    begin
      jResp.I := i;
      i := i + 1;
    end;

  i := 0;
  count_i := jResp.SizeOfArray('categories');
  while i < count_i do
    begin
      jResp.I := i;
      id := jResp.IntOf('categories[i].id');
      name := jResp.StringOf('categories[i].name');
      slug := jResp.StringOf('categories[i].slug');
      i := i + 1;
    end;

  i := 0;
  count_i := jResp.SizeOfArray('tags');
  while i < count_i do
    begin
      jResp.I := i;
      i := i + 1;
    end;

  i := 0;
  count_i := jResp.SizeOfArray('images');
  while i < count_i do
    begin
      jResp.I := i;
      id := jResp.IntOf('images[i].id');
      jResp.DtOf('images[i].date_created',False,date_created);
      jResp.DtOf('images[i].date_created_gmt',False,date_created_gmt);
      jResp.DtOf('images[i].date_modified',False,date_modified);
      jResp.DtOf('images[i].date_modified_gmt',False,date_modified_gmt);
      src := jResp.StringOf('images[i].src');
      name := jResp.StringOf('images[i].name');
      alt := jResp.StringOf('images[i].alt');
      i := i + 1;
    end;

  i := 0;
  count_i := jResp.SizeOfArray('attributes');
  while i < count_i do
    begin
      jResp.I := i;
      i := i + 1;
    end;

  i := 0;
  count_i := jResp.SizeOfArray('default_attributes');
  while i < count_i do
    begin
      jResp.I := i;
      i := i + 1;
    end;

  i := 0;
  count_i := jResp.SizeOfArray('variations');
  while i < count_i do
    begin
      jResp.I := i;
      i := i + 1;
    end;

  i := 0;
  count_i := jResp.SizeOfArray('grouped_products');
  while i < count_i do
    begin
      jResp.I := i;
      i := i + 1;
    end;

  i := 0;
  count_i := jResp.SizeOfArray('meta_data');
  while i < count_i do
    begin
      jResp.I := i;
      i := i + 1;
    end;

  i := 0;
  count_i := jResp.SizeOfArray('_links.self');
  while i < count_i do
    begin
      jResp.I := i;
      href := jResp.StringOf('_links.self[i].href');
      i := i + 1;
    end;

  i := 0;
  count_i := jResp.SizeOfArray('_links.collection');
  while i < count_i do
    begin
      jResp.I := i;
      href := jResp.StringOf('_links.collection[i].href');
      i := i + 1;
    end;



  http.Free;
  resp.Free;
  sbResponseBody.Free;
  jResp.Free;
  date_created.Free;
  date_created_gmt.Free;
  date_modified.Free;
  date_modified_gmt.Free;
  date_on_sale_from.Free;
  date_on_sale_from_gmt.Free;
  date_on_sale_to.Free;
  date_on_sale_to_gmt.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.