Skip to content

Instantly share code, notes, and snippets.

@thomas-harding
Created July 12, 2021 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomas-harding/b4527534eaf3e3e57c6e1f8be329d973 to your computer and use it in GitHub Desktop.
Save thomas-harding/b4527534eaf3e3e57c6e1f8be329d973 to your computer and use it in GitHub Desktop.
Refactor Magento 1 Super Attributes Into Magento 2 Suitable Format
<?php
/*
* USING EXCEL
* 1) Grab the export from Magento
* 2) Filter the blanks out of "Super Attributes SKU" column
* 3) Copy columns sku _super_products_sku _super_attribute_code _super_attribute_option into new sheet
* 4) Save it in same directory as this script and run.
* 5) Refactored feed will be generated.
*/
?>
<style>
td {
border-bottom: 1px solid #ccc;}
td.found {background-color:#00ff00;}
td.nofound {background-color:#ff0000;}
</style>
<?php
function readCSV($csvFile){
$file_handle = fopen($csvFile, 'r');
while (!feof($file_handle) ) {
$line_of_text[] = fgetcsv($file_handle, 0);
}
fclose($file_handle);
return $line_of_text;
}
function _group_by($array, $key) {
$return = array();
foreach($array as $val) {
$return[$val[$key]][] = $val;
}
return $return;
}
function recombobulate($array){
$skuFull = array();
foreach ($array as $row){ //Each sku
//var_dump($row);
$skuOptions = array();
foreach($row AS $options){ //Each sku's options
//var_dump($options);
//Take SKU and use as key
//Take code and opt, put them into array.
array_push($skuOptions,$options["code"] . "=\"" . $options["opt"] . "\"");
$sku = "sku=" . $options["sku"];
}
array_unshift($skuOptions,$sku);
//Make sure to set semicolon as the "Multiple Value Separator" in firebear, otherwise the sku won't be picked up.
$optionsCombined = implode(";",$skuOptions);
array_push($skuFull,$optionsCombined);
}
return $skuFull;
}
$csvOut = "sku,attribute_set_code,configurable_variations\n";
$csv = readCSV('feed.csv');
// Unquote echos to show in tabular format while running
//echo "<table><th>SKU</th><th>Conf</th>";
$counter = 0;
$csvArray = array();
array_push($csvArray,array("sku","attribute_set_code","configurable_variations"));
$lastSku = "";
foreach ( $csv as $c ) {
if($counter > 0) {
// echo "<tr>";
if($c[0] != ""){
$sku = $c[0];
}
$supSKU = $c[1];
$supCode = $c[2];
$supOpt = $c[3];
// echo "<td>" . $sku . "</td>";
// echo "<td>";
if($sku != $lastSku) { //Start New Product
$grouped = _group_by($confArray, 'sku');
$combined = recombobulate($grouped);
var_dump($combined);
array_push($csvArray, array($lastSku, "Default", implode("|", $combined)));
$confArray = array();
}
//We need to push $supSKU to a key in array and any subsequent values go into that.
$confRecord = array(
"sku" => $supSKU,
"code" => $supCode,
"opt" => $supOpt
);
array_push($confArray, $confRecord);
$lastSku = $sku;
}
$counter++;
}
?>
<!--</table>-->
<?php
$csv_handler = fopen ('refactored.csv','w');
foreach($csvArray AS $toGo){
fputcsv($csv_handler,$toGo);
}
fclose ($csv_handler);
@thomas-harding
Copy link
Author

I wanted to paste this here because it could be very useful to those who need to migrate Magento 1 products without using the migration tool. I usually use Firebear Import/Export for this purpose, but it might be useful for other platforms.

@thomas-harding
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment