Converting Excel Files for Magento Import
For those forced to convert a client's excel data files into a file that is consistent with Magento's data structure, here's a script we developed that may help:
<?php
error_reporting(E_ALL);
// echo date('mdY'); //main input $input = "/server-path/import/input/company-website-data-2012-07-31b-extract8.csv"; //copy of main input // $input_ii = "/server-path/import/input/input-".date('mdY-His').".csv"; //price input // $input_prc = "/server-path/import/input/stock_inventorywith_7_PL.csv"; $output = "/server-path/import/output/output-new".date('mdY-His').".csv"; // $image_out = "/server-path/import/output/images".date('mdY-His').".csv"; // $price_out = "/server-path/import/output/prices".date('mdY-His').".csv";
$handle = fopen($input,"r+"); // $handle_ii = fopen($input_ii,"w+"); // $handle_iii = fopen($input_prc,"r+"); $handle_output = fopen($output,"w+"); // $handle_img = fopen($image_out,"w+"); // $handle_prc = fopen($price_out,"w+");
// fclose($handle_ii);
//copy input file, this will be used to find child sku's // copy($input, $input_ii);
// function getBasePrice($sku){ // $input_base = "/server-path/import/input/base-pricing.csv"; // $handle_base = fopen($input_base,"r+");
// while (($data = fgetcsv($handle_base, 0, ",")) != false){ // if($data[0] == $sku){ // return $data[3]; // } // }
// fclose($handle_base); // return $result; // }
function getTierPrice($sku, $baseprice){
// $input_prc = "/server-path/import/input/stock_inventorywith_7_PL.csv"; $input_prc = "/server-path/import/input/new-prices08082012-122744.csv"; $handle_prc = fopen($input_prc,"r+");
$result = ''; while (($data = fgetcsv($handle_prc, 0, ",")) != false){ if(strtolower($sku) == strtolower($data[0])){ //customerGROUPID=QTY=PRICE
if(($data[2] != '') and ($data[3] != '')){ if((($data[1] == 1) and ($data[3] != 1)) || (($data[1] != 1) and ($data[2] != $baseprice))){ $result .= $data[1]."=".$data[3]."=".$data[2]."|"; } }
if(($data[4] != '') and ($data[5] != '')){ if((($data[1] == 1) and ($data[5] != 1)) || (($data[1] != 1) and ($data[4] != $baseprice))){ $result .= $data[1]."=".$data[5]."=".$data[4]."|"; } }
if(($data[6] != '') and ($data[7] != '')){ if((($data[1] == 1) and ($data[7] != 1)) || (($data[1] != 1) and ($data[6] != $baseprice))){ $result .= $data[1]."=".$data[7]."=".$data[6]."|"; } }
if(($data[8] != '') and ($data[9] != '')){ if((($data[1] == 1) and ($data[9] != 1)) || (($data[1] != 1) and ($data[8] != $baseprice))){ $result .= $data[1]."=".$data[9]."=".$data[8]."|"; } }
if(($data[10] != '') and ($data[11] != '')){ if((($data[1] == 1) and ($data[11] != 1)) || (($data[1] != 1) and ($data[10] != $baseprice))){ $result .= $data[1]."=".$data[11]."=".$data[10]."|"; } }
} }
fclose($handle_prc);
return $result; }
$header = array("sku", "attribute_set", "type", "store", "websites", "visibility", "status", "categories", "name", "description", "company_shapes", "company_sizing", "company_status", "company_events", "company_gender", "company_qty_per", "company_unit", "company_material", "company_finish", "company_color2", "company_pollybagged", "company_laserable", "company_engravable", "company_colorfill", "company_new", "company_spinner", "company_polydome", "company_includes", "price", "company_shipsfrom", "short_description", "company_free_2d_die", "company_free_setup", "company_express_shipping", "company_production_time", "tax_class_id", "associated", "config_attributes", "image", "thumbnail", "small_image", "gallery", "related", "tier_prices", "company_threeday", "company_keyhole_drilled", "company_adhesice_back", "company_ecofriendly" );
fputcsv($handle_output, $header, ',', '"');
$i = 1; while (($data = fgetcsv($handle, 0, "|")) != false){ if($i > 1){ $sku = (string) $data[0]; $_attribute_set = "Default"; $_type = ($data[6] == '') ? 'simple' : 'configurable'; $store = "admin"; $_product_websites = "base"; $visibility = ($data[5] == '1') ? 'Nowhere' : 'Catalog, Search'; $status = 'Enabled'; $_category = $data[2]; $name = $data[3];
// $description = ($data[4] != '') ? '<p>'.$data[4].'</p>' : "";
$description = '';
//Additional Information if($data[9] != ""){ $description .= "<p><img style=\"vertical-align: middle;\" src=\"/images/company/product-info/".$data[9]."\" alt=\"Additional Information\" /></p>"; }
$company_shapes = ($data[10] != '') ? "<p><img style=\"vertical-align: middle;\" src=\"/images/company/product-info/".$data[10]."\" alt=\"shapes\" /></p>" : ""; $company_sizing = ($data[12] != '') ? "<p><img src=\"/images/company/product-info/sizing.png\" alt=\"size measurement\" width=\"700\" height=\"225\" /></p>" : ""; // $company_status = (strtoupper($data[11]) != 'NO') ? 'Closeout' : ''; $company_status = $data[11]; $company_events = $data[13]; $company_gender = $data[14]; $company_qty_per = ($data[15] != '') ? $data[15] : '1'; $company_unit = ($data[16] != '') ? $data[16] : 'Each'; $company_material = $data[17]; $company_finish = $data[18]; $company_color2 = $data[19]; $company_pollybagged = ($data[20] == 1) ? 'Yes' : 'No'; $company_laserable = ($data[21] == 1) ? 'Yes' : 'No'; $company_engravable = ($data[22] == 1) ? 'Yes' : 'No'; $company_colorfill = $data[23]; $company_new = ($data[24] == 1) ? 'Yes' : 'No'; $company_spinner = ($data[25] == 1) ? 'Yes' : 'No'; $company_polydome = ($data[26] == 1) ? 'Yes' : 'No'; $company_includes = $data[27]; // $price = ($data[28] == '') ? getBasePrice($data[0]) : $data[28]; $price = $data[28]; $company_shipsfrom = $data[34]; // $short_description = ($data[43] == '') ? $data[3] : $data[43];
$short_description = ''; if($data[4] != ''){ $short_description .= $data[4]."<br/>"; }
if($data[38] != ''){ $short_description .= $data[38]; }
//pricing table if($data[8] != ''){ $short_description .= "<p><img style=\"vertical-align: middle;\" src=\"/images/company/product-info/".$data[8]."\" alt=\"Pricing Table\" /></p>"; }
$company_free_2d_die = ($data[39] == 1) ? 'Yes' : 'No'; $company_free_setup = ($data[40] == 1) ? 'Yes' : 'No'; $company_express_shipping = ($data[41] == 1) ? 'Available for Express Service' : 'Unavailable for Express Service'; $company_production_time = $data[42]; $tax_class_id = 'None'; $associated = $data[6]; $config_attributes = strtolower($data[7]); $image = "/".$data[1]; $thumbnail = "/".$data[1]; $small_image = "/".$data[1];
$galery = ""; if($data[35] != ''){ $galery .= "/".$data[35].","; } if($data[36] != ''){ $galery .= "/".$data[36].","; } if($data[37] != ''){ $galery .= "/".$data[37].","; }
$related = ""; if($data[30] != ''){ $related .= $data[30].","; } if($data[31] != ''){ $related .= $data[31].","; } if($data[32] != ''){ $related .= $data[32].","; } if($data[33] != ''){ $related .= $data[33].","; }
$tier_prices = getTierPrice($data[0], $data[28]);
//company_three days $company_threeday = ''; if($data[43] != ''){ $company_threeday = "<p><img style=\"vertical-align: middle;\" src=\"/images/company/product-info/".$data[43]."\" alt=\"Pricing Table\" /></p>"; }
$company_keyhole_drilled = ($data[44] == 1) ? 'Yes' : 'No'; $company_adhesice_back = ($data[45] == 1) ? 'Yes' : 'No'; $company_ecofriendly = ($data[46] == 1) ? 'Yes' : 'No'; $_links_related_sku = strtoupper($data[30]); fputcsv($handle_output,array($sku, $_attribute_set, $_type, $store, $_product_websites, $visibility, $status, $_category, $name, $description, $company_shapes, $company_sizing, $company_status, $company_events, $company_gender, $company_qty_per, $company_unit, $company_material, $company_finish, $company_color2, $company_pollybagged, $company_laserable, $company_engravable, $company_colorfill, $company_new, $company_spinner, $company_polydome, $company_includes, $price, $company_shipsfrom, $short_description, $company_free_2d_die, $company_free_setup, $company_express_shipping, $company_production_time, $tax_class_id, $associated, $config_attributes, $image, $thumbnail, $small_image, $galery, $related, $tier_prices, $company_threeday, $company_keyhole_drilled, $company_adhesice_back, $company_ecofriendly ), ',','"');
// if($data[35] != ''){ // fputcsv($handle_output,array($sku, '', '', '', '', '', '', '', // '', '', '', '', '', '', '', '', // '', '', '', '', '', '', '', '', // '', '', '', '', '', '', '', '', // '', '', '', '', '', '', // "/".$data[35], // "/".$data[35], // "/".$data[35], // '', '' // ), ',','"'); // } // if($data[36] != ''){ // fputcsv($handle_output,array($sku, '', '', '', '', '', '', '', // '', '', '', '', '', '', '', '', // '', '', '', '', '', '', '', '', // '', '', '', '', '', '', '', '', // '', '', '', '', '', '', // "/".$data[36], // "/".$data[36], // "/".$data[36], // '', '' // ), ',','"'); // } // if($data[37] != ''){ // fputcsv($handle_output,array($sku, '', '', '', '', '', '', '', // '', '', '', '', '', '', '', '', // '', '', '', '', '', '', '', '', // '', '', '', '', '', '', '', '', // '', '', '', '', '', '', // "/".$data[37], // "/".$data[37], // "/".$data[37], // '', '' // ), ',','"'); // } } $i++; }
echo "<center><h3>Output file was generated!</h3></center>"; echo "<center><p>Available at: ".$output."</p></center>";
fclose($handle); fclose($handle_output); ?>
Contact us for more information.