Prestashop/ CSV Import
Discover how to easily connect Prestashop
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
Product CSV Generation code sample
<?php
include_once("../Shopify/STSTfunctions.php");
ini_set("memory_limit","128M"); ini_set("max_execution_time",9000);
ini_set('default_charset', 'utf-8');
header('Content-Type: text');
// Prestashop categories
$categories = array();
$categories["Men"] = array();
$categories["Men"]["Tee-shirt"] = 15;
$categories["Men"]["Shirts"] = 15;
$categories["Men"]["Sweatshirts"] = 16;
$categories["Men"]["Polos"] = 17;
$categories["Men"]["Jackets"] = 18;
$categories["Men"]["Padded Jacket"] = 18;
$categories["Men"]["Jogging Shorts"] = 19;
$categories["Men"]["Pants"] = 20;
$categories["Women"] = array();
$categories["Women"]["Tee-shirt"] = 21;
$categories["Women"]["Tank Top"] = 21;
$categories["Women"]["Shirts"] = 21;
$categories["Women"]["Sweatshirts"] = 22;
$categories["Women"]["Sweat Dress"] = 22;
$categories["Women"]["Polos"] = 23;
$categories["Women"]["Jackets"] = 24;
$categories["Women"]["Padded Jacket"] = 24;
$categories["Women"]["Jogging Shorts"] = 25;
$categories["Women"]["Pants"] = 26;
$categories["Unisex"] = array();
$categories["Unisex"]["Tee-shirt"] = 27;
$categories["Unisex"]["Sweatshirts"] = 28;
$categories["Kids"] = array();
$categories["Kids"]["Boys"] = 29;
$categories["Kids"]["Girls"] = 30;
$categories["Kids"]["Unisex"] = 31;
$stpm = getAllSTSTProductsGrouped();
$fp = fopen('prestashop_product_import.csv', 'w');
// CSV Header
$csvrow = array();
$csvrow[] = "Product ID";
$csvrow[] = "Active (0/1)";
$csvrow[] = "Name *";
$csvrow[] = "Categories (x,y,z...)";
$csvrow[] = "Price tax included";
$csvrow[] = "Tax rules ID";
$csvrow[] = "Wholesale price";
$csvrow[] = "On sale (0/1)";
$csvrow[] = "Discount amount";
$csvrow[] = "Discount percent";
$csvrow[] = "Discount from (yyyy-mm-dd)";
$csvrow[] = "Discount to (yyyy-mm-dd)";
$csvrow[] = "Reference #";
$csvrow[] = "Supplier reference #";
$csvrow[] = "Supplier";
$csvrow[] = "Manufacturer";
$csvrow[] = "EAN13";
$csvrow[] = "UPC";
$csvrow[] = "Ecotax";
$csvrow[] = "Width";
$csvrow[] = "Height";
$csvrow[] = "Depth";
$csvrow[] = "Weight";
$csvrow[] = "Quantity";
$csvrow[] = "Minimal quantity";
$csvrow[] = "Visibility";
$csvrow[] = "Additional shipping cost";
$csvrow[] = "Unity";
$csvrow[] = "Unit price";
$csvrow[] = "Short description";
$csvrow[] = "Description";
$csvrow[] = "Tags (x,y,z...)";
$csvrow[] = "Meta title";
$csvrow[] = "Meta keywords";
$csvrow[] = "Meta description";
$csvrow[] = "URL rewritten";
$csvrow[] = "Text when in stock";
$csvrow[] = "Text when backorder allowed";
$csvrow[] = "Available for order (0 = No, 1 = Yes)";
$csvrow[] = "Product available date";
$csvrow[] = "Product creation date";
$csvrow[] = "Show price (0 = No, 1 = Yes)";
$csvrow[] = "Image URLs (x,y,z...)";
$csvrow[] = "Image alt texts (x,y,z...)";
$csvrow[] = "Delete existing images (0 = No, 1 = Yes)";
$csvrow[] = "Feature(Name:Value:Position)";
$csvrow[] = "Available online only (0 = No, 1 = Yes)";
$csvrow[] = "Condition";
$csvrow[] = "Customizable (0 = No, 1 = Yes)";
$csvrow[] = "Uploadable files (0 = No, 1 = Yes)";
$csvrow[] = "Text fields (0 = No, 1 = Yes)";
$csvrow[] = "Out of stock";
$csvrow[] = "ID / Name of shop";
$csvrow[] = "Advanced stock management";
$csvrow[] = "Depends On Stock";
$csvrow[] = "Warehouse";
fputcsv($fp, $csvrow, ";");
$prestaid = 22;
foreach ($stpm as $stylecode => $style) {
$csvrow = array();
$stylename = $style["StyleName"];
// Categories
$gender = $style["Gender"];
$type = $style["Type"];
$category = $style["Category"];
switch ($gender) {
case "Kids":
$categoryid = $categories["Kids"]["Unisex"];
break;
case "Boys":
$categoryid = $categories["Kids"]["Boys"];
break;
case "Girls":
$categoryid = $categories["Kids"]["Girls"];
break;
default:
$categoryid = $categories[$gender][$type];
break;
}
// Price : take first variant price
$firstvariant = reset($style["variants"]);
$price = $firstvariant["Price<10 EUR"];
// Summer collection : take from first variant
$skustartdate = $firstvariant["SKU_Start_Date"];
// Published
$published = $firstvariant["Published"];
// Descriptions
$shortdescription = $style["ShortDescription"];
$longdescription = str_replace("\n", "<br/>", $style["LongDescription"]);
// Tags
$fit = $style["Fit"];
$neckline = $style["Neckline"];
$sleeve = $style["Sleeve"];
$csvrow[] = $prestaid;
if ($published=="True") $csvrow[] = 1;
else $csvrow[] = 0;
$csvrow[] = $stylename;
$csvrow[] = $categoryid;
$csvrow[] = $price;
$csvrow[] = 1;
$csvrow[] = "";
$csvrow[] = 0;
$csvrow[] = "";
$csvrow[] = "";
$csvrow[] = "";
$csvrow[] = "";
$csvrow[] = $stylecode;
$csvrow[] = $stylecode;
$csvrow[] = "";
$csvrow[] = "";
$csvrow[] = "";
$csvrow[] = "";
$csvrow[] = "";
$csvrow[] = "";
$csvrow[] = "";
$csvrow[] = "";
$csvrow[] = "";
$csvrow[] = "";
$csvrow[] = 1;
$csvrow[] = "both";
$csvrow[] = "";
$csvrow[] = "";
$csvrow[] = "";
$csvrow[] = $shortdescription;
$csvrow[] = $longdescription;
$tags = $category.",".$type.",".$gender.",".$fit.",".$neckline.",".$sleeve;
if ($skustartdate=="2018-01-01") {
$tags .= ",Spring/Summer 2018";
}
$csvrow[] = $tags;
$csvrow[] = "";
$csvrow[] = "";
$csvrow[] = "";
$csvrow[] = "";
$csvrow[] = "";
$csvrow[] = "";
if ($published=="True") $csvrow[] = 1;
else $csvrow[] = 0;
$csvrow[] = "";
$csvrow[] = "";
$csvrow[] = 1;
// Get images for current style and find a SFM picture
$picurls = "";
$pics = getSTSTProductImages($stylecode);
foreach ($pics as $key => $pic) {
$colorcode = $pic["ColorCode"];
$picname = $pic["FName"];
$picurl = $pic["HTMLPath"];
if (substr($picname,0,3)=="SFM") {
$picurls = $picurl;
break;
}
}
$csvrow[] = $picurls;
$csvrow[] = "";
$csvrow[] = "";
$csvrow[] = "";
$csvrow[] = 0;
$csvrow[] = "new";
$csvrow[] = 0;
$csvrow[] = 0;
$csvrow[] = 0;
$csvrow[] = 0;
$csvrow[] = 0;
$csvrow[] = 0;
$csvrow[] = 0;
$csvrow[] = 0;
fputcsv($fp, $csvrow, ";");
$prestaid++;
//if ($prestaid>50) break;
}
fclose($fp);
Combination CSV Generation code sample
<?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);