|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Task; |
|
4
|
|
|
|
|
5
|
|
|
use Dynamic\Foxy\Model\FoxyHelper; |
|
6
|
|
|
use Dynamic\Foxy\Model\OptionType; |
|
7
|
|
|
use Dynamic\Foxy\Model\Variation; |
|
8
|
|
|
use Dynamic\Foxy\Model\VariationType; |
|
9
|
|
|
use SilverStripe\Dev\BuildTask; |
|
10
|
|
|
use SilverStripe\ORM\ValidationException; |
|
11
|
|
|
|
|
12
|
|
|
class OptionToVariationMigration extends BuildTask |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $title = 'Foxy - Option to Variation Migration'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private static $segment = 'foxy-option-to-variation-migration'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
private $variation_fields = [ |
|
28
|
|
|
'Title', |
|
29
|
|
|
'WeightModifier', |
|
30
|
|
|
'CodeModifier', |
|
31
|
|
|
'PriceModifier', |
|
32
|
|
|
'WeightModifierAction', |
|
33
|
|
|
'CodeModifierAction', |
|
34
|
|
|
'PriceModifierAction', |
|
35
|
|
|
'Available', |
|
36
|
|
|
'OptionModifierKey', |
|
37
|
|
|
'SortOrder', |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
private $type_map = []; |
|
44
|
|
|
|
|
45
|
|
|
public function run($request) |
|
46
|
|
|
{ |
|
47
|
|
|
foreach ($this->yieldSingle(FoxyHelper::singleton()->getProducts()) as $product) { |
|
48
|
|
|
foreach ($this->yieldSingle($product->Options()) as $option) { |
|
49
|
|
|
$this->createProductVariation($product, $option); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return \Generator |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function yieldSingle($list) |
|
58
|
|
|
{ |
|
59
|
|
|
foreach ($list as $item) { |
|
60
|
|
|
yield $item; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param $product |
|
66
|
|
|
* @param $option |
|
67
|
|
|
* @return int |
|
68
|
|
|
* @throws ValidationException |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function createProductVariation($product, $option) |
|
71
|
|
|
{ |
|
72
|
|
|
$variation = Variation::create(); |
|
73
|
|
|
|
|
74
|
|
|
foreach ($this->yieldSingle($this->variation_fields) as $fieldName) { |
|
75
|
|
|
$variation->{$fieldName} = $option->{$fieldName}; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$variation->VariationTypeID = $this->findOrMakeVariationType($option); |
|
79
|
|
|
$variation->ProductID = $product->ID; |
|
80
|
|
|
|
|
81
|
|
|
return $variation->write(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param $option |
|
86
|
|
|
* @return mixed |
|
87
|
|
|
* @throws ValidationException |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function findOrMakeVariationType($option) |
|
90
|
|
|
{ |
|
91
|
|
|
if (!array_key_exists($option->Type, $this->type_map)) { |
|
92
|
|
|
$optionType = OptionType::get()->byID($option->Type); |
|
93
|
|
|
$variationType = VariationType::create(); |
|
94
|
|
|
$variationType->Title = $optionType->Title; |
|
95
|
|
|
$variationType->write(); |
|
96
|
|
|
|
|
97
|
|
|
$this->type_map[$optionType->ID] = $variationType->ID; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $this->type_map[$optionType->ID]; |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|