1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Order Item |
4
|
|
|
* |
5
|
|
|
* A class which represents an item within an order and handles CRUD. |
6
|
|
|
* Uses ArrayAccess to be BW compatible with WC_Orders::get_items(). |
7
|
|
|
* |
8
|
|
|
* @class WC_Order_Item |
9
|
|
|
* @version 2.6.0 |
10
|
|
|
* @since 2.6.0 |
11
|
|
|
* @package WooCommerce/Classes |
12
|
|
|
* @author WooThemes |
13
|
|
|
*/ |
14
|
|
|
class WC_Order_Item implements ArrayAccess, WC_Data { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Data array, with defaults. |
18
|
|
|
* @since 2.6.0 |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $_data = array( |
22
|
|
|
'order_id' => 0, |
23
|
|
|
'order_item_id' => 0, |
24
|
|
|
'name' => '', |
25
|
|
|
'type' => '', |
26
|
|
|
'meta_data' => array(), |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* offsetSet for ArrayAccess |
31
|
|
|
* @param string $offset |
32
|
|
|
* @param mixed $value |
33
|
|
|
*/ |
34
|
|
|
public function offsetSet( $offset, $value ) { |
35
|
|
|
if ( 'item_meta_array' === $offset ) { |
36
|
|
|
$offset = 'meta_data'; |
37
|
|
|
} |
38
|
|
|
$this->_data[ $offset ] = $value; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* offsetUnset for ArrayAccess |
43
|
|
|
* @param string $offset |
44
|
|
|
*/ |
45
|
|
|
public function offsetUnset( $offset ) { |
46
|
|
|
if ( 'item_meta_array' === $offset || 'item_meta' === $offset ) { |
47
|
|
|
$this->_data['meta_data'] = array(); |
48
|
|
|
} |
49
|
|
|
unset( $this->_data[ $offset ] ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* offsetExists for ArrayAccess |
54
|
|
|
* @param string $offset |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function offsetExists( $offset ) { |
58
|
|
|
if ( 'item_meta_array' === $offset || 'item_meta' === $offset ) { |
59
|
|
|
return true; |
60
|
|
|
} |
61
|
|
|
return isset( $this->_data[ $offset ] ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* offsetGet for ArrayAccess |
66
|
|
|
* @param string $offset |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
|
|
public function offsetGet( $offset ) { |
70
|
|
|
if( 'item_meta_array' === $offset ) { |
71
|
|
|
$offset = 'meta_data'; |
72
|
|
|
} |
73
|
|
|
elseif( 'item_meta' === $offset ) { |
74
|
|
|
$meta_values = wp_list_pluck( $this->_data['meta_data'], 'value', 'key' ); |
75
|
|
|
return $meta_values; |
76
|
|
|
} |
77
|
|
|
return isset( $this->_data[ $offset ] ) ? $this->_data[ $offset ] : null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Constructor. |
82
|
|
|
* @param int|object $order_item ID to load from the DB (optional) or already queried data. |
|
|
|
|
83
|
|
|
*/ |
84
|
|
|
public function __construct( $item = 0 ) { |
85
|
|
|
if ( $item instanceof WC_Order_Item ) { |
86
|
|
|
if ( $this->is_type( $item->get_type() ) ) { |
87
|
|
|
$this->set_all( $item ); |
88
|
|
|
} |
89
|
|
|
} else { |
90
|
|
|
$this->read( $item ); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set data based on input item. |
96
|
|
|
* @access private |
97
|
|
|
*/ |
98
|
|
|
private function set_all( $item ) { |
99
|
|
|
foreach ( $item->get_data() as $key => $value ) { |
100
|
|
|
$this->_data[ $key ] = $value; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Change data to JSON format. |
106
|
|
|
* @return string Data in JSON format. |
107
|
|
|
*/ |
108
|
|
|
public function __toString() { |
109
|
|
|
return json_encode( $this->get_data() ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Type checking |
114
|
|
|
* @param string $Type |
|
|
|
|
115
|
|
|
* @return boolean |
116
|
|
|
*/ |
117
|
|
|
public function is_type( $type ) { |
118
|
|
|
return $type === $this->get_type(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Internal meta keys we don't want exposed as part of meta_data. |
123
|
|
|
* @return array() |
|
|
|
|
124
|
|
|
*/ |
125
|
|
|
protected function get_internal_meta_keys() { |
126
|
|
|
return array(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get qty. |
131
|
|
|
* @return int |
132
|
|
|
*/ |
133
|
|
|
public function get_qty() { |
134
|
|
|
return 1; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/* |
138
|
|
|
|-------------------------------------------------------------------------- |
139
|
|
|
| Getters |
140
|
|
|
|-------------------------------------------------------------------------- |
141
|
|
|
*/ |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get all class data in array format. |
145
|
|
|
* @since 2.6.0 |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
public function get_data() { |
149
|
|
|
return $this->_data; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get order ID this meta belongs to. |
154
|
|
|
* @return int |
155
|
|
|
*/ |
156
|
|
|
public function get_id() { |
157
|
|
|
return absint( $this->_data['order_id'] ); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get order item ID. |
162
|
|
|
* @return int |
163
|
|
|
*/ |
164
|
|
|
public function get_order_item_id() { |
165
|
|
|
return absint( $this->_data['order_item_id'] ); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get order item name. |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public function get_name() { |
173
|
|
|
return $this->_data['name']; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get order item type. |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
public function get_type() { |
181
|
|
|
return $this->_data['type']; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get meta data |
186
|
|
|
* @return array |
187
|
|
|
*/ |
188
|
|
|
public function get_meta_data() { |
189
|
|
|
return $this->_data['meta_data']; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/* |
193
|
|
|
|-------------------------------------------------------------------------- |
194
|
|
|
| Setters |
195
|
|
|
|-------------------------------------------------------------------------- |
196
|
|
|
*/ |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Set order ID. |
200
|
|
|
* @param int $value |
201
|
|
|
*/ |
202
|
|
|
public function set_order_id( $value ) { |
203
|
|
|
$this->_data['order_id'] = absint( $value ); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Set order item ID. |
208
|
|
|
* @param int $value |
209
|
|
|
*/ |
210
|
|
|
public function set_order_item_id( $value ) { |
211
|
|
|
$this->_data['order_item_id'] = absint( $value ); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Set order item name. |
216
|
|
|
* @param string $value |
217
|
|
|
*/ |
218
|
|
|
public function set_name( $value ) { |
219
|
|
|
$this->_data['name'] = wc_clean( $value ); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Set order item type. |
224
|
|
|
* @param string $value |
225
|
|
|
*/ |
226
|
|
|
public function set_type( $value ) { |
227
|
|
|
$this->_data['type'] = wc_clean( $value ); |
228
|
|
|
|
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Set meta data. |
233
|
|
|
* @param array $data Key/Value pairs |
234
|
|
|
*/ |
235
|
|
|
public function set_meta_data( $data ) { |
236
|
|
|
foreach ( $data as $key => $value ) { |
237
|
|
|
$this->_data['meta_data'][ $key ] = $value; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Set meta data. |
243
|
|
|
* @param array $data Key/Value pairs |
|
|
|
|
244
|
|
|
*/ |
245
|
|
|
public function add_meta_data( $key, $value, $unique = false ) { |
246
|
|
|
if ( $unique ) { |
247
|
|
|
$meta_ids = array_keys( wp_list_pluck( $this->_data['meta_data'], 'key' ), $key ); |
248
|
|
|
$this->_data['meta_data'] = array_diff_key( $this->_data['meta_data'], array_fill_keys( $meta_ids, '' ) ); |
249
|
|
|
} |
250
|
|
|
$this->_data['meta_data'][] = array( |
251
|
|
|
'key' => $key, |
252
|
|
|
'value' => $value |
253
|
|
|
); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Update meta data by key or ID, if provided. |
258
|
|
|
* @param string $key |
259
|
|
|
* @param string $value |
260
|
|
|
* @param int $meta_id |
261
|
|
|
*/ |
262
|
|
|
public function update_meta_data( $key, $value, $meta_id = '' ) { |
263
|
|
|
if ( $meta_id && isset( $this->_data['meta_data'][ $meta_id ] ) ) { |
264
|
|
|
$this->_data['meta_data'][ $meta_id ] = array( |
265
|
|
|
'key' => $key, |
266
|
|
|
'value' => $value |
267
|
|
|
); |
268
|
|
|
} else { |
269
|
|
|
$this->add_meta_data( $key, $value, true ); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/* |
274
|
|
|
|-------------------------------------------------------------------------- |
275
|
|
|
| CRUD methods |
276
|
|
|
|-------------------------------------------------------------------------- |
277
|
|
|
| |
278
|
|
|
| Methods which create, read, update and delete data from the database. |
279
|
|
|
| |
280
|
|
|
*/ |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Insert data into the database. |
284
|
|
|
* @since 2.6.0 |
285
|
|
|
*/ |
286
|
|
View Code Duplication |
public function create() { |
|
|
|
|
287
|
|
|
global $wpdb; |
288
|
|
|
$wpdb->insert( $wpdb->prefix . 'woocommerce_order_items', array( |
289
|
|
|
'order_item_name' => $this->get_name(), |
290
|
|
|
'order_item_type' => $this->get_type(), |
291
|
|
|
'order_id' => $this->get_id() |
292
|
|
|
) ); |
293
|
|
|
$this->set_item_id( $wpdb->insert_id ); |
|
|
|
|
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Update data in the database. |
298
|
|
|
* @since 2.6.0 |
299
|
|
|
*/ |
300
|
|
View Code Duplication |
public function update() { |
|
|
|
|
301
|
|
|
global $wpdb; |
302
|
|
|
$wpdb->update( $wpdb->prefix . 'woocommerce_order_items', array( |
303
|
|
|
'order_item_name' => $this->get_name(), |
304
|
|
|
'order_item_type' => $this->get_type(), |
305
|
|
|
'order_id' => $this->get_id() |
306
|
|
|
), array( 'order_item_id' => $this->get_order_item_id() ) ); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Read from the database. |
311
|
|
|
* @since 2.6.0 |
312
|
|
|
* @param int|object $item ID of object to read, or already queried object. |
313
|
|
|
*/ |
314
|
|
|
public function read( $item ) { |
315
|
|
|
global $wpdb; |
316
|
|
|
|
317
|
|
|
if ( is_numeric( $item ) && ! empty( $item ) ) { |
318
|
|
|
$data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d LIMIT 1;", $item ) ); |
319
|
|
|
} elseif ( ! empty( $item->order_item_id ) ) { |
320
|
|
|
$data = $item; |
321
|
|
|
} else { |
322
|
|
|
$data = false; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
if ( $data ) { |
326
|
|
|
$this->set_order_id( $data->order_id ); |
327
|
|
|
$this->set_order_item_id( $data->order_item_id ); |
328
|
|
|
$this->set_name( $data->order_item_name ); |
329
|
|
|
$this->set_type( $data->order_item_type ); |
330
|
|
|
$this->read_meta_data(); |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Read Meta Data from the database. Ignore any internal properties. |
336
|
|
|
*/ |
337
|
|
|
protected function read_meta_data() { |
338
|
|
|
$this->_data['meta_data'] = array(); |
339
|
|
|
|
340
|
|
|
if ( $this->get_order_item_id() ) { |
341
|
|
|
// Get cache key - uses get_cache_prefix to invalidate when needed |
342
|
|
|
$cache_key = WC_Cache_Helper::get_cache_prefix( 'orders' ) . 'meta_data_' . $this->get_order_item_id(); |
343
|
|
|
$item_meta_array = wp_cache_get( $cache_key, 'orders' ); |
344
|
|
|
|
345
|
|
|
if ( false === $item_meta_array ) { |
346
|
|
|
global $wpdb; |
347
|
|
|
|
348
|
|
|
$metadata = $wpdb->get_results( $wpdb->prepare( "SELECT meta_id, meta_key, meta_value FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = %d ORDER BY meta_id", $this->get_order_item_id() ) ); |
349
|
|
|
foreach ( $metadata as $metadata_row ) { |
350
|
|
|
if ( in_array( $metadata_row->meta_key, $this->get_internal_meta_keys() ) ) { |
351
|
|
|
continue; |
352
|
|
|
} |
353
|
|
|
$this->_data['meta_data'][ $metadata_row->meta_id ] = (object) array( 'key' => $metadata_row->meta_key, 'value' => $metadata_row->meta_value ); |
354
|
|
|
} |
355
|
|
|
wp_cache_set( $cache_key, $item_meta_array, 'orders' ); |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Save data to the database. |
362
|
|
|
* @since 2.6.0 |
363
|
|
|
*/ |
364
|
|
|
public function save() { |
365
|
|
|
if ( ! $this->get_order_item_id() ) { |
366
|
|
|
$this->create(); |
367
|
|
|
} else { |
368
|
|
|
$this->update(); |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Delete data from the database. |
374
|
|
|
* @since 2.6.0 |
375
|
|
|
*/ |
376
|
|
|
public function delete() { |
377
|
|
|
global $wpdb; |
378
|
|
|
$wpdb->delete( $wpdb->prefix . 'woocommerce_order_items', array( 'order_item_id' => $this->get_order_item_id() ) ); |
379
|
|
|
} |
380
|
|
|
} |
381
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.