Created
July 12, 2021 10:59
-
-
Save thomas-harding/b4527534eaf3e3e57c6e1f8be329d973 to your computer and use it in GitHub Desktop.
Refactor Magento 1 Super Attributes Into Magento 2 Suitable Format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
Here's my gist for additional images https://gist.github.com/thomas-harding/5211913c429d0fb6655e4ff7b7934e01
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.