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

WooCommerce List Products having SKU

See more WooCommerce Examples

Gets information for a product by SKU.

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.JsonArray;

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

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  sbResponseBody: TStringBuilder;
  jarrResp: TJsonArray;
  respStatusCode: 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 https://example.com/wp-json/wc/v3/products?sku=3386460107914 \
  //      -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';

  sbResponseBody := TStringBuilder.Create;
  http.SetUrlVar('sku','3386460107914');
  //  Use "https" or "http" depending on what your site needs.
  success := http.QuickGetSb('http://example.com/wp-json/wc/v3/products?sku={$sku}',sbResponseBody);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  jarrResp := TJsonArray.Create;
  jarrResp.LoadSb(sbResponseBody);
  jarrResp.EmitCompact := False;

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

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


  http.Free;
  sbResponseBody.Free;
  jarrResp.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.