-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix_products_csv_import.php
101 lines (83 loc) · 2.46 KB
/
fix_products_csv_import.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
function findDuplicatesAndSaveCSV($filename, $columnIndex, $manual_url_key_row_numbers) {
// Check if file exists
if (!file_exists($filename)) {
echo "File not found.";
return;
}
// Open CSV file for reading
$file = fopen($filename, 'r');
if (!$file) {
echo "Error opening file.";
return;
}
// Initialize array to store data from CSV
$data = [];
$url_keys = [];
$unset_url_key_skus = [];
$headers = fgetcsv($file);
// Read CSV file line by line and parse into array
while (($row = fgetcsv($file)) !== false) {
// Check if column index is valid
if ($columnIndex < 0 || $columnIndex >= count($row)) {
echo "Invalid column index.";
fclose($file);
return;
}
$data[] = $row;
//Search duplicates
if ($row[17] !== '' && array_search($row[17], $url_keys, true)) {
$unset_url_key_skus[] = $row[0];
}
$url_keys[] = trim($row[17]);
}
foreach ($data as $index => $datum) {
//TMP Fix for products with the same names
if (in_array($index+1, $manual_url_key_row_numbers)) {
$data[$index][17] .= '-' . $index;
continue;
}
if (in_array($datum[0], $unset_url_key_skus, true)) {
$data[$index][17] = '';
}
}
// Close file
fclose($file);
// Save to new CSV file
if (!empty($unset_url_key_skus)) {
$duplicateFilename = 'fixed_url_duplicates.csv';
$duplicateFile = fopen($duplicateFilename, 'w');
fputcsv($duplicateFile, $headers);
// Write duplicates to file
foreach ($data as $fixed_row) {
fputcsv($duplicateFile, $fixed_row);
}
fclose($duplicateFile);
echo "saved to $duplicateFilename.";
} else {
echo "No duplicates found.";
}
}
// Usage example
$filename = 'data.csv'; // Change this to your CSV file's path
$columnIndex = 0; // Change this to the index of the column you want to check for duplicates
//TMP workaround. It should be done via Magento Plugin for Url_Key Generator.
$manual_url_key_row_numbers = [
129,
154,
238, 368,
384,
539, 542, 545, 548, 551, 554,
540, 543, 546, 549, 552, 555,
627,
628,
682, 690, 827, 831,
826, 830,
871,
872,
883,
972,
985,
1187
];
findDuplicatesAndSaveCSV($filename, $columnIndex, $manual_url_key_row_numbers);