Skip to content

Instantly share code, notes, and snippets.

@thomas-harding
Created July 12, 2021 11:26
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/5211913c429d0fb6655e4ff7b7934e01 to your computer and use it in GitHub Desktop.
Save thomas-harding/5211913c429d0fb6655e4ff7b7934e01 to your computer and use it in GitHub Desktop.
Refactor Magento 1 Additional Images Into Magento 2 Suitable Format
<?php
/*
* USING EXCEL
* 1) Grab the export from Magento
* 2) Filter the blanks out of "_media_image" column
* 3) Copy columns sku image, small_image, thumbnail, _media_image 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;
}
$csvOut = "sku,media_gallery\n";
const ADDR = "https://your.magento1.website/media/catalog/product"; //Media folder
$csv = readCSV('images.csv'); // Make sure this exists or you will have a forever error log.
//echo "<table><th>SKU</th><th>Conf</th>";
$counter = 0;
$csvArray = array();
array_push($csvArray,array("sku","base_image","small_image","thumbnail_image","additional_images"));
$lastSku = "";
foreach ( $csv as $c ) {
echo "<tr>";
if($counter > 0) {
//$sku = $c[0];
if($c[0] != ""){
$sku = $c[0];
}
$img = ADDR . $c[4]; //Additional Image
//echo $sku . ".....";
//echo $img . "<br /><hr />";
// echo "<td>";
if($sku != $lastSku) { //We're on a new one
array_push($csvArray, array($lastSku, $b_img, $s_img, $t_img, implode(",", $confArray)));
$confArray = array();
}
//$confArray = array();
//need to try and bypass pesky duplicates
if($c[0]!="") { //The additional image is on the same line as the sku
$curr_arr = array($c[1], $c[2], $c[3]);
if (!in_array($c[4], $curr_arr)) {
array_push($confArray, $img); //Push the additional shit
}
}else{
$curr_arr = array($b_img, $s_img, $t_img);
if (!in_array($img, $curr_arr)) {
array_push($confArray, $img); //Push the additional shit
}
}
if($c[1]!= "") { //Base image
$b_img = ADDR . $c[1];
}
if($c[2]!= "") { //Small Image
$s_img = ADDR . $c[2];
}
if($c[3]!= "") { //Thumbnail
$t_img = ADDR . $c[3];
}
$lastSku = $sku;
}
$counter++;
// echo "</tr>";
}
?>
<!--</table>-->
<?php
$csv_handler = fopen ('images_processed.csv','w');
foreach($csvArray AS $toGo){
if($toGo[0] != "") {
fputcsv($csv_handler, $toGo);
}
}
fclose ($csv_handler);
foreach($csvArray AS $toGo){
echo $toGo[0] . "......" . $toGo[1] . "<br />";
}
@thomas-harding
Copy link
Author

Here's my gist for configurable products/super attributes - https://gist.github.com/thomas-harding/b4527534eaf3e3e57c6e1f8be329d973

@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.

I know it's not perfect by any standards (and more experienced PHP coders could create a tool to do everything all in one), but if this saves 1 developer even an hour of their time, then this gist was worth creating.

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