Cmdlets for WooCommerce

Build 20.0.7587

Establishing a Connection

With the CData Cmdlets users can install a data module, set the connection properties, and start scripting. This section provides examples of using our WooCommerce Cmdlets with native PowerShell cmdlets, like the CSV import and export cmdlets.

Installing and Connecting

If you have PSGet, installing the cmdlets can be accomplished from the PowerShell Gallery with the following command. You can also obtain a setup from the CData site.

Install-Module WooCommerceCmdlets

The following line is then added to your profile, loading the cmdlets on the next session:

Import-Module WooCommerceCmdlets;

You can then use the Connect-WooCommerce cmdlet to create a connection object that can be passed to other cmdlets:

$conn = Connect-WooCommerce -ConsumerKey 'ConsumerKey' -ConsumerSecret 'ConsumerSecret' -Url 'http://localhost/wordpress/'

Connecting to WooCommerce

WooCommerce supports the following authentication methods: Basic Authentication, one-legged OAuth1.0 Authentication and standard OAuth2.0 Authentication.

Additionally, in order to connect to WooCommerce, you also must specify the URL of the instance to connect to.

Connecting using Basic Authentication

Some plugins and some servers may interfere with the Authorization header, if not configured properly.

For these cases, Basic authentication may be used. In this authentication mode, the user provides the consumer_key and the consumer_secret as user and password respectively.

These credentials are then sent as query parameters with each request.

Specify the following properties:

  • User - Set this to your consumer key
  • Password - Set this to your consumer secret
  • Url - Set this to the base URL of the WooCommerce instance to connect to.

Connecting using one-legged OAuth 1.0 Authentication

Specify the following properties (NOTE: the below credentials are generated from WooCommerce settings page and should not be confused with the credentials generated by using WordPress OAuth2.0 plugin):

  • ConsumerKey
  • ConsumerSecret
  • Url - Set this to the base URL of the WooCommerce instance to connect to.

Connecting using WordPress OAuth 2.0 Authentication

After having configured the OAuth2 Plugin plugin, you may connect to WooCommerce by providing the following connection proeprties:

  • OAuthClientId
  • OAuthClientSecret
  • CallbackURL
  • InitiateOAuth - Set this to either GETANDREFRESH or REFRESH
  • Url - Set this to the base URL of the WooCommerce instance to connect to.

In addition to the above connection parameters, you can optionally set the AuthScheme parameter to force the preferred authentication scheme.

  • AuthScheme - Set this to either Basic, OAuth or OAuthOneLegged, depending on your preferred authentication method.

Retrieving Data

The Select-WooCommerce cmdlet provides a native PowerShell interface for retrieving data:

$results = Select-WooCommerce -Connection $conn -Table "Orders" -Columns @("ParentId, Total") -Where "ParentId='3'"
The Invoke-WooCommerce cmdlet provides an SQL interface. This cmdlet can be used to execute an SQL query via the Query parameter.

Piping Cmdlet Output

The cmdlets return row objects to the pipeline one row at a time. The following line exports results to a CSV file:

Select-WooCommerce -Connection $conn -Table Orders -Where "ParentId = '3'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myOrdersData.csv -NoTypeInformation

You will notice that we piped the results from Select-WooCommerce into a Select-Object cmdlet and excluded some properties before piping them into an Export-CSV cmdlet. We do this because the CData Cmdlets append Connection, Table, and Columns information onto each row object in the result set, and we do not necessarily want that information in our CSV file.

However, this makes it easy to pipe the output of one cmdlet to another. The following is an example of converting a result set to JSON:

 
PS C:\> $conn  = Connect-WooCommerce -ConsumerKey 'ConsumerKey' -ConsumerSecret 'ConsumerSecret' -Url 'http://localhost/wordpress/'
PS C:\> $row = Select-WooCommerce -Connection $conn -Table "Orders" -Columns (ParentId, Total) -Where "ParentId = '3'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  "Orders",
  "Columns":  [

  ],
  "ParentId":  "MyParentId",
  "Total":  "MyTotal"
} 

Deleting Data

The following line deletes any records that match the criteria:

Select-WooCommerce -Connection $conn -Table Orders -Where "ParentId = '3'" | Remove-WooCommerce

Updating Data

The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into WooCommerce, checking first whether a record already exists and needs to be updated instead of inserted.

Import-Csv -Path C:\MyOrdersUpdates.csv | %{
  $record = Select-WooCommerce -Connection $conn -Table Orders -Where ("Id = `'"+$_.Id+"`'")
  if($record){
    Update-WooCommerce -Connection $conn -Table Orders -Columns @("ParentId","Total") -Values @($_.ParentId, $_.Total) -Where "Id  = `'$_.Id`'"
  }else{
    Add-WooCommerce -Connection $conn -Table Orders -Columns @("ParentId","Total") -Values @($_.ParentId, $_.Total)
  }
}

Copyright (c) 2020 CData Software, Inc. - All rights reserved.
Build 20.0.7587