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
|
|
|
} |
42
|
|
|
|
43
|
|
|
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
|
|
|
]; |
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
|
|
|
} |
85
|
|
|
|
86
|
|
|
foreach ($orderStatusDefinitions as $orderStatusHandle => $orderStatusDefinition) { |
87
|
|
|
$orderStatus = array_key_exists($orderStatusHandle, $orderStatuses) |
88
|
|
|
? $orderStatuses[$orderStatusHandle] |
89
|
|
|
: new Commerce_OrderStatusModel(); |
90
|
|
|
|
91
|
|
|
unset($orderStatuses[$orderStatusHandle]); |
92
|
|
|
|
93
|
|
|
$this->populateOrderStatus($orderStatus, $orderStatusDefinition, $orderStatusHandle); |
94
|
|
|
$emailIds = []; |
95
|
|
|
if (is_array(@$orderStatusDefinition['emails'])) { |
96
|
|
|
foreach ($orderStatusDefinition['emails'] as $emailName) { |
97
|
|
|
if (array_key_exists($emailName, $emails)) { |
98
|
|
|
$emailIds[] = $emails[$emailName]->id; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (!Craft::app()->commerce_orderStatuses->saveOrderStatus($orderStatus, $emailIds)) { // Save orderstatus via craft |
104
|
|
|
$this->addErrors($orderStatus->getAllErrors()); |
105
|
|
|
|
106
|
|
|
continue; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($force) { |
111
|
|
|
foreach ($orderStatuses as $orderStatus) { |
112
|
|
|
Craft::app()->commerce_orderStatuses->deleteOrderStatusById($orderStatus->id); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
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
|
|
|
private function populateOrderStatus(Commerce_OrderStatusModel $orderStatus, array $orderStatusDefinition, $orderStatusHandle) |
127
|
|
|
{ |
128
|
|
|
$orderStatus->setAttributes([ |
129
|
|
|
'handle' => $orderStatusHandle, |
130
|
|
|
'name' => $orderStatusDefinition['name'], |
131
|
|
|
'color' => $orderStatusDefinition['color'], |
132
|
|
|
'sortOrder' => $orderStatusDefinition['sortOrder'], |
133
|
|
|
'default' => $orderStatusDefinition['default'], |
134
|
|
|
]); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|