WooCommerce / REST API

Discover how to easily use Wordpress API to connect to ours

1/ Before you begin

Some prerequisites

2/Connect API's with PHP

Samples for both API's

3/Integration Logic

Some basic functions you can reuse

4/PHP Code

A demo of what can be achieved

Integration Logic

Read (GET), Create (POST) and Update (PUT) Functions

We wrote some basic functions with PHP CURL to call the GET, POST and PUT functions of the REST API.

For the pure GET function, we use the easy “file_get_contents” PHP functions. It works fine. Here is an example of the create (POST) function (Legacy V3):

function createInWordpress($url, $object_type, $data) {
    $jsonData[$object_type] = $data;
    $jsonDataEncoded = json_encode($jsonData);

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    $result = curl_exec($ch);
    curl_close($ch);

    $jsonDataDecoded = json_decode($result, true);

    return $jsonDataDecoded;
}

The update function (PUT) is similar (V2):

function updateInWordpress($url, $data) {
    $jsonDataEncoded = json_encode($data);

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    $result = curl_exec($ch); curl_close($ch);

    $jsonDataDecoded = json_decode($result, true);

    return $jsonDataDecoded;
}

We then wrote some functions to retrieve a category or a tag from WooCommerce and if it doesn’t exist, to create it, by combining the functions here above.

Product and Variants Import

We looked for a solution that would allow us to fully automate the product import process.

A comment on the pictures though.

The standard behaviour of WooCommerce is to allow multiple pictures on the product but only 1 picture per variation. We were able to import pictures on the product by using a URL. But importing the picture on the variation must be done by using the internal id of the picture.

For this reason, we decided to split the import process in 2 processes: 1 for products and variants import and 1 for picture import.

The products and variants import process is the following:

  1. In the product import REST API, WooCommerce requires to use internal id’s for the categories and the tags. So, we use the REST API to first get the existing categories and tags and their internal id’s. If a new category or a new tag is required, we create it on the fly, during the import process, and keep its internal id in memory.

  2. We then call the ST/ST API to retrieve the product catalogue, grouped by style and sorted.

  3. For each style, we first ask WooCommerce if it already exists in order to determine if we do a create or an update (with internal id). We do this via a GET with a filter on the SKU with the style code.

  4. Depending on create or update, we create or update the WooCommerce JSON data structure. We use the style code as SKU for the product.

  5. As mentioned, we do not import pictures at this stage.

  6. At the product level, the API requires to already list the values of the color and size options. These values will be created on the fly during the import process. But they must also be listed at product level.

  7. We then loop on all the variants of a style and we map the variant related data (like price, stock, color code and size code). We use the full B2B code (style code + color code + size code) for the SKU of a variant.

  8. Once the structure is filled, we call the POST (create) or PUT (update) method of WooCommerce for the product object.

As already mentioned, it is possible to import pictures by URL on the product but not on the variants. On the variant, we can only import 1 picture and by internal id.

Therefore, in the pictures import script, we first import all pictures on the product and store the internal id’s. For each variant, we then select the main picture (SFM*), get its internal id and send this to Wordpress.