class-wc-rest-orders-controller.php
10.9 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
/**
* REST API Orders controller
*
* Handles requests to the /orders endpoint.
*
* @package WooCommerce\RestApi
* @since 2.6.0
*/
use Automattic\WooCommerce\Utilities\OrderUtil;
defined( 'ABSPATH' ) || exit;
/**
* REST API Orders controller class.
*
* @package WooCommerce\RestApi
* @extends WC_REST_Orders_V2_Controller
*/
class WC_REST_Orders_Controller extends WC_REST_Orders_V2_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc/v3';
/**
* Calculate coupons.
*
* @throws WC_REST_Exception When fails to set any item.
* @param WP_REST_Request $request Request object.
* @param WC_Order $order Order data.
* @return bool
*/
protected function calculate_coupons( $request, $order ) {
if ( ! isset( $request['coupon_lines'] ) ) {
return false;
}
// Validate input and at the same time store the processed coupon codes to apply.
$coupon_codes = array();
$discounts = new WC_Discounts( $order );
$current_order_coupons = array_values( $order->get_coupons() );
$current_order_coupon_codes = array_map(
function( $coupon ) {
return $coupon->get_code();
},
$current_order_coupons
);
foreach ( $request['coupon_lines'] as $item ) {
if ( ! empty( $item['id'] ) ) {
throw new WC_REST_Exception( 'woocommerce_rest_coupon_item_id_readonly', __( 'Coupon item ID is readonly.', 'woocommerce' ), 400 );
}
if ( empty( $item['code'] ) ) {
throw new WC_REST_Exception( 'woocommerce_rest_invalid_coupon', __( 'Coupon code is required.', 'woocommerce' ), 400 );
}
$coupon_code = wc_format_coupon_code( wc_clean( $item['code'] ) );
$coupon = new WC_Coupon( $coupon_code );
// Skip check if the coupon is already applied to the order, as this could wrongly throw an error for single-use coupons.
if ( ! in_array( $coupon_code, $current_order_coupon_codes, true ) ) {
$check_result = $discounts->is_coupon_valid( $coupon );
if ( is_wp_error( $check_result ) ) {
throw new WC_REST_Exception( 'woocommerce_rest_' . $check_result->get_error_code(), $check_result->get_error_message(), 400 );
}
}
$coupon_codes[] = $coupon_code;
}
// Remove all coupons first to ensure calculation is correct.
foreach ( $order->get_items( 'coupon' ) as $existing_coupon ) {
$order->remove_coupon( $existing_coupon->get_code() );
}
// Apply the coupons.
foreach ( $coupon_codes as $new_coupon ) {
$results = $order->apply_coupon( $new_coupon );
if ( is_wp_error( $results ) ) {
throw new WC_REST_Exception( 'woocommerce_rest_' . $results->get_error_code(), $results->get_error_message(), 400 );
}
}
return true;
}
/**
* Prepare a single order for create or update.
*
* @throws WC_REST_Exception When fails to set any item.
* @param WP_REST_Request $request Request object.
* @param bool $creating If is creating a new object.
* @return WP_Error|WC_Data
*/
protected function prepare_object_for_database( $request, $creating = false ) {
$id = isset( $request['id'] ) ? absint( $request['id'] ) : 0;
$order = new WC_Order( $id );
$schema = $this->get_item_schema();
$data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) );
// Handle all writable props.
foreach ( $data_keys as $key ) {
$value = $request[ $key ];
if ( ! is_null( $value ) ) {
switch ( $key ) {
case 'coupon_lines':
case 'status':
// Change should be done later so transitions have new data.
break;
case 'billing':
case 'shipping':
$this->update_address( $order, $value, $key );
break;
case 'line_items':
case 'shipping_lines':
case 'fee_lines':
if ( is_array( $value ) ) {
foreach ( $value as $item ) {
if ( is_array( $item ) ) {
if ( $this->item_is_null( $item ) || ( isset( $item['quantity'] ) && 0 === $item['quantity'] ) ) {
$order->remove_item( $item['id'] );
} else {
$this->set_item( $order, $key, $item );
}
}
}
}
break;
case 'meta_data':
if ( is_array( $value ) ) {
foreach ( $value as $meta ) {
$order->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
}
}
break;
default:
if ( is_callable( array( $order, "set_{$key}" ) ) ) {
$order->{"set_{$key}"}( $value );
}
break;
}
}
}
/**
* Filters an object before it is inserted via the REST API.
*
* The dynamic portion of the hook name, `$this->post_type`,
* refers to the object type slug.
*
* @param WC_Data $order Object object.
* @param WP_REST_Request $request Request object.
* @param bool $creating If is creating a new object.
*/
return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}_object", $order, $request, $creating );
}
/**
* Save an object data.
*
* @since 3.0.0
* @throws WC_REST_Exception But all errors are validated before returning any data.
* @param WP_REST_Request $request Full details about the request.
* @param bool $creating If is creating a new object.
* @return WC_Data|WP_Error
*/
protected function save_object( $request, $creating = false ) {
try {
$object = $this->prepare_object_for_database( $request, $creating );
if ( is_wp_error( $object ) ) {
return $object;
}
// Make sure gateways are loaded so hooks from gateways fire on save/create.
WC()->payment_gateways();
if ( ! is_null( $request['customer_id'] ) && 0 !== $request['customer_id'] ) {
// Make sure customer exists.
if ( false === get_user_by( 'id', $request['customer_id'] ) ) {
throw new WC_REST_Exception( 'woocommerce_rest_invalid_customer_id', __( 'Customer ID is invalid.', 'woocommerce' ), 400 );
}
// Make sure customer is part of blog.
if ( is_multisite() && ! is_user_member_of_blog( $request['customer_id'] ) ) {
add_user_to_blog( get_current_blog_id(), $request['customer_id'], 'customer' );
}
}
if ( $creating ) {
$object->set_created_via( 'rest-api' );
$object->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) );
$object->calculate_totals();
} else {
// If items have changed, recalculate order totals.
if ( isset( $request['billing'] ) || isset( $request['shipping'] ) || isset( $request['line_items'] ) || isset( $request['shipping_lines'] ) || isset( $request['fee_lines'] ) || isset( $request['coupon_lines'] ) ) {
$object->calculate_totals( true );
}
}
// Set coupons.
$this->calculate_coupons( $request, $object );
// Set status.
if ( ! empty( $request['status'] ) ) {
$object->set_status( $request['status'] );
}
$object->save();
// Actions for after the order is saved.
if ( true === $request['set_paid'] ) {
if ( $creating || $object->needs_payment() ) {
$object->payment_complete( $request['transaction_id'] );
}
}
return $this->get_object( $object->get_id() );
} catch ( WC_Data_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
} catch ( WC_REST_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
}
/**
* Get formatted item data.
*
* @param WC_Order $order WC_Data instance.
* @return array
*/
protected function get_formatted_item_data( $order ) {
$item_data = parent::get_formatted_item_data( $order );
$cpt_hidden_keys = array();
if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
$cpt_hidden_keys = (new \WC_Order_Data_Store_CPT())->get_internal_meta_keys();
}
// XXX: This might be removed once we finalize the design for internal keys vs meta vs props in COT.
if ( ! empty( $item_data['meta_data'] ) ) {
$item_data['meta_data'] = array_filter(
$item_data['meta_data'],
function( $meta ) use ( $cpt_hidden_keys ) {
return ! in_array( $meta->key, $cpt_hidden_keys, true );
}
);
}
return $item_data;
}
/**
* Prepare objects query.
*
* @since 3.0.0
* @param WP_REST_Request $request Full details about the request.
* @return array
*/
protected function prepare_objects_query( $request ) {
// This is needed to get around an array to string notice in WC_REST_Orders_V2_Controller::prepare_objects_query.
$statuses = $request['status'];
unset( $request['status'] );
// Prevents WC_REST_Orders_V2_Controller::prepare_objects_query() from generating a meta_query for 'customer'.
// which COT can handle as a native field.
$cot_customer =
( OrderUtil::custom_orders_table_usage_is_enabled() && isset( $request['customer'] ) )
? $request['customer']
: null;
if ( $cot_customer ) {
unset( $request['customer'] );
}
$args = parent::prepare_objects_query( $request );
$args['post_status'] = array();
foreach ( $statuses as $status ) {
if ( in_array( $status, $this->get_order_statuses(), true ) ) {
$args['post_status'][] = 'wc-' . $status;
} elseif ( 'any' === $status ) {
// Set status to "any" and short-circuit out.
$args['post_status'] = 'any';
break;
} else {
$args['post_status'][] = $status;
}
}
// Put the statuses back for further processing (next/prev links, etc).
$request['status'] = $statuses;
// Add back 'customer' to args and request.
if ( ! is_null( $cot_customer ) ) {
$args['customer'] = $cot_customer;
$request['customer'] = $cot_customer;
}
return $args;
}
/**
* Get objects.
*
* @param array $query_args Query args.
* @return array
*/
protected function get_objects( $query_args ) {
// Do not use WC_Order_Query for the CPT datastore.
if ( ! OrderUtil::custom_orders_table_usage_is_enabled() ) {
return parent::get_objects( $query_args );
}
$query = new \WC_Order_Query(
array_merge(
$query_args,
array(
'paginate' => true,
)
)
);
$results = $query->get_orders();
return array(
'objects' => $results->orders,
'total' => $results->total,
'pages' => $results->max_num_pages,
);
}
/**
* Get the Order's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = parent::get_item_schema();
$schema['properties']['coupon_lines']['items']['properties']['discount']['readonly'] = true;
return $schema;
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['status'] = array(
'default' => 'any',
'description' => __( 'Limit result set to orders which have specific statuses.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'string',
'enum' => array_merge( array( 'any', 'trash' ), $this->get_order_statuses() ),
),
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
}