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

Connect API's with PHP

Connect to Stanley/Stella Product API in PHP

The first step of the integration is to call the Stanley/Stella Product REST API from the PHP code.

The standard output of the Stanley/Stella API is a “flat” list of product variants (SKU’s).

In our PHP code, we added the grouping of the variants at style level and the sorting of the variants by colors and sizes.

For simplicity, we only download the product data in 1 language (English).

See the following PHP code as inspiration. Feel free to extend this code with additional data coming from the API.

<?php
function getAllSTSTProducts() {
    $url = "https://api.stanleystella.com/webrequest/products/get_json";
    $jsonData = array(
        'jsonrpc' => '2.0',
        'method' => 'call',
        'params' => array(
            'db_name' => "production_api",
            'password' => "YOUR_PASSWORD",
            'user' => "YOUR_LOGIN",
            "LanguageCode" => "en_US"
        ),
        'id' => 0
    );
    $ch = curl_init($url);
    $jsonDataEncoded = json_encode($jsonData);
    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);
    
    $jsonDataDecoded = json_decode(json_decode($result)->result, true);
    curl_close($ch);
    
    // Sort the data by style, color and size
    foreach ($jsonDataDecoded as $key => $row) {
        $styleorder[$key] = $row['SequenceStyle'];
        $sizeorder[$key] = $row['SizeSequence'];
        $colororder[$key] = $row['ColorSequence'];
    }
    array_multisort($styleorder, SORT_ASC, $colororder, SORT_ASC, $sizeorder, SORT_ASC, $jsonDataDecoded);

    return $jsonDataDecoded;
}

function getAllSTSTProductsGrouped() {
    $stpm = getAllSTSTProducts();
    
    $allstststyles = array();
    foreach ($stpm as $key => $variant) {
        $stylecode = $variant["StyleCode"];
        if (!isset($allstststyles[$stylecode])) {
            $allstststyles[$stylecode] = array();
            $allstststyles[$stylecode]["StyleCode"] = $variant["StyleCode"];
            $allstststyles[$stylecode]["StyleName"] = $variant["StyleName"];
            $allstststyles[$stylecode]["Type"] = $variant["Type"];
            $allstststyles[$stylecode]["Category"] = $variant["Category"];
            $allstststyles[$stylecode]["Gender"] = $variant["Gender"];
            $allstststyles[$stylecode]["Fit"] = $variant["Fit"];
            $allstststyles[$stylecode]["Neckline"] = $variant["Neckline"];
            $allstststyles[$stylecode]["Sleeve"] = $variant["Sleeve"];
            $allstststyles[$stylecode]["ShortDescription"] = $variant["ShortDescription"];
            $allstststyles[$stylecode]["LongDescription"] = $variant["LongDescription"];
            $allstststyles[$stylecode]["SequenceStyle"] = $variant["SequenceStyle"];
        }
    
        if (!isset($allstststyles[$stylecode]["variants"])) $allstststyles[$stylecode]["variants"] = array();
            $var = array();
            $var["B2BSKUREF"] = $variant["B2BSKUREF"];
            $var["ColorCode"] = $variant["ColorCode"];
            $var["Color"] = $variant["Color"];
            $var["ColorSequence"] = $variant["ColorSequence"];
            $var["SizeCode"] = $variant["SizeCode"];
            $var["SizeCodeNavision"] = $variant["SizeCodeNavision"];
            $var["SizeSequence"] = $variant["SizeSequence"];
            $var["Stock"] = $variant["Stock"];
            $var["Price<10 EUR"] = $variant["Price<10 EUR"];
            $var["SKU_Start_Date"] = $variant["SKU_Start_Date"];
            $var["Published"] = $variant["Published"];
            $allstststyles[$stylecode]["variants"][$variant["B2BSKUREF"]] = $var;
        }
    
    return($allstststyles);

}

Connect to WooCommerce REST API in PHP

WooCommerce provides several versions of JSON/REST API. The behaviour of these versions is not similar from one to another. We used the “Legacy V3” version of the API (which is unfortunately deprecated) and the official “V2” version of the API.

You can find the documentation of the V2 here. We found the official V2 version of the API much more complicated than the legacy V3. Here is why:

  • With both V2 and Legacy V3, the product creation/update requires to work with internal id’s of categories and tags. So we need to retrieve these first.

  • But in V2, there is a pagination mechanism returning the values 10 by 10 (with our “posts_per_page” setting). We were able to change this to a “posts_per_page” of maximum 100 items, which is unfortunately not enough when retrieving, for example, all products. So we had to implement a loop going through the different pages in order to get all categories and all tags.

  • The Legacy V3 lets you import a product and its variants (“variations” in WooCommerce language) in 1 call, which is very handy. V2 on the other hand does not allow this. The process is :

    1. First import the product

    2. Then import the variations

    3. Then import the pictures

So, as you can see, the official V2 version of the JSON/REST API of WooCommerce introduces some regressions compared to the Legacy V3.

Take a look at our examples of PHP code to see the different tests we realized.

Enable REST API Features in WooCommerce

You need to enable the REST API in the WooCommerce configuration.

Go to:

  • WooCommerce -> Settings

  • Tab Advanced


  • Subtab Legacy API


  • Tick the checkbox and click “Save

  • Then go to subtab REST API to create a new API Key. Click on “Add key”


  • Enter a description and select the user.

  • Choose permissions “Read/Write”.

  • Click on Generate API key


Be sure to copy the consumer key and the consumer secret now. After this step it will not be possible anymore!

We use these keys with HTTP Basic Authentication during our requests. Our base URL is for example something like :

https://YOUR_CONSUMER_KEY:YOUR_CONSUMER_SECRET@YOUR_DOMAIN/wp-json/wc/v2


By default, the REST API is available behind the following endpoint: /?rest_route=

The documentation uses another endpoint:

  • /wc-api/v3 for the legacy V3 version

  • /wp-json/wc/v2 for the official V2 version

In order to use these endpoints, you need to change the permalinks configuration. By default, the configuration selected is “Plain”. You must change this to any other configuration. We chose “Day and name”.