Example 1 - Simple Export

Introduction and data structure

 

This first example shows the simple export through the API of all the items in visible status from the PIM account.

Let's start by creating a generic export connector. To do this, in the Channel Marketplace in our platform, select the first connector:

Generic export connector

On the first page of the connector, the Parameters page, select a private key. This will ensure the appropriate security during the data transfer.

Select a private key

On the Parameters page also select the languages to connect and the status of the items to be sent.

Languages to export  and the status

In this example we will only connect the English language and will only send items which are set as visible.

The parameter Include empty categories, when active, exports those categories which do not have any items. "Export multi-language titles” allows us to choose if we want the titles of the fields to be exported in the different languages, or only in the main language.

Empty categories

When the parameters are configured as in the screenshot, neither the empty categories nor the multi-language titles will be exported.

Once the connector parameters in the first tab are configured, it is time for the next step: the Output data Tab. Here you can configure the different tables and their fields.

In our example, we will select a few fields from each of the tables.

In Categories we select the following fields: Reference, Name, Category Reference, Description and Image.

In Categories we select the following fields

In the Products table we select the Reference, Name, Category Reference, Description and Image fields:

Products Tab

For the Variants table, we export the Variant Reference, Product Reference and 2 variable attributes: Size and Price.

In Variants we select the following fields

Saving all the settings, you create the connector which is now ready for the first call.

Connector ready for the first call

Once the connector is created, you can choose the items to export. We did not apply any filters in this example, except the status of the items.

In the section Categories, we set the entire category tree as visible; as a result we will have 14 visible categories:

Category tree

We now choose five products (randomly) and, among those, only two with variants (six variants in total):

Random products

It is important that the 16 variants are set as visible:

The variants

 

Code

 

The code is essential for the connection. In this case, we are going to see an example of how to export the desired items through the SL API. 

We start by creating a SalesLayer_Conn() class with the connector's credentials as parameters. Credentials are in the Sales Layer connector as shown in the following screenshot:

Custum Connector

We have saved the connector code in the $connector_id  parameter and its key in the $secret_key parameter.

It is now time to make a call to get_info() function of the class. This function extracts all the data configured in the connector and saves it locally.

As a parameter of the function itself we have submitted the value of $last_update. This parameter corresponds to the UNIX date of the last synchronization and is saved after every synchronization, ready to be used in the function call on the next synchronization.

As it is the first sync in our example, we set the parameter as 0.

Through the has_response_error() function we can check if the API has returned any error, and we can now print the following relevant information using these SDK functions:

  • The API version is through the get_response_api_version() function.
  • UNIX date of the exact time of the API call with the get_response_time() function.
  • The default language of the PIM by calling the get_response_default_language() function.

Thanks to the Nice_r library (more info here) we can now print the data returned by the API with the get_response_table_data() function.

<?php 
define('LOC_BASE', dirname(__FILE__) . '/');
require(LOC_BASE.'SalesLayer-Conn.php');
require(LOC_BASE.'lib/nice_r-master/Nicer.php');
?>

<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
    <link rel="stylesheet" type="text/css" href="lib/nice_r-master/nice_r.css?version=<?php echo filemtime(LOC_BASE.'lib/nice_r-master/nice_r.css'); ?>"/>
    <script type="text/javascript" src="lib/nice_r-master/nice_r.js?version=<?php echo filemtime(LOC_BASE.'lib/nice_r-master/nice_r.js'); ?>"></script>
</head>
<body>
    <?php
    //SL conector credentials
    $connector_id = 'CN12347H3308C4486';
    $secret_key = '7aeb575d9bf15bfa238e8f01842417a2';

    $last_update= "0";

    //Create object with the credentials on the connector in S
    $SLConn = new SalesLayer_Conn ($connector_id, $secret_key);
    
    //API call to export the information
    $SLConn->get_info($last_update);

    if ($SLConn->has_response_error()) {
        echo "<h4>Error:</h4>\n\n Code: ".$SLConn->get_response_error().
             "<br>\nMessage: ".           $SLConn->get_response_error_message();
    } else {
        echo "<h4>Response OK</h4>\n".
             "<p>".
             "API version: <b>".            $SLConn->get_response_api_version()          ."</b><br />\n".
             "Time: <b>".                   $SLConn->get_response_time('unix')                 ."</b><br/>\n".
             "Default language: <b>".       $SLConn->get_response_default_language()     ."</b><br/><br />\n".
             "</p>";
        
        //Print response from API
        $n = new Nicer($SLConn->get_response_table_data());
        $n->render();
        echo "<hr/>";
    }       
    ?>
</body>
</html>

 

Execution and its results

 

These are the results after running the code script:

Results after running the code script

In our example, we set 14 categories, 5 products, and 16 variants as visible but it has returned:

  • 11 categories
  • 5 products
  • 6 variants

This is the result of our settings in the connector parameters since we intended to avoid exporting empty categories. Therefore, categories without any associated products will not be exported and only direct categories and the ones depending on them will be exported.

Include empty categories

Even if the variants are set as visible, they will not be exported if they are not linked to parent products that have also been exported. Only 2 of the 5 products that we set as visible have variants. As a result, only those variants linked to these two products will be exported.

In greater detail, we can also confirm the exported tables and fields. In the category table we can see the four fields that we have selected in the connector:

Category

In the product table we can find the following:

Products

The variant table will look like in the screenshot below: 

Variants