|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Commerce\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Craft\Craft; |
|
6
|
|
|
use Craft\Commerce_OrderStatusModel; |
|
7
|
|
|
use NerdsAndCompany\Schematic\Services\Base; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Schematic Commerce Order Statuses Service. |
|
11
|
|
|
* |
|
12
|
|
|
* Sync Craft Setups. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Nerds & Company |
|
15
|
|
|
* @copyright Copyright (c) 2015-2017, Nerds & Company |
|
16
|
|
|
* @license MIT |
|
17
|
|
|
* |
|
18
|
|
|
* @see http://www.nerds.company |
|
19
|
|
|
*/ |
|
20
|
|
|
class OrderStatuses extends Base |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Export orderStatuses. |
|
24
|
|
|
* |
|
25
|
|
|
* @param OrderStatusModel[] $orderStatuses |
|
26
|
|
|
* |
|
27
|
|
|
* @return array |
|
28
|
|
|
*/ |
|
29
|
2 |
|
public function export(array $orderStatuses = []) |
|
30
|
|
|
{ |
|
31
|
2 |
|
if (!count($orderStatuses)) { |
|
32
|
|
|
$orderStatuses = Craft::app()->commerce_orderStatuses->getAllOrderStatuses(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
2 |
|
Craft::log(Craft::t('Exporting Commerce Order Statuses')); |
|
36
|
|
|
|
|
37
|
2 |
|
$orderStatusDefinitions = []; |
|
38
|
|
|
|
|
39
|
2 |
|
foreach ($orderStatuses as $orderStatus) { |
|
40
|
2 |
|
$orderStatusDefinitions[$orderStatus->handle] = $this->getOrderStatusDefinition($orderStatus); |
|
41
|
2 |
|
} |
|
42
|
|
|
|
|
43
|
2 |
|
return $orderStatusDefinitions; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get order statuses definition. |
|
48
|
|
|
* |
|
49
|
|
|
* @param Commerce_OrderStatusModel $orderStatus |
|
50
|
|
|
* |
|
51
|
|
|
* @return array |
|
52
|
|
|
*/ |
|
53
|
2 |
|
private function getOrderStatusDefinition(Commerce_OrderStatusModel $orderStatus) |
|
54
|
|
|
{ |
|
55
|
|
|
return [ |
|
56
|
2 |
|
'name' => $orderStatus->name, |
|
57
|
2 |
|
'color' => $orderStatus->color, |
|
58
|
2 |
|
'sortOrder' => $orderStatus->sortOrder, |
|
59
|
2 |
|
'default' => $orderStatus->default, |
|
60
|
2 |
|
'emails' => array_column($orderStatus->getEmails(), 'name'), |
|
61
|
2 |
|
]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Attempt to import order statuses. |
|
66
|
|
|
* |
|
67
|
|
|
* @param array $orderStatusDefinitions |
|
68
|
|
|
* @param bool $force If set to true order statuses not included in the import will be deleted |
|
69
|
|
|
* |
|
70
|
|
|
* @return Result |
|
71
|
|
|
*/ |
|
72
|
4 |
|
public function import(array $orderStatusDefinitions, $force = false) |
|
73
|
|
|
{ |
|
74
|
4 |
|
Craft::log(Craft::t('Importing Commerce Order Statuses')); |
|
75
|
|
|
|
|
76
|
4 |
|
$orderStatuses = []; |
|
77
|
4 |
|
foreach (Craft::app()->commerce_orderStatuses->getAllOrderStatuses() as $orderStatus) { |
|
78
|
|
|
$orderStatuses[$orderStatus->handle] = $orderStatus; |
|
79
|
4 |
|
} |
|
80
|
|
|
|
|
81
|
4 |
|
$emails = []; |
|
82
|
4 |
|
foreach (Craft::app()->commerce_emails->getAllEmails() as $email) { |
|
83
|
|
|
$emails[$email->name] = $email; |
|
84
|
4 |
|
} |
|
85
|
|
|
|
|
86
|
4 |
|
foreach ($orderStatusDefinitions as $orderStatusHandle => $orderStatusDefinition) { |
|
87
|
2 |
|
$orderStatus = array_key_exists($orderStatusHandle, $orderStatuses) |
|
88
|
2 |
|
? $orderStatuses[$orderStatusHandle] |
|
89
|
2 |
|
: new Commerce_OrderStatusModel(); |
|
90
|
|
|
|
|
91
|
2 |
|
unset($orderStatuses[$orderStatusHandle]); |
|
92
|
|
|
|
|
93
|
2 |
|
$this->populateOrderStatus($orderStatus, $orderStatusDefinition, $orderStatusHandle); |
|
94
|
2 |
|
$emailIds = []; |
|
95
|
2 |
|
if (is_array(@$orderStatusDefinition['emails'])) { |
|
96
|
2 |
|
foreach ($orderStatusDefinition['emails'] as $emailName) { |
|
97
|
|
|
if (array_key_exists($emailName, $emails)) { |
|
98
|
|
|
$emailIds[] = $emails[$emailName]->id; |
|
99
|
|
|
} |
|
100
|
2 |
|
} |
|
101
|
2 |
|
} |
|
102
|
|
|
|
|
103
|
2 |
|
if (!Craft::app()->commerce_orderStatuses->saveOrderStatus($orderStatus, $emailIds)) { // Save orderstatus via craft |
|
104
|
2 |
|
$this->addErrors($orderStatus->getAllErrors()); |
|
105
|
|
|
|
|
106
|
2 |
|
continue; |
|
107
|
|
|
} |
|
108
|
4 |
|
} |
|
109
|
|
|
|
|
110
|
4 |
|
if ($force) { |
|
111
|
2 |
|
foreach ($orderStatuses as $orderStatus) { |
|
112
|
|
|
Craft::app()->commerce_orderStatuses->deleteOrderStatusById($orderStatus->id); |
|
113
|
2 |
|
} |
|
114
|
2 |
|
} |
|
115
|
|
|
|
|
116
|
4 |
|
return $this->getResultModel(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Populate orderStatus. |
|
121
|
|
|
* |
|
122
|
|
|
* @param Commerce_OrderStatusModel $orderStatus |
|
123
|
|
|
* @param array $orderStatusDefinition |
|
124
|
|
|
* @param string $orderStatusHandle |
|
125
|
|
|
*/ |
|
126
|
2 |
|
private function populateOrderStatus(Commerce_OrderStatusModel $orderStatus, array $orderStatusDefinition, $orderStatusHandle) |
|
127
|
|
|
{ |
|
128
|
2 |
|
$orderStatus->setAttributes([ |
|
129
|
2 |
|
'handle' => $orderStatusHandle, |
|
130
|
2 |
|
'name' => $orderStatusDefinition['name'], |
|
131
|
2 |
|
'color' => $orderStatusDefinition['color'], |
|
132
|
2 |
|
'sortOrder' => $orderStatusDefinition['sortOrder'], |
|
133
|
2 |
|
'default' => $orderStatusDefinition['default'], |
|
134
|
2 |
|
]); |
|
135
|
2 |
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|