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

Some Sample PHP Code

Preamble

This code is given as an illustration and comes without any warranty.

Product Import

 

Picture Import

 

<?php
ini_set("memory_limit","128M");
ini_set("max_execution_time",9000);
ini_set('default_charset', 'utf-8');
header('Content-Type: text');
$consumer_key = 'YOUR_CONSUMER_KEY';
$consumer_secret = 'YOUR_CONSUMER_SECRET';
$domain = 'YOUR_DOMAIN';

// Use API v2
$baseurl = "https://".$consumer_key.":".$consumer_secret."@".$domain."/wp-json/wc/v2";

// Get product by sku
// We do a test with only 1 product (for testing time purpose)

$stylecode = "STPM540";
$url = $baseurl."/products?filter[sku]=".$stylecode;
$response = file_get_contents($url);
$jsonDataDecoded = json_decode($response, true);
$productid = null;
foreach ($jsonDataDecoded as $key => $product) {
    $productid = $product["id"];
    break;
}

if ($productid!=null) {
    // Get all pictures from STST
    $pics = getSTSTProductImages($stylecode);
    $picurls = array();
    $wppicid = array();
    $piccounter = 1;
    $first = true;
    $picdata = array();
    $picdata['images'] = array();
    foreach ($pics as $key => $pic) {
        $colorcode = $pic["ColorCode"];
        $picname = $pic["FName"];
        $picurl = $pic["HTMLPath"];

        if (!isset($picurls[$colorcode])) $picurls[$colorcode] = array();
        $picurls[$colorcode][] = $picname;

        // Add all pictures on the product
        // Set position 0 for the first SFM
        // Get the picture wordpress id back and store it
    
        $picobject = array();
        $picobject['src'] = $picurl;
        $picobject['name'] = $picname;

        if (substr($picname,0,3)=="SFM" && $first) {
            $picobject['position'] = 0;
            $first = false;
        } else {
            $picobject['position'] = $piccounter;
            $piccounter++;
        }
        $picdata['images'][] = $picobject;
    }
    $url = $baseurl."/products/".$productid;
    $answer = updateInWordpress($url, $picdata);
    foreach ($answer["images"] as $key => $value) {
        $wppicid[$value["name"]] = $value["id"];
    }

    // Get the variants for this product
    // Loop per page
    $allvariants = array();
    $pagenb = 1;
    $url = $baseurl."/products/".$productid."/variations";
    $response = file_get_contents($url);
    $jsonDataDecoded = json_decode($response, true);
    foreach ($jsonDataDecoded as $key => $var) {
        $allvariants[$var["sku"]] = $var;
    }
    while (!empty($jsonDataDecoded)) {
        $pagenb++;
        $url = $baseurl."/products/".$productid."/variations?page=".$pagenb;
        $response = file_get_contents($url);
        $jsonDataDecoded = json_decode($response, true);
        foreach ($jsonDataDecoded as $key => $var) {
            $allvariants[$var["sku"]] = $var;
        }
    }

    foreach ($allvariants as $sku => $var) {
        // Update variant pictures
        $varid = $var["id"];
        $colorcode = substr($sku, 7, 4);
        echoFlush($colorcode);
        $newvariant = array();
        $newvariant['image'] = array();
        if (isset($picurls[$colorcode])) {
            foreach ($picurls[$colorcode] as $key => $pic) {
                if (substr($pic,0,3)=="SFM") {
                    $newvariant['image']['id'] = $wppicid[$pic]; break;
                }
            }
            $url = $baseurl."/products/".$productid."/variations/".$varid;
            $answer = updateInWordpress($url, $newvariant);
            print_r($answer);
        }
    }
}