WooCommerce / Sample Implementation

Discover how to easily configure Wordpress to connect to our API

1/Setup your Server

A basic configuration to unleash your webshop.

2/Install WooCommerce

Enable needed features for WooCommerce

3/Install WP-AllImport

Setup the import plugin

4/Update the stock

Enable fast-stock refresh

Install WP-ALL Import

Preamble

This is a paid plugin but it is wise to install it as it make considerably easy to import product in your shop. Cost is around $149

Go to the editor's website : http://www.wpallimport.com/portal/

Create an account and buy both WP All Import Pro + WP All Import Plugin for WooCommerce Pro

You will receive an email with two links – right click on each one to get the complete (long) link


Download and Install Plugin

Download the install files. You must do this manually from your server

$ wget -i / 'http://paste_your_first_link_here' -O wp-allimport-pro.zip
$ wget -i / 'http://paste_your_second_link_here' -O wpai-woocommerce-add-on-pro.zip

And unzip these

$ unzip wp-allimport-pro.zip
$ unzip wpai-woocommerce-add-on-pro.zip

Finally move the archives to the wp-content plugin folder

$ sudo mv /wp-all-import-pro/ /var/www/wordpress/wp-content/plugins/
$ sudo mv /wpai-woocommerce-add-on/ /var/www/wordpress/wp-content/plugins/

Set correct permissions on those plugins folder

$ sudo chown -R www-data:www-data /var/www/wordpress/wp-content/plugins/wp-all-import-pro
$ sudo chown -R www-data:www-data /var/www/wordpress/wp-content/plugins/wpai-woocommerce-add-on
$ sudo chmod 755 /var/www/wordpress/wp-content/plugins/wp-all-import-pro
$ sudo chmod 755 /var/www/wordpress/wp-content/plugins/wpai-woocommerce-add-on

Go back to your WP-Admin page, Go to “Plugins > Installed plugins” and activate both plugins

Create a script that will retrieve the latest information

This step is optional - but the best way would be to get all products directly within your system without any human intervention, so we will now create a small php script that will output a csv file in the folder that is monitored by the WP-AllImport plugin.

  • In order to do this you will need your API credentials. If you don’t have one you can send an email to insidesales@stanleystella.com

  • You will also need to understand how an API call works and how you can filter the dataset you want to import. The below script gets the full list of products in English

First, let’s create a folder to hold our script

$ sudo mkdir /var/www/wordpress/scripts

And create our php script : GetFile.php

$ sudo nano /var/www/wordpress/scripts/GetFile.php

Copy paste the following

<?php
ini_set("memory_limit","128M");
ini_set("max_execution_time",9000);
ini_set('default_charset', 'utf-8');
 
// Set API Url
$url = 'https://api.stanleystella.com/webrequest/products/get_json';

// This is where the file is outputted.
// Should be the "look in" folder of the WP Import Plugin

$csvFileName = '/var/www/wordpress/wp-content/uploads/wpallimport/files/products.csv';

$jsonData = array(
    'jsonrpc' => '2.0',
    'method' => 'call',
    'params' => array(
        'db_name' => 'production_api',
        'password' => 'your-api-password-here',
        'user' => 'your-api-login-here',
        // Select here the language you wish to import
        'LanguageCode' => 'en_US',
        // This is very important if you want to retrieve only products we sell
        'Published' => 'True',
    ),
    'id' => 0
);

//Initiate cURL
$ch = curl_init($url);

//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);

// Setup cURL// Do not return the result in the browser
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Set https
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//Execute the request
$result = curl_exec($ch);
//Decode output and isolate the content of the "result" key
$jsonDataDecoded = json_decode(json_decode($result)->result, true);
//Close CURL
curl_close($ch);

// Generate csv
$fp = fopen($csvFileName,'w+');
fprintf($fp, chr(0xEF).chr(0xBB).chr(0xBF));
foreach($jsonDataDecoded as $row){
    fputcsv($fp,$row);
    }
fclose($fp); 
?>

Save the file. Now if you call https://wordpress.example.com/scripts/GetFile.php it will download a file called “products.csv” in the /var/www/wordpress/wp-content/uploads/wpallimport/files folder.

Now let's configure the plugin to read it...

Configure the plugin

Create a new import

Go to "All Import > New Import" and click on "Use Existing File". Select the file you want to process (in our case products.csv) then set that it must create New items as WooCommerce Products, and finally proceed to step 2.



You will be presented a screen with the first line of your csv file + navigation buttons



Click on "Proceed to step 3" to define the mapping.

