-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
195 lines (186 loc) · 4.29 KB
/
main.js
File metadata and controls
195 lines (186 loc) · 4.29 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { stringToArray, arrayToString, getImgLink, Btn } from './js/utils.js';
import { variationsHeader } from './js/variations-header.js';
import generateCsv from './js/generate-csv-file.js';
import getCsvData from './js/get-csv-data.js';
import showAlertMsg, { ALERT_TYPE } from './js/alert-msg.js';
const csvFileInput = document.getElementById('csv-file-input');
const form = document.getElementById('form');
const downloadBtn = Btn('downloadBtn');
downloadBtn.disable();
let ID = 1;
const variationsRowsContainer = [];
variationsRowsContainer.push(variationsHeader);
form.addEventListener('submit', () => {
generateCsv(variationsRowsContainer)
});
csvFileInput.addEventListener('change', (event) => {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = (event) => {
const fileData = event.target.result;
const csvData = getCsvData(fileData);
for (let i = 0; i < csvData.length; i++) {
const attrs = getAttributesFromRow(csvData[i]);
const productVariations = generateVariations(attrs);
const csvRows = variationsToRows(productVariations, attrs, csvData[i]);
csvRows.map((row) => {
variationsRowsContainer.push(row);
});
}
downloadBtn.enable();
showAlertMsg(
'The variations have been generated successfully. Click on "Download Variations" button to download the CSV file',
ALERT_TYPE.SUCCESS
)
};
reader.readAsText(file);
});
function getAttributesFromRow(row) {
const attributes = [];
for (let i = 1; i < 10; i++) {
const attrName = `AttributeName${i}`;
const attrVals = `AttributeValues${i}`;
if (row[attrName]) {
const newAttribute = {
attributeName: row[attrName],
attributeValues: stringToArray(row[attrVals]),
};
attributes.push(newAttribute);
} else {
break;
}
}
return attributes;
}
function generateVariations(attributes) {
const keys = attributes.map((attr) => attr.attributeName);
const values = attributes.map((attr) => attr.attributeValues);
const cartesianProduct = (...arrays) =>
arrays.reduce(
(acc, arr) => acc.flatMap((x) => arr.map((y) => [...x, y])),
[[]]
);
const variations = cartesianProduct(...values).map((productVariation) =>
keys.reduce(
(acc, key, i) => ({
...acc,
[key]: productVariation[i],
}),
{}
)
);
return variations;
}
function variationsToRows(variations, attributes, parentData) {
let variationsRows = [];
let i = 1;
variations.map((variation) => {
let row = [
'',
'variation',
`${parentData['SKU']}-${i}`,
`${parentData['Name']}`,
'1',
'0',
'visible',
`${parentData['Short description']}`,
`${parentData['Description']}`,
'',
'',
'taxable',
'parent',
'1',
'',
'',
'0',
'0',
'',
'',
'',
'',
'0',
'',
'',
`${parentData['Price']}`,
'',
'',
'',
getImgLink(parentData['ImageLink'], variation.Color),
'',
'',
'',
'',
'',
'',
'',
'',
'',
];
for (const [key, value] of Object.entries(variation)) {
row = row.concat([key, value, '', '1']);
}
variationsRows.push(row);
i++;
});
variationsRows.unshift(generateParentRow(attributes, parentData));
variationsRows.map((row) => {
row[0] = ID;
row[38] = ID;
row[16] = '1';
ID++;
if (variationsRows.indexOf(row) > 0) {
row[32] = variationsRows[0][2];
}
});
return variationsRows;
}
function generateParentRow(attributes, rowData) {
let parentRow = [
`${rowData['SKU']}`,
'variable',
`${rowData['SKU']}`,
`${rowData['Name']}`,
'1',
'0',
'visible',
`${rowData['Short description']}`,
`${rowData['Description']}`,
'',
'',
'taxable',
'',
'1',
'',
'',
'0',
'0',
'',
'',
'',
'',
'1',
'',
'',
'',
`${rowData['Categories']}`,
'',
'',
`${rowData['ImageLink']}`,
'',
'',
'',
'',
'',
'',
'',
'',
'',
];
const newCols = attributes.map((attr) => [
attr.attributeName,
`"${arrayToString(attr.attributeValues)}"`,
'1',
'1',
]);
return parentRow.concat(newCols).flat();
}