-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrder.php
More file actions
207 lines (176 loc) · 4.88 KB
/
Order.php
File metadata and controls
207 lines (176 loc) · 4.88 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
196
197
198
199
200
201
202
203
204
205
206
207
<?php
class Order {
private $db;
private $environment;
private $id;
private $order = [];
public function __construct(
DB $db,
string $environment,
int $id = null
) {
$this->db = $db;
$this->environment = $environment;
if ($id) {
$this->id = $id;
$this->load();
}
}
public function load()
{
$this->order['data'] = $this->loadData();
$this->order['meta'] = $this->loadMeta($this->id);
$this->order['items'] = $this->loadOrderItems();
$this->order['comments'] = $this->loadComments();
$this->order['bookings'] = $this->loadBookings();
$this->order['vouchers'] = $this->loadVouchers();
}
private function loadData()
{
return $this->db->getRow(
$this->environment,
'SELECT *
FROM wp_posts
WHERE ID=?
AND post_type="shop_order"
LIMIT 1;',
[$this->id]
);
}
private function loadMeta($parentId)
{
return $this->db->getRows(
$this->environment,
'SELECT *
FROM wp_postmeta
WHERE post_id=?',
[$parentId]
);
}
private function loadComments()
{
return $this->db->getRows(
$this->environment,
'SELECT *
FROM wp_comments
WHERE comment_post_ID=?',
[$this->id]
);
}
private function loadBookings()
{
$bookings = $this->db->getRows(
$this->environment,
'SELECT *
FROM wp_posts
WHERE post_parent=?
AND post_type="wc_booking"',
[$this->id]
);
if ($bookings) {
foreach ($bookings as &$booking) {
$booking['meta'] = $this->loadMeta($booking['ID']);
}
}
return $bookings;
}
private function loadOrderItems()
{
$orderItems = $this->db->getRows(
$this->environment,
'SELECT *
FROM wp_woocommerce_order_items
WHERE order_id=?',
[$this->id]
);
foreach ($orderItems as &$orderItem) {
$orderItem['meta'] = $this->db->getRows(
$this->environment,
'SELECT *
FROM wp_woocommerce_order_itemmeta
WHERE order_item_id=?',
[$orderItem['order_item_id']]
);
}
return $orderItems;
}
private function loadVouchers(): array
{
$vouchers = [];
// look through all order item meta fields for any voucher codes
foreach ($this->order['items'] as $item) {
foreach ($item['meta'] as $meta) {
if ($meta['meta_key'] == '_woo_vou_codes') {
$voucherCodes = explode(', ', $meta['meta_value']);
foreach ($voucherCodes as $voucherCode) {
$vouchers[] = $this->loadVoucher(trim($voucherCode));
}
}
}
}
return $vouchers;
}
private function loadVoucher(string $voucherCode): array
{
$voucher = $this->db->getRow(
$this->environment,
"SELECT *
FROM wp_posts
WHERE ID = (
SELECT post_id
FROM wp_postmeta
WHERE meta_key = '_woo_vou_purchased_codes'
AND meta_value = :voucher_code
);",
[ 'voucher_code' => $voucherCode ]
);
$voucher['meta'] = $this->db->getRows(
$this->environment,
"SELECT *
FROM wp_postmeta
WHERE post_id = :post_id;",
[ 'post_id' => $voucher['ID'] ]
);
return $voucher;
}
public function get($dataKey)
{
return $this->order['data'][$dataKey];
}
public function getData(): array
{
return $this->order['data'];
}
public function getMeta(): array
{
return $this->order['meta'];
}
public function getItems(): array
{
return $this->order['items'];
}
public function hasComments(): bool
{
return (is_array($this->order['comments']) && count($this->order['comments']));
}
public function getComments(): array
{
return $this->order['comments'];
}
public function hasBookings(): bool
{
return (is_array($this->order['bookings']) && count($this->order['bookings']));
}
public function getBookings(): array
{
return $this->order['bookings'];
}
public function hasVouchers(): bool
{
return (is_array($this->order['vouchers']) && count($this->order['vouchers']));
}
public function getVouchers(): array
{
return $this->order['vouchers'];
}
}