Example 2 - The Last Update Parameter

Introduction and data structure

 

Through the Sales Layer API, it is possible to export modified items after a certain date. This date (API parameter called last_update, Other Modeling Parameters | Sales Layer Help Center) is a parameter passed to the get_info() function of the SDK and defines the last date when the data have been updated. By using this parameter you ask the API to return the items modified on the platform after a certain date.

Warning:  If the connector has been modified or refreshed after the last past date, all items will be returned and the previous date will not be taken into account.

In this Example 2 we are going to show the use of this parameter.

We are going to use the connector created in Example 1, with the same table configuration (see table configuration in Example 1) and the same filters and parameters:

1

We haven’t added any search or label filters in the connector for the items we want to export, so the only parameter to consider will be the visible status.

  • In our example we have 14 visible categories:
2

The last modification date is 20/08/2020 at 11:28 am.

  • Regarding the products, we have 5 elements set as visible:
3

Thanks to the list mode we can check that the last modification date is 21/08/2020 at 11:27 am.

  • Among the variants, we have 6 elements:
4

Those 6 correspond to the total variants for the products set as visible.

As we can see, the last modification date of these variants was 21/08/2020 at 11:19 am.

 

Code

 

The code example to export the data with the $last_update parameter is the following:

<?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 connector credencials
    $connector_id = 'CN12347H3308C4486';
    $secret_key = '7aeb575d9bf15bfa238e8f01842417a2';
    
    //Definition of the last_update parameter
    $last_update= "1597924800";

    //Create object with the credentials on the connector in SL
    $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 the API response
        $n = new Nicer($SLConn->get_response_table_data());
        $n->render();
        echo "<hr/>";

    }       
    ?>
</body>
</html>

We start by creating a SalesLayer_Conn()class with the connector's credentials as parameters. These credentials can be found in the Sales Layer connector and have been saved in the $connector_id and $secret_key (see Example 1).

Now, we can make a call to the get_info() function of the class. This function extracts all data configured in the connector and saves them locally.

The parameter $last_update was passed as a parameter of the function, with the following date: 21/08/2020 12:00 UTC, corresponding to the UNIX value 1598007600. In this way, we are asking the API to return the changes made after this date, assuming that the connector was not modified or refreshed after the  $last_update date.

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 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 (click here for more info) we can now print the data returned by the API with the get_response_table_data().

 

Execution and its results

 

Running the script we got the following result:

5

As we can see, all the tables haven’t returned any modified items. This is because of the $last_update parameter that we have chosen (21/08/2020 at 12:00): a date that is after the last modification date of the visible items

Once our tables and their last modification dates have been analyzed, we select 21/08/2020 at 11:00 UTC as the $last_update date, corresponding to the UNIX value 1598004000.

As shown below, there have been no subsequent modifications to the Category Table:

2

The reference 42PH420305221 in the Product Table was modified, including its 3 variants. As a result, both the product and its variants will be exported since by default the API always exports the sub-items of a modified item:

6

In the Variant Table, we have 3 modified variants after the $last_update date but, as explained in the previous point, all the variants of the reference 42PH420305221 will be exported, therefore we have 5 modified variants in total:

7

When executing the script with the new date of $last_update, we obtained the following response:

8

This last image shows the items modified after the $last_update date.