Title & Description

Note that in all screens you can drag-and-drop the fields from the right pane to the editor.


First create your product card (the "Item" or "Product Level"). This is what will be displayed on the pages of your shop. Note that the final layout entirely depends on the template you have setup for your shop and that this configuration is out of the scope of thhis guide. For the purpose of this tutorial, we use the "Storefront" standard theme of WooCommerce.

Here is a sample configuration. You can click on "Preview" to see how it will display


WooCommerce AddOn Setup

This configures how the import plugin will create our products (in our case : Variable Products)

General Tab

Configure the product as "Variable Product" and configure the SKU and the regular price. For this example we decided to use column 57, which is the recommended sales prices for less than 10 pieces.


Inventory Tab

If you want to retrieve the stock from here, set "Manage Stock" to "Yes" and drag-and-drop the right column that holds Stock Qty

Note that it is not recommended to refresh the stock daily with this method, as the import file is huge. For stock purpose, please consider using our webservice


Attributes Tab

This one is really important as it will tell the plugin what kind of variants we want to create. In this case, each product will have different Colors and Size, so let’s write it down.


Advanced Tab

If you wish to use the product order we suggest, fill in the “Menu Order” field.

By doing so you will use the same sorting as we do on our webshop - new collection and best sellers on top.


Variation Tab

This one is also really important: it will tell the plugin how we want to group our variants. To do so we can use any field that is common to all variants. For this tutorial we will use a combination of StyleCode and StyleName.


Images

You have the opportunity to download our pictures during the import process - please do not direct link to these. You define the template to download and the plugin will automatically match it. If a picture doesn’t exist on our server it simply won’t download but no error will be raised.

If you do not want to download a lot of pictures, you might also wish to link only to field 95, which is the "main" picture of a style.

For a complete and comprehensive guide on our pictures, you can visit https://api.stanleystella.com/page/naming-conventions

Typically, you would probably want to download the following set per product:

https://res.cloudinary.com/www-stanleystella-com/t_pim/TechnicalNames/SFM0_{column_3[1]}_ {column_4[1]}.jpg
https://res.cloudinary.com/www-stanleystella-com/t_pim/TechnicalNames/SFM5_{column_3[1]}_ {column_4[1]}.jpg
https://res.cloudinary.com/www-stanleystella-com/t_pim/TechnicalNames/PFM0_{column_3[1]}_ {column_4[1]}.jpg
https://res.cloudinary.com/www-stanleystella-com/t_pim/TechnicalNames/PFM5_{column_3[1]}_ {column_4[1]}.jpg
https://res.cloudinary.com/www-stanleystella-com/t_pim/TechnicalNames/PBM0_{column_3[1]}_ {column_4[1]}.jpg
https://res.cloudinary.com/www-stanleystella-com/t_pim/TechnicalNames/PBM5_{column_3[1]}_ {column_4[1]}.jpg

Note: the below screenshot shows old URL's that aren't used anymore.


Custom fields

You might want to create some specific fields to further filter on your webshop. In the below section, we have defined three custom fields: Published, New Style and New Product


These fields are all boolean field (value 1/0 or True/False), so to make our life easier, we have setup a mapping to turn the boolean value into a readable one. To do so, for each field we click on "Field Options..." then "Mapping". See the below translation example for the "Published" field:


Taxonomy, Categories, Tags

In this tab we will setup categories and tagging. As for the other fields you can tune this to fit your needs. We recommend to add as many tags as possible as it it the way Wordpress works.

First set the categories. In the below screenshot we describe a hierarchical category based on types (like Top > Sweatshirts )

Then set all tags you want to use. We enabled he following tagging: Gender, Fit, Neckline, Sleeve type and Collection.

The collection is located in Column 76 and is a date : we launch collections on January 1st and September 1st every year, so we can setup a mapping like shown below. Note we are currently exploring better ways to handle this information.


Other Product Options

Here you can select if the imported product are immediately published or are set as draft.


Finally save everything by clicking on Next

Run the Import

Go to “All Import”, and click on Run Import. Be prepared to wait for a while, Wordpress is not known for being fast…


Fine tune the setup

The WP plugin is very powerful and allows you to tune the way you import the data. You can do by modifying the general import settings :


On the next screen you can define:

  • if the plugin needs to create new posts for new data in our file

  • if the plugin must delete the lines that no longer exists in our file

  • which fields must be updated each time you run the script


If you check "Choose wich data to update" you will see a list of the mapped fields and need to check/uncheck those you wish to update. In the below screenshot, we update everything but images: