WooCommerce / REST API
Discover how to easily use Wordpress API to connect to ours
Some Sample PHP Code
Preamble
This code is given as an illustration and comes without any warranty.
Product Import
PHP Sample Code
<?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';
$baseurl = "https://".$consumer_key.":".$consumer_secret."@".$domain."/wc-api/v3";
/* GET EXISTING CATEGORIES */
global $allcats;
$allcats = array();
$url = $baseurl."/products/categories?fields=id,name"; //?filter[limit]=15
$response = file_get_contents($url);
$jsonDataDecoded = json_decode($response, true);
foreach ($jsonDataDecoded["product_categories"] as $key => $cat) {
$allcats[$cat["name"]] = $cat["id"];
}
/* GET EXISTING TAGS */
global $alltags;
$alltags = array();
$url = $baseurl."/products/tags?fields=id,name"; //?filter[limit]=15
$response = file_get_contents($url);
$jsonDataDecoded = json_decode($response, true);
foreach ($jsonDataDecoded["product_tags"] as $key => $tag) {
$alltags[$tag["name"]] = $tag["id"];
}
/* GET PRODUCTS FROM STST API */
$allststvariants = getAllSTSTProductsGrouped();
foreach ($allststvariants as $stylecode => $style) {
// Check if style already imported. If yes : update else : create
$url = $baseurl."/products?filter[sku]=".$stylecode;
$response = file_get_contents($url);
$responsearray = json_decode($response, true);
//print_r($responsearray);
if (empty($responsearray["products"])) { // Prepare structure to create new
$stylename = $style["StyleName"];
$shortdesc = $style["ShortDescription"];
$longdesc = str_replace("\n", "<br/>", $style["LongDescription"]);
$type = $style["Type"];
$category = $style["Category"];
$gender = $style["Gender"];
$fit = $style["Fit"];
$neckline = $style["Neckline"];
$sleeve = $style["Sleeve"];
$responsearray["products"]['title'] = $stylecode." / ".$stylename;
$responsearray["products"]['type'] = 'variable';
// TODO should come from the published status of STST
$responsearray["products"]['status'] = 'publish';
$responsearray["products"]['permalink'] = 'https://testwp.stanleystella.com/product/'.$stylecode."-". str_replace(" ", "-", $stylename);
$responsearray["products"]['sku'] = $stylecode;
$responsearray["products"]['price'] = '0';
$responsearray["products"]['regular_price'] = '0';
$responsearray["products"]['managing_stock'] = 1;
$responsearray["products"]['stock_quantity'] = 0;
$responsearray["products"]['in_stock'] = 1;
$responsearray["products"]['visible'] = 1;
$responsearray["products"]['catalog_visibility'] = 'visible';
$responsearray["products"]['shipping_required'] = 1;
$responsearray["products"]['description'] = $longdesc;
$responsearray["products"]['short_description'] = $shortdesc;
$categories = array();
$categories[] = getAndCreateCategory($baseurl, $category);
$categories[] = getAndCreateCategory($baseurl, $type);
$responsearray["products"]['categories'] = $categories;
$tags = array();
$tags[] = getAndCreateTag($baseurl, $gender);
$tags[] = getAndCreateTag($baseurl, $fit);
$tags[] = getAndCreateTag($baseurl, $neckline);
$tags[] = getAndCreateTag($baseurl, $sleeve);
// NO PICTURE IMPORT AT THIS STAGE
$responsearray["products"]['attributes'] = array();
$colorattr = array();
$colorattr['name'] = 'Color';
$colorattr['slug'] = 'color';
$colorattr['position'] = 0;
$colorattr['visible'] = 1;
$colorattr['variation'] = 1;
$colorattr['options'] = array();
$sizeattr = array();
$sizeattr['name'] = 'Size';
$sizeattr['slug'] = 'size';
$sizeattr['position'] = 1;
$sizeattr['visible'] = 1;
$sizeattr['variation'] = 1;
$sizeattr['options'] = array();
$responsearray["products"]['variations'] = array();
$firstvariant = true;
foreach ($style["variants"] as $key => $variant) {
$colorcode = $variant["ColorCode"];
$colorname = $variant["Color"];
$colorsequence = $variant["ColorSequence"];
$sizecodenav = $variant["SizeCodeNavision"];
$sizecode = $variant["SizeCode"];
$sizesequence = $variant["SizeSequence"];
$stock = $variant["Stock"];
$published = $variant["Published"];
$SKU_Start_Date = $variant["SKU_Start_Date"];
$price = $variant["Price<10 EUR"];
$b2bskuref = $variant["B2BSKUREF"];
// Take the values of the first variant to set the generic.
if ($firstvariant) {
$firstvariant = false;
if ($SKU_Start_Date=="2018-01-01") {
$tags[] = getAndCreateTag($baseurl, "Spring/Summer 2018");
}
}
if (!in_array($colorname, $colorattr['options'])) $colorattr['options'][] = $colorname;
if (!in_array($sizecode, $sizeattr['options'])) $sizeattr['options'][] = $sizecode;
$newvariant = array();
$newvariant['sku'] = $b2bskuref;
$newvariant['price'] = $price;
$newvariant['regular_price'] = $price;
$newvariant['managing_stock'] = 1;
$newvariant['stock_quantity'] = $stock;
if ($stock>0) $newvariant['in_stock'] = 1; else $newvariant['in_stock'] = 0;
$newvariant['visible'] = 1;
$newvariant['attributes'] = array();
$colora = array();
$colora['name'] = "Color";
$colora['slug'] = "color";
$colora['option'] = $colorname;
$newvariant['attributes'][] = $colora;
$sizea = array();
$sizea['name'] = "Size";
$sizea['slug'] = "size";
$sizea['option'] = $sizecode;
$newvariant['attributes'][] = $sizea;
$responsearray["products"]['variations'][] = $newvariant;
}
$responsearray["products"]['tags'] = $tags;
$responsearray["products"]['attributes'][] = $colorattr;
$responsearray["products"]['attributes'][] = $sizeattr;
$url = $baseurl."/products";
$newproductid = createInWooCommerce($url, $responsearray);
} else {
// Equivalent logic for update
/* * ... */
updateInWooCommerce($url, $responsearray);
}
}
Picture Import
PHP Sample Code
<?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);
}
}
}