Skip to content

Instantly share code, notes, and snippets.

@NodokaMurmevent
Created August 10, 2018 09:48
Show Gist options
  • Save NodokaMurmevent/a54abdd8250fd5289d701acaa7c0b1c5 to your computer and use it in GitHub Desktop.
Save NodokaMurmevent/a54abdd8250fd5289d701acaa7c0b1c5 to your computer and use it in GitHub Desktop.
a shity php convert for csv export magento <1.6 to import on magento 2, not perfect and you need to clean by yourself some thing
<?php
/*
License
* GLWT(Good Luck With That) Public License
* Copyright (c) Everyone, except Author
*
* Everyone is permitted to copy, distribute, modify, merge, sell, publish,
* sublicense or whatever they want with this software but at their OWN RISK.
*
* Preamble
*
* The author has absolutely no clue what the code in this project does.
* It might just work or not, there is no third option.
*
*
* GOOD LUCK WITH THAT PUBLIC LICENSE
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION, AND MODIFICATION
*
* 0. You just DO WHATEVER YOU WANT TO as long as you NEVER LEAVE A
* TRACE TO TRACK THE AUTHOR of the original product to blame for or hold
* responsible.
*
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Good luck and Godspeed.
*/
header('Content-Type: application/excel');
header('Content-Disposition: attachment; filename="sample.csv"');
set_time_limit ( 3600 );
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
$csv = csv_to_associative_array("export_all_products_clean-2.csv",',','"');
// echo "<pre>";
// var_dump($csv[0]);
// echo "</pre>";
$newCSV = array();
$skuParent = "";
$head =["sku","store_view_code","attribute_set_code","product_type","categories","product_websites","name","description","short_description","weight","product_online","tax_class_name","visibility","price","special_price","special_price_from_date","special_price_to_date","url_key","meta_title","meta_keywords","meta_description","base_image","base_image_label","small_image","small_image_label","thumbnail_image","thumbnail_image_label","swatch_image","swatch_image_label","created_at","updated_at","new_from_date","new_to_date","display_product_options_in","map_price","msrp_price","map_enabled","gift_message_available","custom_design","custom_design_from","custom_design_to","custom_layout_update","page_layout","product_options_container","msrp_display_actual_price_type","country_of_manufacture","additional_attributes","qty","out_of_stock_qty","use_config_min_qty","is_qty_decimal","allow_backorders","use_config_backorders","min_cart_qty","use_config_min_sale_qty","max_cart_qty","use_config_max_sale_qty","is_in_stock","notify_on_stock_below","use_config_notify_stock_qty","manage_stock","use_config_manage_stock","use_config_qty_increments","qty_increments","use_config_enable_qty_inc","enable_qty_increments","is_decimal_divided","website_id","related_skus","related_position","crosssell_skus","crosssell_position","upsell_skus","upsell_position","additional_images","additional_image_labels","hide_from_product_page","custom_options","bundle_price_type","bundle_sku_type","bundle_price_view","bundle_weight_type","bundle_values","bundle_shipment_type","configurable_variations","configurable_variation_labels","associated_skus"];
array_push($newCSV,$head);
foreach ($csv as $k => $v) {
$k=$k+1;
$newCSV[$k][0] = $v["sku"];
$newCSV[$k][1] = "";
$newCSV[$k][2] = "Default";
if ($v["type"] == "simple") {$newCSV[$k][3] = "virtual"; } else { $newCSV[$k][3] = $v["type"];}
$newCSV[$k][5] = "base";
$newCSV[$k][6] = $v["name"];
$newCSV[$k][7] = trim($v["description"]);
$newCSV[$k][8] = $v["short_description"];
$newCSV[$k][9] = $v["weight"];
$newCSV[$k][10] = "1"; //online
$newCSV[$k][11] = "Taxable Goods";
if ($v["visibility"] == "Nulle part") {
$newCSV[$k][12] = "Not Visible Individually";
} else {
$newCSV[$k][12] = "Catalog, Search";
}
$newCSV[$k][13] = $v["price"];
$newCSV[$k][14] = "";
$newCSV[$k][15] = "";
$newCSV[$k][16] = "";
//metas
$newCSV[$k][18] = trim(str_replace("pioupiou","",$v["meta_title"]));
$newCSV[$k][19] = trim($v["meta_keyword"]);
$newCSV[$k][20] = trim($v["meta_description"]);
//images
$newCSV[$k][21] = $v["image"];
$newCSV[$k][22] = $v["image_label"];
$newCSV[$k][23] = $v["small_image"];
$newCSV[$k][24] = $v["small_image_label"];
$newCSV[$k][25] = $v["thumbnail"];
$newCSV[$k][26] = $v["thumbnail_label"];
$newCSV[$k][27] = "";
$newCSV[$k][28] = "";
$newCSV[$k][29] = "29-05-2018 08:28";
$newCSV[$k][30] = "29-05-2018 08:28";
$newCSV[$k][31] = "";
$newCSV[$k][32] = "";
$newCSV[$k][33] = "Block after Info Column"; //display_product_options_in
$newCSV[$k][34] = "";
$newCSV[$k][35] = "";
$newCSV[$k][36] = "";
$newCSV[$k][37] = "No";
$newCSV[$k][38] = "";
$newCSV[$k][39] = "";
$newCSV[$k][40] = "";
$newCSV[$k][41] = "";
$newCSV[$k][42] = "";
$newCSV[$k][43] = "";
$newCSV[$k][44] = "";
$newCSV[$k][45] = "";
$newCSV[$k][46] = ""; //additional_attributes
$newCSV[$k][47] = $v["qty"];
$newCSV[$k][48] = "0.0000";
$newCSV[$k][49] = "1";
$newCSV[$k][50] = "0";
$newCSV[$k][51] = "0";
$newCSV[$k][52] = "1";
$newCSV[$k][53] = "1.0000";
$newCSV[$k][54] = "1";
$newCSV[$k][55] = "10000.0000";
$newCSV[$k][56] = "1";
$newCSV[$k][57] = "1";
$newCSV[$k][58] = "1.0000";
$newCSV[$k][59] = "1";
if ($newCSV[$k][3] == "configurable") {
$newCSV[$k][60] = "0"; // managestock
$newCSV[$k][61] = "0";
} else {
$newCSV[$k][60] = "1";
$newCSV[$k][61] = "1";
}
$newCSV[$k][62] = "1";
$newCSV[$k][63] = "1.0000";
$newCSV[$k][64] = "1";
$newCSV[$k][65] = "0";
$newCSV[$k][66] = "0";
$newCSV[$k][67] = "0";
//related
$newCSV[$k][68] = "";
$newCSV[$k][69] = "";
//crossell
$newCSV[$k][70] = "";
$newCSV[$k][71] = "";
//upsells
$newCSV[$k][72] = "";
$newCSV[$k][73] = "";
$newCSV[$k][74] = ""; //additional image
$newCSV[$k][75] = ""; //additional image label
$newCSV[$k][76] = "";
$newCSV[$k][77] = ""; //custom options
//bundles
$newCSV[$k][78] = "";
$newCSV[$k][79] = "";
$newCSV[$k][80] = "";
$newCSV[$k][81] = "";
$newCSV[$k][82] = "";
$newCSV[$k][83] = "";
$newCSV[$k][85] = "";
$v["color"]=trim(str_replace(["/ ","/"," "],["-","_","-"],$v["color"]));
if ($v["color"] =="Multicolore") {
$v["color"] ="";
}
$testconfigurable = $newCSV[$k][3];
// here a complete exemple for convert the options (need to exist before the csv import)
if ($testconfigurable == "configurable") {
$newCSV[$k][4] = replaceCateg($v["category_ids"]);
$CategParent = $newCSV[$k][4] ;
$keyParent = $k;
$skuParent = $newCSV[$k][0];
$sdfkhsjkghParent = $newCSV[$keyParent][85];
$newCSV[$k][17] = trim($v["url_key"]);
}elseif($newCSV[$k][3] == "virtual") {
$newCSV[$k][4] = $CategParent;
$piou = explode( '-', $skuParent);
$newCSV[$k][0] = trim(str_replace(" ","-",$piou[0]));
$piou[0] = trim(str_replace(" ","-",$piou[0]));
if ( $v["color"] != "" && $v["taille_xs"] != "" ) {
$c = $v["color"];
$txs = $v["taille_xs"];
$newCSV[$k][0] = $newCSV[$k][0]."-".$c."-".$txs;
$newCSV[$keyParent][84] .="sku=".$newCSV[$k][0].",color=".$c.",taille_xs=".$txs."|";
$newCSV[$k][46] = 'color='.$c.',taille_xs='.$txs;
$newCSV[$k][17] = $piou[0]."-".$c."-".$txs;
if ( $sdfkhsjkghParent == "") {
$newCSV[$keyParent][85] = "taille_xs=Taille XS,color=Color";
}
}
elseif ($v["color"] != "" && $v["taille_sg"] != "") {
$c = $v["color"];
$bonnet = $v["taille_sg"];
$newCSV[$k][0] =$newCSV[$k][0]."-".$c."-".$bonnet;
$newCSV[$keyParent][84] .="sku=".$newCSV[$k][0].",color=".$c.",taille_bonnet=".$bonnet."|";
$newCSV[$k][46] = 'color='.$c.',taille_bonnet='.$bonnet;
$newCSV[$k][17] = $piou[0]."-".$c."-".$bonnet;
if ( $sdfkhsjkghParent == "") {
$newCSV[$keyParent][85] = "taille_bonnet=Taille Bonnet,color=Color";
}
}
elseif ($v["color"] != "" && $v["taille_1"] != "") {
$c = $v["color"];
$txl = $v["taille_1"];
$newCSV[$k][0] =$newCSV[$k][0]."-".$c."-".$txl;
$newCSV[$keyParent][84] .="sku=".$newCSV[$k][0].",color=".$c.",taille_1=".$txl."|";
$newCSV[$k][46] = 'color='.$c.',taille_1='.$txl;
$newCSV[$k][17] = $piou[0]."-".$c."-".$txl;
if ( $sdfkhsjkghParent == "") {
$newCSV[$keyParent][85] = "taille_1=Taille 1,color=Color";
}
}
elseif ($v["color"] != "" && $v["taille_34"] != "") {
$c = $v["color"];
$t34 = $v["taille_34"];
$newCSV[$k][0] =$newCSV[$k][0]."-".$c."-".$t34;
$newCSV[$keyParent][84] .="sku=".$newCSV[$k][0].",color=".$c.",taille_34=".$t34."|";
$newCSV[$k][46] = 'color='.$c.',taille_34='.$t34;
$newCSV[$k][17] = $piou[0]."-".$c."-".$t34;
if ( $sdfkhsjkghParent == "") {
$newCSV[$keyParent][85] = "taille_34=Taille 34,color=Color";
}
}
elseif ($v["taille_sg"] != "") {
$bonnet=$v["taille_sg"];
$newCSV[$k][0] =$newCSV[$k][0]."-".$bonnet;
$newCSV[$keyParent][84] .="sku=".$newCSV[$k][0].",taille_bonnet=".$bonnet."|";
$newCSV[$k][46] = 'taille_bonnet='.$bonnet;
$newCSV[$k][17] = $piou[0]."-".$bonnet;
if ( $sdfkhsjkghParent == "") {
$newCSV[$keyParent][85] = "taille_bonnet=Taille Bonnet";
}
}
elseif ($v["taille_34"] != "") {
$t34=$v["taille_34"];
$newCSV[$k][0] =$newCSV[$k][0]."-".$t34;
$newCSV[$keyParent][84] .="sku=".$newCSV[$k][0].",taille_34=".$t34."|";
$newCSV[$k][46] = 'taille_34='.$t34;
$newCSV[$k][17] = $piou[0]."-".$t34;
if ( $sdfkhsjkghParent == "") {
$newCSV[$keyParent][85] = "taille_34=Taille 34";
}
}
elseif ($v["taille_1"] != "") {
$t1=$v["taille_1"];
$newCSV[$k][0] =$newCSV[$k][0]."-".$t1;
$newCSV[$keyParent][84] .="sku=".$newCSV[$k][0].",taille_1=".$t1."|";
$newCSV[$k][46] = 'taille_1='.$t1;
$newCSV[$k][17] = $piou[0]."-".$t1;
if ( $sdfkhsjkghParent == "") {
$newCSV[$keyParent][85] = "taille_1=Taille 1";
}
}
elseif ($v["color"] != "") {
$col=$v["color"];
$newCSV[$k][0] =$newCSV[$k][0]."-".$col;
$newCSV[$keyParent][84] .="sku=".$newCSV[$k][0].",color=".$col."|";
$newCSV[$k][46] = 'color='.$col;
$newCSV[$k][17] = $piou[0]."-".$col;
if ( $sdfkhsjkghParent == "") {
$newCSV[$keyParent][85] = "color=Color";
}
}
elseif ($v["taille_xs"] != "") {
$txs=$v["taille_xs"];
$newCSV[$k][0] =$newCSV[$k][0]."-".$txs;
$newCSV[$keyParent][84] .="sku=".$newCSV[$k][0].",taille_xs=".$txs."|";
$newCSV[$k][46] = 'taille_xs='.$txs;
$newCSV[$k][17] = $piou[0]."-".$txs;
if ( $sdfkhsjkghParent == "") {
$newCSV[$keyParent][85] = "taille_xs=Taille XS";
}
}
}
$newCSV[$k][86] = ""; //associated skus
}
foreach ($newCSV as $key => $value) {
$newCSV[$key][84] = substr($newCSV[$key][84], 0, -1);
ksort($newCSV[$key]);
}
//echo "<pre>";
//print_r($newCSV);
//echo "</pre>";
// $out = arrayToCsv($newCSV);
// echo $out;
/**
* replace the id bye the name, you need to find by yourself the matching
*
* @param [array] $caca
* @return String
*/
function replaceCateg($caca){
$piou = explode( ',', $caca );
if (count($piou) == 1 && $piou[0] =="14") {$piou = ["14","92"];}
$parents = ["%4%","%5%","%73%","%115%","%92%","%15%"];
$parentsLabels = ["machin1","piou","machin2","miaou","yolo","upvote"];
$brand = [
"%31%",
];
$brandLabel = [
"yolilo",
];
$miaou = [
"%16%",
];
$miaouLabel = [
"yololi",
];
$Nutella = "Default Category,";
$categ = "";
foreach ($piou as $k => $v) {
$categ = $v;
if (in_array("%".$v."%",$parents)) {
$arbo = "/%".$v."%";
}
elseif(in_array("%".$v."%",$brand)) {
$MarqueNutella = ",Default Category/"."%".$v."%";
}
}
if ($categ !="") {
$Nutella .= "Default Category".$arbo.",Default Category".$arbo."/"."%".$categ."%".$MarqueNutella ;
} else {
$Nutella .= "Default Category".$arbo.",".$MarqueNutella ;
}
// $Nutella .= $MarqueNutella;
$Nutella = str_replace($parents,$parentsLabels,$Nutella);
$Nutella = str_replace($brand,$brandLabel,$Nutella);
$Nutella = str_replace($miaou,$miaouLabel,$Nutella);
return $Nutella;
}
function arrayToCsv( array $fields, $delimiter = ',', $enclosure = '"', $encloseAll = false, $nullToMysqlNull = false ) {
$delimiter_esc = preg_quote($delimiter, '/');
$enclosure_esc = preg_quote($enclosure, '/');
$outputString = "";
foreach($fields as $tempFields) {
$output = array();
foreach ( $tempFields as $field ) {
// ADDITIONS BEGIN HERE
if (gettype($field) == 'integer' || gettype($field) == 'double') {
$field = strval($field); // Change $field to string if it's a numeric type
}
// ADDITIONS END HERE
if ($field === null && $nullToMysqlNull) {
$output[] = 'NULL';
continue;
}
// Enclose fields containing $delimiter, $enclosure or whitespace
if ( $encloseAll || preg_match( "/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field ) ) {
$field = $enclosure . str_replace($enclosure, $enclosure . $enclosure, $field) . $enclosure;
}
$output[] = $field;
}
$outputString .= implode( $delimiter, $output )."\r\n";
}
return $outputString;
}
function csv_to_associative_array($file, $delimiter = ',', $enclosure = '"')
{
if (($handle = fopen($file, "r")) !== false) {
$headers = fgetcsv($handle, 0, $delimiter, $enclosure);
$lines = [];
while (($data = fgetcsv($handle, 0, $delimiter, $enclosure)) !== false) {
$current = [];
$i = 0;
foreach ($headers as $header) {
$current[$header] = $data[$i++];
}
$lines[] = $current;
}
fclose($handle);
return $lines;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment