Prestashop/ CSV Import

Discover how to easily connect Prestashop

1/ Before you begin

Some prerequisites

2/Connect API's with PHP

Samples for both API's

3/Import CSV

Learn how to manually import data and automate refresh

4/PHP Code

A demo of what can be achieved

Final Step : Automation

Preamble

With this CSV import technique and the need to use internal id’s, it is very difficult to fully automate the product import process.

While we were able to import the Stanley/Stella product catalogue, we also made in this scenario a huge assumption: The styles are always sorted in the same order. Therefore, having a counter to define the internal id is enough. But this is absolutely no robust solution. If a new style appears, all id’s would be shifted by 1 (in our example) after the new style. The PHP program should ideally store the internal Prestashop id’s in a MySQL database, for example.

In order to fully automate this process, we suggest using the Prestashop REST API instead of importing with CSV files. The PHP program should then have the logic to ask Prestashop for the right internal id’s for categories and for existing styles (in case of update).

Documentation of the Prestashop REST API can be found here:

  • http://doc.prestashop.com/display/PS16/Web+service+tutorial 

  • http://doc.prestashop.com/display/PS16/Web+service+reference

See reference flow for update of objects via the API, according to the Prestashop documentation.

PHP Code

See the following code as an inspiration for generating csv file

<?php
ini_set("memory_limit","128M");
ini_set("max_execution_time",9000);
ini_set('default_charset', 'utf-8');
header('Content-Type: text');

$stpm = getAllSTSTProductsGrouped();

$fp = fopen('prestashop_combination_import.csv', 'w');
// CSV Header
$csvrow = array();
    $csvrow[] = "Product ID*";
    $csvrow[] = "Product Reference";
    $csvrow[] = "Attribute (Name:Type:Position)*";
    $csvrow[] = "Value (Value:Position)*";
    $csvrow[] = "Reference";
    $csvrow[] = "Supplier reference";
    $csvrow[] = "EAN13";
    $csvrow[] = "UPC";
    $csvrow[] = "Wholesale price";
    $csvrow[] = "Impact on price";
    $csvrow[] = "Ecotax";
    $csvrow[] = "Quantity";
    $csvrow[] = "Minimal quantity";
    $csvrow[] = "Impact on weight";
    $csvrow[] = "Default (0 = No, 1 = Yes)";
    $csvrow[] = "Combination available date";
    $csvrow[] = "Image position";
    $csvrow[] = "Image URLs (x,y,z...)";
    $csvrow[] = "Image alt texts (x,y,z...)";
    $csvrow[] = "ID / Name of shop";
    $csvrow[] = "Advanced Stock Managment";
    $csvrow[] = "Depends on stock";
    $csvrow[] = "Warehouse";

fputcsv($fp, $csvrow, ";");

$prestaid = 22;

foreach ($stpm as $stylecode => $style) {
    // Price : take first variant price
    $firstvariant = reset($style["variants"]);
    $firstprice = $firstvariant["Price<10 EUR"];
    // Summer collection : take from first variant
    $firstskustartdate = $firstvariant["SKU_Start_Date"];
    // Published
    $firstpublished = $firstvariant["Published"];
    // Get images for current style
    $picurls = array();
    $pics = getSTSTProductImages($stylecode);
    foreach ($pics as $key => $pic) {
        $colorcode = $pic["ColorCode"];
        $picname = $pic["FName"];
        $picurl = $pic["HTMLPath"];
        if (!isset($picurls[$colorcode])) $picurls[$colorcode] = array();
        $picurls[$colorcode][] = $picurl;
    }

    foreach ($style["variants"] as $key => $var) {
        $csvrow = array();
        $csvrow[] = $prestaid;
        $csvrow[] = $var["B2BSKUREF"];
        $colorname = $var["Color"];
        $colorcode = $var["ColorCode"];
        $sizename = $var["SizeCode"];
        $colorsequence = $var["ColorSequence"];
        $sizesequence = $var["SizeSequence"];
        $csvrow[] = "Color:color:".$colorsequence.",Size:select:".$sizesequence;
        $csvrow[] = $colorname.":".$colorsequence.",".$sizename.":".$sizesequence;
        $csvrow[] = $var["B2BSKUREF"];
        $csvrow[] = $var["B2BSKUREF"];
        $csvrow[] = "";
        $csvrow[] = "";
        $csvrow[] = "";
        $varprice = $var["Price<10 EUR"];
        $csvrow[] = $varprice - $firstprice;
        $csvrow[] = 0;
        $csvrow[] = $var["Stock"];
        $csvrow[] = 1;
        $csvrow[] = "";
        $csvrow[] = 0;
        $csvrow[] = $var["SKU_Start_Date"];
        // Image position & url's & alt text
        $csvrow[] = "";
        $first = true;
        $allpicurls = "";
        if (isset($picurls[$colorcode])) {
            foreach ($picurls[$colorcode] as $key => $picurl) {
                if (!$first) {
                    $allpicurls.=",";
                }
                $allpicurls.=$picurl;
                $first = false;
            }
        }
        $csvrow[] = $allpicurls;
        $csvrow[] = "";
        $csvrow[] = "";
        $csvrow[] = "";
        $csvrow[] = "";
        $csvrow[] = "";
        fputcsv($fp, $csvrow, ";");
    }
}

fclose($fp);