|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* allows the creation of variations from a CSV |
|
6
|
|
|
* CSV will have the following fields: |
|
7
|
|
|
* ProductTitle, |
|
8
|
|
|
* Size, |
|
9
|
|
|
* Colour, |
|
10
|
|
|
* Price |
|
11
|
|
|
* If you like to add more fields, then it is recommended that you extend this BuildTask |
|
12
|
|
|
* to your own BuildTask. |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
class EcommerceTaskCSVToVariations extends BuildTask |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
protected $forreal = false; |
|
19
|
|
|
|
|
20
|
|
|
protected $title = "Create variations from a Spreadsheets (comma separated file CSV)"; |
|
21
|
|
|
|
|
22
|
|
|
protected $description = " |
|
23
|
|
|
Does not delete any record, it only updates and adds. |
|
24
|
|
|
The minimum recommend columns are: ProductTitle (or ProductInternalItemID), Size, Colour, Price, InternalItemID. |
|
25
|
|
|
You can add ?forreal=1 to the URL to run the task for real."; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* excluding base folder |
|
29
|
|
|
* |
|
30
|
|
|
* e.g. assets/files/mycsv.csv |
|
31
|
|
|
* @var String |
|
32
|
|
|
*/ |
|
33
|
|
|
private static $file_location = ""; |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Cell entry for a price that is not available |
|
37
|
|
|
* @var String |
|
38
|
|
|
*/ |
|
39
|
|
|
private static $no_price_available = "POA"; |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var Array |
|
43
|
|
|
*/ |
|
44
|
|
|
private static $attribute_type_field_names = array( |
|
|
|
|
|
|
45
|
|
|
"Size", |
|
46
|
|
|
"Colour" |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Is the CSV separated by , or ; or [tab]? |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $csvSeparator = ","; |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var Boolean |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $debug = true; |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* the original data from the CVS |
|
63
|
|
|
* @var Array |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $csv = array(); |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Structure will be as follows: |
|
69
|
|
|
* |
|
70
|
|
|
* ProductID => array( |
|
71
|
|
|
* "Product" => $product, |
|
72
|
|
|
* "VariationRows" => array( |
|
73
|
|
|
* [1] => array( |
|
74
|
|
|
* "Data" => array(), |
|
75
|
|
|
* "Variation" => $variation |
|
76
|
|
|
* ) |
|
77
|
|
|
* ) |
|
78
|
|
|
* ), |
|
79
|
|
|
* ProductID => array( |
|
80
|
|
|
* "Product" => $product, |
|
81
|
|
|
* "VariationRows" => array( |
|
82
|
|
|
* [1] => array( |
|
83
|
|
|
* "Data" => array(), |
|
84
|
|
|
* "Variation" => $variation |
|
85
|
|
|
* ), |
|
86
|
|
|
* [2] => array( |
|
87
|
|
|
* "Data" => array(), |
|
88
|
|
|
* "Variation" => $variation |
|
89
|
|
|
* ) |
|
90
|
|
|
* ) |
|
91
|
|
|
* ) |
|
92
|
|
|
* |
|
93
|
|
|
* @var Array |
|
94
|
|
|
*/ |
|
95
|
|
|
protected $data = array(); |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* list of products without variations |
|
99
|
|
|
* @return Array |
|
100
|
|
|
*/ |
|
101
|
|
|
protected $soleProduct = array(); |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* The default page of where the products are added. |
|
105
|
|
|
* @var Int |
|
106
|
|
|
*/ |
|
107
|
|
|
protected $defaultProductParentID = 0; |
|
108
|
|
|
|
|
109
|
|
|
public function getDescription() |
|
110
|
|
|
{ |
|
111
|
|
|
if ($this->csvSeparator == "\t") { |
|
112
|
|
|
$this->csvSeparatorName = "[TAB]"; |
|
|
|
|
|
|
113
|
|
|
} else { |
|
114
|
|
|
$this->csvSeparatorName = $this->csvSeparator; |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
return $this->description .". The file to be used is: ".$this->Config()->get("file_location").". The columns need to be separated by '".$this->csvSeparatorName."'"; |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* |
|
121
|
|
|
*/ |
|
122
|
|
|
public function run($request) |
|
|
|
|
|
|
123
|
|
|
{ |
|
124
|
|
|
increase_time_limit_to(3600); |
|
125
|
|
|
increase_memory_limit_to('512M'); |
|
126
|
|
|
if ($request->param("forreal") || (isset($_GET["forreal"]) && $_GET["forreal"] == 1)) { |
|
127
|
|
|
$this->forreal = true; |
|
128
|
|
|
} |
|
129
|
|
|
if ($this->forreal) { |
|
130
|
|
|
$this->reset(); |
|
131
|
|
|
} |
|
132
|
|
|
$this->readFile(); |
|
133
|
|
|
$this->createProducts(); |
|
134
|
|
|
$this->findVariations(); |
|
135
|
|
|
if ($this->forreal) { |
|
136
|
|
|
$this->createVariations(); |
|
137
|
|
|
$this->getExtraDataForVariations(); |
|
138
|
|
|
} else { |
|
139
|
|
|
$this->showData(); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* do more with Product |
|
145
|
|
|
* @param Product $product |
|
146
|
|
|
* @param Array $row |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function addMoreProduct($product, $row) |
|
|
|
|
|
|
149
|
|
|
{ |
|
150
|
|
|
//overwrite in an extension of this task |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* do more with Product that does have any variations |
|
155
|
|
|
* @param Product $product |
|
156
|
|
|
* @param Array $row |
|
157
|
|
|
*/ |
|
158
|
|
|
protected function addMoreProductForProductWithoutVariations($product, $row) |
|
|
|
|
|
|
159
|
|
|
{ |
|
160
|
|
|
//overwrite in an extension of this task |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* do more with Product Variation |
|
165
|
|
|
* @param ProductAttributeType $attributeType |
|
166
|
|
|
* @param String $fieldName |
|
167
|
|
|
* @param Product $product |
|
168
|
|
|
*/ |
|
169
|
|
|
protected function addMoreAttributeType($attributeType, $fieldName, $product) |
|
|
|
|
|
|
170
|
|
|
{ |
|
171
|
|
|
//overwrite in an extension of this task |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* do more with Product Variation |
|
176
|
|
|
* @param ProductAttributeType $attributeValue |
|
177
|
|
|
* @param ProductAttributeType $attributeType |
|
178
|
|
|
* @param Product $product |
|
179
|
|
|
*/ |
|
180
|
|
|
protected function addMoreToAttributeValue($attributeValue, $attributeType, $product) |
|
|
|
|
|
|
181
|
|
|
{ |
|
182
|
|
|
//overwrite in an extension of this task |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* do more with Product Variation |
|
187
|
|
|
* @param ProductVariation $variation |
|
188
|
|
|
* @param Array $variationData |
|
189
|
|
|
* @param Product $product |
|
190
|
|
|
*/ |
|
191
|
|
|
protected function addMoreToVariation($variation, $variationData, $product) |
|
|
|
|
|
|
192
|
|
|
{ |
|
193
|
|
|
//overwrite in an extension of this task |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
protected function reset() |
|
198
|
|
|
{ |
|
199
|
|
|
//to do... |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
protected function readFile() |
|
203
|
|
|
{ |
|
204
|
|
|
echo "================================================ READING FILE ================================================"; |
|
205
|
|
|
$this->alterationMessage("<h3>".$this->getDescription()."</h3>", "created"); |
|
206
|
|
|
$rowCount = 1; |
|
207
|
|
|
$rows = array(); |
|
208
|
|
|
$fileLocation = $this->config()->get("file_location"); |
|
209
|
|
|
$this->alterationMessage("$fileLocation is the file we are reading", "created"); |
|
210
|
|
|
if (($handle = fopen($fileLocation, "r")) !== false) { |
|
211
|
|
|
while (($data = fgetcsv($handle, 100000, $this->csvSeparator)) !== false) { |
|
212
|
|
|
$rows[] = $data; |
|
213
|
|
|
$rowCount++; |
|
214
|
|
|
} |
|
215
|
|
|
fclose($handle); |
|
216
|
|
|
} |
|
217
|
|
|
//$rows = str_getcsv(file_get_contents(, ",", '"'); |
|
|
|
|
|
|
218
|
|
|
|
|
219
|
|
|
$header = array_shift($rows); |
|
220
|
|
|
|
|
221
|
|
|
$this->csv = array(); |
|
222
|
|
|
$rowCount = 1; |
|
223
|
|
|
foreach ($rows as $row) { |
|
224
|
|
|
if (count($header) != count($row)) { |
|
225
|
|
|
$this->alterationMessage("I am trying to merge ".implode(", ", $header)." with ".implode(", ", $row)." but the column count does not match!", "deleted"); |
|
226
|
|
|
die("STOPPED"); |
|
|
|
|
|
|
227
|
|
|
} |
|
228
|
|
|
$this->csv[] = array_combine($header, $row); |
|
229
|
|
|
$rowCount++; |
|
230
|
|
|
} |
|
231
|
|
|
//data fixes |
|
232
|
|
|
foreach ($this->csv as $key => $row) { |
|
233
|
|
|
if (!isset($row["ProductTitle"])) { |
|
234
|
|
|
$this->csv[$key]["ProductTitle"] = ""; |
|
235
|
|
|
} |
|
236
|
|
|
if (!isset($row["ProductInternalItemID"])) { |
|
237
|
|
|
$this->csv[$key]["ProductInternalItemID"] = $row["ProductTitle"]; |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
$this->alterationMessage("Imported ".count($this->csv)." rows with ".count($header)." cells each"); |
|
241
|
|
|
$this->alterationMessage("Fields are: ".implode("<br /> - ............ ", $header)); |
|
242
|
|
|
$this->alterationMessage("================================================", "show"); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* |
|
247
|
|
|
* |
|
248
|
|
|
*/ |
|
249
|
|
|
protected function createProducts() |
|
250
|
|
|
{ |
|
251
|
|
|
$this->alterationMessage("================================================ CREATING PRODUCTS ================================================", "show"); |
|
252
|
|
|
$productsCompleted = array(); |
|
253
|
|
|
foreach ($this->csv as $row) { |
|
254
|
|
|
if (!isset($productsCompleted[$row["ProductTitle"]])) { |
|
255
|
|
|
$filterArray = array( |
|
256
|
|
|
"Title" => $row["ProductTitle"], |
|
257
|
|
|
"InternalItemID" => $row["ProductInternalItemID"] |
|
258
|
|
|
); |
|
259
|
|
|
$product = DataObject::get_one( |
|
260
|
|
|
'ProductPage', |
|
261
|
|
|
$filterArray, |
|
262
|
|
|
$cacheDataObjectGetOne = false |
|
263
|
|
|
); |
|
264
|
|
|
if ($product && $product->ParentID) { |
|
265
|
|
|
$this->defaultProductParentID = $product->ParentID; |
|
266
|
|
|
} elseif (!$this->defaultProductParentID) { |
|
267
|
|
|
$this->defaultProductParentID = DataObject::get_one( |
|
268
|
|
|
'ProductGroup', |
|
269
|
|
|
null, |
|
270
|
|
|
$cacheDataObjectGetOne = false |
|
271
|
|
|
)->ID; |
|
272
|
|
|
} |
|
273
|
|
|
if (!$product) { |
|
274
|
|
|
$product = ProductPage::create($filterArray); |
|
275
|
|
|
$product->MenuTitle = $row["ProductTitle"]; |
|
276
|
|
|
|
|
277
|
|
|
$this->alterationMessage("Creating Product: ".$row["ProductTitle"], "created"); |
|
278
|
|
|
} else { |
|
279
|
|
|
$this->alterationMessage("Product: ".$row["ProductTitle"]." already exists"); |
|
280
|
|
|
} |
|
281
|
|
|
if (!$product->ParentID) { |
|
282
|
|
|
$product->ParentID = $this->defaultProductParentID; |
|
283
|
|
|
} |
|
284
|
|
|
$product->Title = $row["ProductTitle"]; |
|
285
|
|
|
$product->InternalItemID = $row["ProductInternalItemID"]; |
|
286
|
|
View Code Duplication |
if ($this->forreal) { |
|
|
|
|
|
|
287
|
|
|
$this->addMoreProduct($product, $row); |
|
288
|
|
|
$product->write("Stage"); |
|
289
|
|
|
if ($product->IsPublished()) { |
|
290
|
|
|
$product->Publish('Stage', 'Live'); |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
$productsCompleted[$row["ProductTitle"]] = $product->ID; |
|
294
|
|
|
$this->data[$product->ID] = array( |
|
295
|
|
|
"Product" => $product, |
|
296
|
|
|
"VariationRows" => array() |
|
297
|
|
|
); |
|
298
|
|
|
} |
|
299
|
|
|
} |
|
300
|
|
|
$this->alterationMessage("================================================", "show"); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
|
|
304
|
|
|
protected function findVariations() |
|
305
|
|
|
{ |
|
306
|
|
|
$this->alterationMessage("================================================ FINDING VARIATIONS ================================================", "show"); |
|
307
|
|
|
foreach ($this->data as $productKey => $data) { |
|
308
|
|
|
$product = $data["Product"]; |
|
309
|
|
|
$title = $product->Title; |
|
310
|
|
|
$internalItemID = $product->InternalItemID; |
|
311
|
|
|
foreach ($this->csv as $key => $row) { |
|
312
|
|
|
if (strtolower(trim($title)) == strtolower(trim($row["ProductTitle"])) || strtolower(trim($internalItemID)) == strtolower(trim($row["ProductInternalItemID"]))) { |
|
313
|
|
|
$this->data[$product->ID]["VariationRows"][$key] = array( |
|
314
|
|
|
"Data" => $row, |
|
315
|
|
|
"Variation" => null |
|
316
|
|
|
); |
|
317
|
|
|
} |
|
318
|
|
|
} |
|
319
|
|
|
if (count($this->data[$product->ID]["VariationRows"]) < 2) { |
|
320
|
|
|
$varData = array_shift($this->data[$product->ID]["VariationRows"]); |
|
321
|
|
|
$varDataRow = $varData["Data"]; |
|
322
|
|
|
$this->addFieldToObject($product, $data, "Price", ""); |
|
323
|
|
|
$this->addFieldToObject($product, $data, "InternalItemID", ""); |
|
324
|
|
View Code Duplication |
if ($this->forreal) { |
|
|
|
|
|
|
325
|
|
|
$this->addMoreProductForProductWithoutVariations($product, $varDataRow); |
|
326
|
|
|
$product->write("Stage"); |
|
327
|
|
|
if ($product->IsPublished()) { |
|
328
|
|
|
$product->Publish('Stage', 'Live'); |
|
329
|
|
|
} |
|
330
|
|
|
} |
|
331
|
|
|
$this->soleProduct[$product->ID] = $product->Title.", ID: ".$product->ID; |
|
332
|
|
|
unset($this->data[$productKey]); |
|
333
|
|
|
$this->alterationMessage("Removing data for ".$product->Title." because there is only ONE variation. ", "deleted"); |
|
334
|
|
|
} else { |
|
335
|
|
|
$this->alterationMessage("Found ".count($this->data[$product->ID]["VariationRows"])." Variations for ".$product->Title); |
|
336
|
|
|
} |
|
337
|
|
|
} |
|
338
|
|
|
$this->alterationMessage("================================================", "show"); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
protected function showData() |
|
342
|
|
|
{ |
|
343
|
|
|
echo "<h2>Variation Summary</h2>"; |
|
344
|
|
|
foreach ($this->data as $productKey => $value) { |
|
345
|
|
|
if (isset($value["Product"]) && $value["Product"]) { |
|
346
|
|
|
$this->data[$productKey]["Product"] = $value["Product"]->Title.", ID: ".$value["Product"]->ID; |
|
347
|
|
|
} else { |
|
348
|
|
|
$this->data[$productKey]["Product"] = "Not found"; |
|
349
|
|
|
} |
|
350
|
|
|
$this->alterationMessage($this->data[$productKey]["Product"].", variations: ".count($this->data[$productKey]["VariationRows"]), "created"); |
|
351
|
|
|
} |
|
352
|
|
|
echo "<h2>Products without variations</h2>"; |
|
353
|
|
|
foreach ($this->soleProduct as $productKey => $value) { |
|
354
|
|
|
$this->alterationMessage($value, "created"); |
|
355
|
|
|
} |
|
356
|
|
|
echo "<h2>Variation data</h2>"; |
|
357
|
|
|
echo "<pre>"; |
|
358
|
|
|
print_r($this->data); |
|
359
|
|
|
echo "</pre>"; |
|
360
|
|
|
echo "<h2>CSV Data</h2>"; |
|
361
|
|
|
echo "<pre>"; |
|
362
|
|
|
print_r($this->csv); |
|
363
|
|
|
echo "</pre>"; |
|
364
|
|
|
die("====================================================== STOPPED - add ?forreal=1 to run for real. ======================================"); |
|
|
|
|
|
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
protected function createVariations() |
|
368
|
|
|
{ |
|
369
|
|
|
$this->alterationMessage("================================================ CREATING VARIATIONS ================================================", "show"); |
|
370
|
|
|
foreach ($this->data as $data) { |
|
371
|
|
|
$types = array(); |
|
372
|
|
|
$values = array(); |
|
373
|
|
|
$product = $data["Product"]; |
|
374
|
|
|
$arrayForCreation = array(); |
|
375
|
|
|
$variationFilter = array(); |
|
|
|
|
|
|
376
|
|
|
$this->alterationMessage("<h1>Working out variations for ".$product->Title."</h1>"); |
|
377
|
|
|
//create attribute types for one product |
|
378
|
|
|
$this->alterationMessage("....Creating attribute types"); |
|
379
|
|
|
foreach ($this->Config()->get("attribute_type_field_names") as $fieldKey => $fieldName) { |
|
380
|
|
|
$startMessage = "........Checking field $fieldName"; |
|
381
|
|
|
$attributeTypeName = trim($data["Product"]->Title)."_".$fieldName; |
|
382
|
|
|
$filterArray = array("Name" => $attributeTypeName); |
|
383
|
|
|
$type = DataObject::get_one( |
|
384
|
|
|
'ProductAttributeType', |
|
385
|
|
|
$filterArray, |
|
386
|
|
|
$cacheDataObjectGetOne = false |
|
387
|
|
|
); |
|
388
|
|
|
if (!$type) { |
|
389
|
|
|
$this->alterationMessage($startMessage." ... creating new attribute type: ".$attributeTypeName, "created"); |
|
390
|
|
|
$type = ProductAttributeType::create($filterArray); |
|
391
|
|
|
$type->Label = $attributeTypeName; |
|
|
|
|
|
|
392
|
|
|
$type->Sort = $fieldKey; |
|
|
|
|
|
|
393
|
|
|
} else { |
|
394
|
|
|
$this->alterationMessage($startMessage." ... found existing attribute type: ".$attributeTypeName); |
|
395
|
|
|
} |
|
396
|
|
|
$this->addMoreAttributeType($type, $fieldName, $product); |
|
|
|
|
|
|
397
|
|
|
$type->write(); |
|
398
|
|
|
$types[$fieldName] = $type; |
|
399
|
|
|
$product->VariationAttributes()->add($type); |
|
400
|
|
|
} |
|
401
|
|
|
//go through each variation to make the values |
|
402
|
|
|
$this->alterationMessage("....Creating attribute values"); |
|
403
|
|
|
foreach ($data["VariationRows"] as $key => $row) { |
|
404
|
|
|
//go through each value |
|
405
|
|
|
foreach ($this->Config()->get("attribute_type_field_names") as $fieldName) { |
|
406
|
|
|
if (!isset($row["Data"][$fieldName])) { |
|
407
|
|
|
$this->alterationMessage("ERROR; $fieldName not set at all....", "deleted"); |
|
408
|
|
|
continue; |
|
409
|
|
|
} elseif (!trim($row["Data"][$fieldName])) { |
|
410
|
|
|
$this->alterationMessage("skipping $fieldName as there are no entries..."); |
|
411
|
|
|
continue; |
|
412
|
|
|
} |
|
413
|
|
|
$startMessage = "........Checking field $fieldName"; |
|
414
|
|
|
//create attribute value |
|
415
|
|
|
$attributeValueName = $row["Data"][$fieldName]; |
|
416
|
|
|
$filterArray = array("Code" => $attributeValueName, "TypeID" => $types[$fieldName]->ID); |
|
417
|
|
|
$value = DataObject::get_one( |
|
418
|
|
|
'ProductAttributeValue', |
|
419
|
|
|
$filterArray, |
|
420
|
|
|
$cacheDataObjectGetOne = false |
|
421
|
|
|
); |
|
422
|
|
|
if (!$value) { |
|
423
|
|
|
$this->alterationMessage($startMessage."............creating new attribute value: <strong>".$attributeValueName."</strong> for ".$types[$fieldName]->Name, "created"); |
|
424
|
|
|
$value = ProductAttributeValue::create($filterArray); |
|
425
|
|
|
$value->Code = $attributeValueName; |
|
|
|
|
|
|
426
|
|
|
$value->Value = $attributeValueName; |
|
|
|
|
|
|
427
|
|
|
} else { |
|
428
|
|
|
$this->alterationMessage($startMessage."............found existing attribute value: <strong>".$attributeValueName."</strong> for ".$types[$fieldName]->Name); |
|
429
|
|
|
} |
|
430
|
|
|
$this->addMoreAttributeType($value, $types[$fieldName], $product); |
|
|
|
|
|
|
431
|
|
|
$value->write(); |
|
432
|
|
|
$values[$fieldName] = $value; |
|
433
|
|
|
|
|
434
|
|
|
//add at arrays for creation... |
|
435
|
|
|
if (!isset($arrayForCreation[$types[$fieldName]->ID])) { |
|
436
|
|
|
$arrayForCreation[$types[$fieldName]->ID] = array(); |
|
437
|
|
|
} |
|
438
|
|
|
$arrayForCreation[$types[$fieldName]->ID][] = $value->ID; |
|
439
|
|
|
if (!isset($variationFilters[$key])) { |
|
440
|
|
|
$variationFilters[$key] = array(); |
|
|
|
|
|
|
441
|
|
|
} |
|
442
|
|
|
$variationFilters[$key][$types[$fieldName]->ID] = $value->ID; |
|
|
|
|
|
|
443
|
|
|
} |
|
444
|
|
|
} |
|
445
|
|
|
//remove attribute types without values... (i.e. product only has size of colour) |
|
446
|
|
|
foreach ($product->VariationAttributes() as $productTypeToBeDeleted) { |
|
447
|
|
|
if ($productTypeToBeDeleted->Values()->count() == 0) { |
|
448
|
|
|
$this->alterationMessage("....deleting attribute type with no values: ".$productTypeToBeDeleted->Title); |
|
449
|
|
|
$product->VariationAttributes()->remove($productTypeToBeDeleted); |
|
450
|
|
|
} |
|
451
|
|
|
} |
|
452
|
|
|
$this->alterationMessage("....Creating Variations ///"); |
|
453
|
|
|
//$this->alterationMessage("....Creating Variations From: ".print_r(array_walk($arrayForCreation, array($this, 'implodeWalk')))); |
|
|
|
|
|
|
454
|
|
|
//generate variations |
|
455
|
|
|
$variationAttributeValuesPerVariation = array(); |
|
456
|
|
|
foreach ($arrayForCreation as $typeID => $variationEntry) { |
|
457
|
|
|
foreach ($variationEntry as $positionOfVariation => $attributeValueID) { |
|
458
|
|
|
$variationAttributeValuesPerVariation[$positionOfVariation][$typeID] = $attributeValueID; |
|
459
|
|
|
} |
|
460
|
|
|
} |
|
461
|
|
|
|
|
462
|
|
|
foreach ($variationAttributeValuesPerVariation as $variationAttributes) { |
|
463
|
|
|
$variation = $product->getVariationByAttributes($variationAttributes); |
|
464
|
|
|
if ($variation instanceof ProductVariation) { |
|
465
|
|
|
$this->alterationMessage(".... Variation " . $variation->FullName . " Already Exists ///"); |
|
|
|
|
|
|
466
|
|
|
} else { |
|
467
|
|
|
//2. if not, create variation with attributes |
|
468
|
|
|
$className = $product->getClassNameOfVariations(); |
|
469
|
|
|
$newVariation = new $className( |
|
470
|
|
|
array( |
|
471
|
|
|
'ProductID' => $product->ID, |
|
472
|
|
|
'Price' => $product->Price |
|
473
|
|
|
) |
|
474
|
|
|
); |
|
475
|
|
|
$newVariation->setSaveParentProduct(false); |
|
476
|
|
|
$newVariation->write(); |
|
477
|
|
|
$newVariation->AttributeValues()->addMany($variationAttributes); |
|
478
|
|
|
$this->alterationMessage(".... Variation " . $newVariation->FullName . " created ///", "created"); |
|
479
|
|
|
} |
|
480
|
|
|
} |
|
481
|
|
|
|
|
482
|
|
|
//find variations and add to VariationsRows |
|
483
|
|
|
foreach ($data["VariationRows"] as $key => $row) { |
|
484
|
|
|
$variation = $product->getVariationByAttributes($variationFilters[$key]); |
|
485
|
|
|
if ($variation instanceof ProductVariation) { |
|
486
|
|
|
$this->alterationMessage("........Created variation, ".$variation->getTitle()); |
|
487
|
|
|
$this->data[$product->ID]["VariationRows"][$key]["Variation"] = $variation; |
|
488
|
|
|
} else { |
|
489
|
|
|
$this->alterationMessage("........Could not find variation", "deleted"); |
|
490
|
|
|
} |
|
491
|
|
|
} |
|
492
|
|
|
} |
|
493
|
|
|
$this->alterationMessage("================================================", "show"); |
|
494
|
|
|
} |
|
495
|
|
|
|
|
496
|
|
|
protected function getExtraDataForVariations() |
|
497
|
|
|
{ |
|
498
|
|
|
$this->alterationMessage("================================================ ADDING EXTRA DATA ================================================", "show"); |
|
499
|
|
|
foreach ($this->data as $productData) { |
|
500
|
|
|
$product = $productData["Product"]; |
|
501
|
|
|
$this->alterationMessage("<h1>Adding extra data for ".$product->Title." with ".(count($productData["VariationRows"]))."</h1>"." Variations"); |
|
502
|
|
|
foreach ($productData["VariationRows"] as $key => $row) { |
|
503
|
|
|
$variation = $row["Variation"]; |
|
504
|
|
|
$variationData = $row["Data"]; |
|
505
|
|
|
if ($variation instanceof ProductVariation) { |
|
506
|
|
|
$this->alterationMessage("<h3>....Updating ".$variation->getTitle()."</h3>", "show"); |
|
507
|
|
|
if (isset($variationData["Price"])) { |
|
508
|
|
|
if ($price = floatval($variationData["Price"]) - 0) { |
|
509
|
|
|
if (floatval($variation->Price) != floatval($price)) { |
|
|
|
|
|
|
510
|
|
|
$this->alterationMessage("........Price = ".$price, "created"); |
|
511
|
|
|
$variation->Price = $price; |
|
|
|
|
|
|
512
|
|
|
} |
|
513
|
|
|
} else { |
|
514
|
|
|
$this->alterationMessage("........NO Price", "deleted"); |
|
515
|
|
|
} |
|
516
|
|
|
} else { |
|
517
|
|
|
$this->alterationMessage("........NO Price field", "deleted"); |
|
518
|
|
|
} |
|
519
|
|
|
$this->addFieldToObject($variation, $variationData, "Price", ""); |
|
520
|
|
|
$this->addFieldToObject($variation, $variationData, "InternalItemID", ""); |
|
521
|
|
|
$this->addMoreToVariation($variation, $variationData, $product); |
|
522
|
|
|
$variation->write(); |
|
523
|
|
|
} else { |
|
524
|
|
|
$this->alterationMessage("....Could not find variation for ".print_r($row), "deleted"); |
|
525
|
|
|
} |
|
526
|
|
|
} |
|
527
|
|
|
} |
|
528
|
|
|
$this->alterationMessage("================================================", "show"); |
|
529
|
|
|
} |
|
530
|
|
|
|
|
531
|
|
|
/** |
|
532
|
|
|
* adds a field to the variation |
|
533
|
|
|
* @param ProductVariation | Product $variation |
|
534
|
|
|
* @param array $variationData - the array of data |
|
535
|
|
|
* @param String $objectField - the name of the field on the variation itself |
|
536
|
|
|
* @param String $arrayField - the name of the field in the variationData |
|
537
|
|
|
* |
|
538
|
|
|
*/ |
|
539
|
|
|
protected function addFieldToObject($variation, $variationData, $objectField, $arrayField = "") |
|
540
|
|
|
{ |
|
541
|
|
|
if (!$arrayField) { |
|
542
|
|
|
$arrayField = $objectField; |
|
543
|
|
|
} |
|
544
|
|
|
if (isset($variationData[$arrayField])) { |
|
545
|
|
|
if ($value = $variationData[$arrayField]) { |
|
546
|
|
|
if ($variation->$objectField != $value) { |
|
547
|
|
|
$this->alterationMessage("........$objectField = ".$value, "changed"); |
|
548
|
|
|
} |
|
549
|
|
|
$variation->$objectField = $value; |
|
550
|
|
|
} else { |
|
551
|
|
|
$this->alterationMessage("........NO $arrayField value", "deleted"); |
|
552
|
|
|
} |
|
553
|
|
|
} else { |
|
554
|
|
|
$this->alterationMessage("........NO $arrayField field", "deleted"); |
|
555
|
|
|
} |
|
556
|
|
|
} |
|
557
|
|
|
|
|
558
|
|
|
/* |
|
559
|
|
|
* @param string $message |
|
560
|
|
|
* @param string $style |
|
561
|
|
|
*/ |
|
562
|
|
|
protected function alterationMessage($message, $style = "") |
|
563
|
|
|
{ |
|
564
|
|
|
if (!Director::isDev() || $style) { |
|
565
|
|
|
DB::alteration_message($message, $style); |
|
566
|
|
|
ob_start(); |
|
567
|
|
|
ob_end_flush(); |
|
568
|
|
|
} else { |
|
569
|
|
|
echo "."; |
|
570
|
|
|
ob_start(); |
|
571
|
|
|
ob_end_flush(); |
|
572
|
|
|
} |
|
573
|
|
|
} |
|
574
|
|
|
} |
|
575
|
|
|
|
|
576
|
|
|
|
|
577
|
|
|
class EcommerceTaskCSVToVariations_EXT extends Extension |
|
|
|
|
|
|
578
|
|
|
{ |
|
579
|
|
|
private static $allowed_actions = array( |
|
|
|
|
|
|
580
|
|
|
"ecommercetaskcsvtovariations" => true |
|
581
|
|
|
); |
|
582
|
|
|
|
|
583
|
|
|
//NOTE THAT updateEcommerceDevMenuConfig adds to Config options |
|
584
|
|
|
//but you can als have: updateEcommerceDevMenuDebugActions |
|
585
|
|
|
public function updateEcommerceDevMenuRegularMaintenance($buildTasks) |
|
586
|
|
|
{ |
|
587
|
|
|
$buildTasks[] = "ecommercetaskcsvtovariations"; |
|
588
|
|
|
return $buildTasks; |
|
589
|
|
|
} |
|
590
|
|
|
|
|
591
|
|
|
public function ecommercetaskcsvtovariations($request) |
|
592
|
|
|
{ |
|
593
|
|
|
$this->owner->runTask("ecommercetaskcsvtovariations", $request); |
|
594
|
|
|
} |
|
595
|
|
|
} |
|
596
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.