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 helpdesk@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.