Passed
Push — master ( f8a28a...ab42dc )
by Mike
04:36
created

OrderRequest::prepare_shipping_lines()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 6
nop 3
dl 0
loc 13
rs 9.6111
c 0
b 0
f 0
1
<?php
2
/**
3
 * Convert data in the product schema format to a product object.
4
 *
5
 * @package WooCommerce/RestApi
6
 */
7
8
namespace WooCommerce\RestApi\Controllers\Version4\Schema;
9
10
defined( 'ABSPATH' ) || exit;
11
12
/**
13
 * OrderRequest class.
14
 */
15
class OrderRequest extends AbstractRequest {
16
17
	/**
18
	 * Convert request to object.
19
	 *
20
	 * @return \WC_Order
21
	 */
22
	public function prepare_object() {
23
		$id     = (int) $this->get_param( 'id', 0 );
24
		$object = new \WC_Order( $id );
25
26
		$this->set_props( $object );
27
		$this->set_meta_data( $object );
28
		$this->set_line_items( $object );
29
		$this->calculate_coupons( $object );
30
31
		return $object;
32
	}
33
34
	/**
35
	 * Set order props.
36
	 *
37
	 * @param \WC_Order $object Order object reference.
38
	 */
39
	protected function set_props( &$object ) {
40
		$props = [
41
			'parent_id',
42
			'currency',
43
			'customer_id',
44
			'customer_note',
45
			'payment_method',
46
			'payment_method_title',
47
			'transaction_id',
48
			'billing',
49
			'shipping',
50
			'status',
51
		];
52
53
		$request_props = array_intersect_key( $this->request, array_flip( $props ) );
54
		$prop_values   = [];
55
56
		foreach ( $request_props as $prop => $value ) {
57
			switch ( $prop ) {
58
				case 'customer_id':
59
					$prop_values[ $prop ] = $this->parse_customer_id_field( $value );
60
					break;
61
				case 'billing':
62
				case 'shipping':
63
					$address     = $this->parse_address_field( $value, $object, $prop );
64
					$prop_values = array_merge( $prop_values, $address );
65
					break;
66
				default:
67
					$prop_values[ $prop ] = $value;
68
			}
69
		}
70
71
		foreach ( $prop_values as $prop => $value ) {
72
			$object->{"set_$prop"}( $value );
73
		}
74
	}
75
76
	/**
77
	 * Set order line items.
78
	 *
79
	 * @param \WC_Order $object Order object reference.
80
	 */
81
	protected function set_line_items( &$object ) {
82
		$types = [
83
			'line_items',
84
			'shipping_lines',
85
			'fee_lines',
86
		];
87
88
		foreach ( $types as $type ) {
89
			if ( ! isset( $this->request[ $type ] ) || ! is_array( $this->request[ $type ] ) ) {
90
				continue;
91
			}
92
			$items = $this->request[ $type ];
93
94
			foreach ( $items as $item ) {
95
				if ( ! is_array( $item ) ) {
96
					continue;
97
				}
98
				if ( $this->item_is_null( $item ) || ( isset( $item['quantity'] ) && 0 === $item['quantity'] ) ) {
99
					$object->remove_item( $item['id'] );
100
				} else {
101
					$this->set_item( $object, $type, $item );
102
				}
103
			}
104
		}
105
	}
106
107
	/**
108
	 * Helper method to check if the resource ID associated with the provided item is null.
109
	 * Items can be deleted by setting the resource ID to null.
110
	 *
111
	 * @param array $item Item provided in the request body.
112
	 * @return bool True if the item resource ID is null, false otherwise.
113
	 */
114
	protected function item_is_null( $item ) {
115
		$keys = array( 'product_id', 'method_id', 'method_title', 'name', 'code' );
116
117
		foreach ( $keys as $key ) {
118
			if ( array_key_exists( $key, $item ) && is_null( $item[ $key ] ) ) {
119
				return true;
120
			}
121
		}
122
123
		return false;
124
	}
125
126
	/**
127
	 * Maybe set an item prop if the value was posted.
128
	 *
129
	 * @param WC_Order_Item $item   Order item.
0 ignored issues
show
Bug introduced by
The type WooCommerce\RestApi\Cont...n4\Schema\WC_Order_Item was not found. Did you mean WC_Order_Item? If so, make sure to prefix the type with \.
Loading history...
130
	 * @param string        $prop   Order property.
131
	 * @param array         $posted Request data.
132
	 */
133
	protected function maybe_set_item_prop( $item, $prop, $posted ) {
134
		if ( isset( $posted[ $prop ] ) ) {
135
			$item->{"set_$prop"}( $posted[ $prop ] );
136
		}
137
	}
138
139
	/**
140
	 * Maybe set item props if the values were posted.
141
	 *
142
	 * @param WC_Order_Item $item   Order item data.
143
	 * @param string[]      $props  Properties.
144
	 * @param array         $posted Request data.
145
	 */
146
	protected function maybe_set_item_props( $item, $props, $posted ) {
147
		foreach ( $props as $prop ) {
148
			$this->maybe_set_item_prop( $item, $prop, $posted );
149
		}
150
	}
151
152
	/**
153
	 * Maybe set item meta if posted.
154
	 *
155
	 * @param WC_Order_Item $item   Order item data.
156
	 * @param array         $posted Request data.
157
	 */
158
	protected function maybe_set_item_meta_data( $item, $posted ) {
159
		if ( ! empty( $posted['meta_data'] ) && is_array( $posted['meta_data'] ) ) {
160
			foreach ( $posted['meta_data'] as $meta ) {
161
				if ( isset( $meta['key'] ) ) {
162
					$value = isset( $meta['value'] ) ? $meta['value'] : null;
163
					$item->update_meta_data( $meta['key'], $value, isset( $meta['id'] ) ? $meta['id'] : '' );
164
				}
165
			}
166
		}
167
	}
168
169
	/**
170
	 * Gets the product ID from the SKU or posted ID.
171
	 *
172
	 * @param array $posted Request data.
173
	 * @return int
174
	 * @throws \WC_REST_Exception When SKU or ID is not valid.
175
	 */
176
	protected function get_product_id_from_line_item( $posted ) {
177
		if ( ! empty( $posted['sku'] ) ) {
178
			$product_id = (int) wc_get_product_id_by_sku( $posted['sku'] );
179
		} elseif ( ! empty( $posted['product_id'] ) && empty( $posted['variation_id'] ) ) {
180
			$product_id = (int) $posted['product_id'];
181
		} elseif ( ! empty( $posted['variation_id'] ) ) {
182
			$product_id = (int) $posted['variation_id'];
183
		} else {
184
			throw new \WC_REST_Exception( 'woocommerce_rest_required_product_reference', __( 'Product ID or SKU is required.', 'woocommerce' ), 400 );
185
		}
186
		return $product_id;
187
	}
188
189
	/**
190
	 * Create or update a line item.
191
	 *
192
	 * @param array  $posted Line item data.
193
	 * @param string $action 'create' to add line item or 'update' to update it.
194
	 * @param object $item Passed when updating an item. Null during creation.
195
	 * @return WC_Order_Item_Product
0 ignored issues
show
Bug introduced by
The type WooCommerce\RestApi\Cont...a\WC_Order_Item_Product was not found. Did you mean WC_Order_Item_Product? If so, make sure to prefix the type with \.
Loading history...
196
	 * @throws WC_REST_Exception Invalid data, server error.
197
	 */
198
	protected function prepare_line_items( $posted, $action = 'create', $item = null ) {
199
		$item    = is_null( $item ) ? new \WC_Order_Item_Product( ! empty( $posted['id'] ) ? $posted['id'] : '' ) : $item;
0 ignored issues
show
Bug introduced by
It seems like ! empty($posted['id']) ? $posted['id'] : '' can also be of type string; however, parameter $item of WC_Order_Item_Product::__construct() does only seem to accept array|integer|object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

199
		$item    = is_null( $item ) ? new \WC_Order_Item_Product( /** @scrutinizer ignore-type */ ! empty( $posted['id'] ) ? $posted['id'] : '' ) : $item;
Loading history...
200
		$product = wc_get_product( $this->get_product_id_from_line_item( $posted ) );
201
202
		if ( $product !== $item->get_product() ) {
203
			$item->set_product( $product );
204
205
			if ( 'create' === $action ) {
206
				$quantity = isset( $posted['quantity'] ) ? $posted['quantity'] : 1;
207
				$total    = wc_get_price_excluding_tax( $product, array( 'qty' => $quantity ) );
208
				$item->set_total( $total );
209
				$item->set_subtotal( $total );
210
			}
211
		}
212
213
		$this->maybe_set_item_props( $item, array( 'name', 'quantity', 'total', 'subtotal', 'tax_class' ), $posted );
214
		$this->maybe_set_item_meta_data( $item, $posted );
215
216
		return $item;
217
	}
218
219
	/**
220
	 * Create or update an order shipping method.
221
	 *
222
	 * @param array  $posted $shipping Item data.
223
	 * @param string $action 'create' to add shipping or 'update' to update it.
224
	 * @param object $item Passed when updating an item. Null during creation.
225
	 * @return \WC_Order_Item_Shipping
226
	 * @throws \WC_REST_Exception Invalid data, server error.
227
	 */
228
	protected function prepare_shipping_lines( $posted, $action = 'create', $item = null ) {
229
		$item = is_null( $item ) ? new \WC_Order_Item_Shipping( ! empty( $posted['id'] ) ? $posted['id'] : '' ) : $item;
0 ignored issues
show
Bug introduced by
It seems like ! empty($posted['id']) ? $posted['id'] : '' can also be of type string; however, parameter $item of WC_Order_Item_Shipping::__construct() does only seem to accept array|integer|object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

229
		$item = is_null( $item ) ? new \WC_Order_Item_Shipping( /** @scrutinizer ignore-type */ ! empty( $posted['id'] ) ? $posted['id'] : '' ) : $item;
Loading history...
230
231
		if ( 'create' === $action ) {
232
			if ( empty( $posted['method_id'] ) ) {
233
				throw new \WC_REST_Exception( 'woocommerce_rest_invalid_shipping_item', __( 'Shipping method ID is required.', 'woocommerce' ), 400 );
234
			}
235
		}
236
237
		$this->maybe_set_item_props( $item, array( 'method_id', 'method_title', 'total' ), $posted );
238
		$this->maybe_set_item_meta_data( $item, $posted );
239
240
		return $item;
241
	}
242
243
	/**
244
	 * Create or update an order fee.
245
	 *
246
	 * @param array  $posted Item data.
247
	 * @param string $action 'create' to add fee or 'update' to update it.
248
	 * @param object $item Passed when updating an item. Null during creation.
249
	 * @return \WC_Order_Item_Fee
250
	 * @throws \WC_REST_Exception Invalid data, server error.
251
	 */
252
	protected function prepare_fee_lines( $posted, $action = 'create', $item = null ) {
253
		$item = is_null( $item ) ? new \WC_Order_Item_Fee( ! empty( $posted['id'] ) ? $posted['id'] : '' ) : $item;
0 ignored issues
show
Bug introduced by
It seems like ! empty($posted['id']) ? $posted['id'] : '' can also be of type string; however, parameter $item of WC_Order_Item_Fee::__construct() does only seem to accept array|integer|object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

253
		$item = is_null( $item ) ? new \WC_Order_Item_Fee( /** @scrutinizer ignore-type */ ! empty( $posted['id'] ) ? $posted['id'] : '' ) : $item;
Loading history...
254
255
		if ( 'create' === $action ) {
256
			if ( empty( $posted['name'] ) ) {
257
				throw new \WC_REST_Exception( 'woocommerce_rest_invalid_fee_item', __( 'Fee name is required.', 'woocommerce' ), 400 );
258
			}
259
		}
260
261
		$this->maybe_set_item_props( $item, array( 'name', 'tax_class', 'tax_status', 'total' ), $posted );
262
		$this->maybe_set_item_meta_data( $item, $posted );
263
264
		return $item;
265
	}
266
267
	/**
268
	 * Wrapper method to create/update order items.
269
	 * When updating, the item ID provided is checked to ensure it is associated
270
	 * with the order.
271
	 *
272
	 * @param \WC_Order $order order object.
273
	 * @param string    $item_type The item type.
274
	 * @param array     $posted item provided in the request body.
275
	 * @throws \WC_REST_Exception If item ID is not associated with order.
276
	 */
277
	protected function set_item( &$order, $item_type, $posted ) {
278
		if ( ! empty( $posted['id'] ) ) {
279
			$action = 'update';
280
		} else {
281
			$action = 'create';
282
		}
283
284
		$method = 'prepare_' . $item_type;
285
		$item   = null;
286
287
		// Verify provided line item ID is associated with order.
288
		if ( 'update' === $action ) {
289
			$item = $order->get_item( absint( $posted['id'] ), false );
290
291
			if ( ! $item ) {
292
				throw new \WC_REST_Exception( 'woocommerce_rest_invalid_item_id', __( 'Order item ID provided is not associated with order.', 'woocommerce' ), 400 );
293
			}
294
		}
295
296
		// Prepare item data.
297
		$item = $this->$method( $posted, $action, $item );
298
299
		do_action( 'woocommerce_rest_set_order_item', $item, $posted );
300
301
		// If creating the order, add the item to it.
302
		if ( 'create' === $action ) {
303
			$order->add_item( $item );
304
		} else {
305
			$item->save();
306
		}
307
	}
308
309
	/**
310
	 * Parse address data.
311
	 *
312
	 * @param array     $data  Posted data.
313
	 * @param \WC_Order $object Order object reference.
314
	 * @param string    $type   Address type.
315
	 * @return array
316
	 */
317
	protected function parse_address_field( $data, $object, $type = 'billing' ) {
318
		$address = [];
319
		foreach ( $data as $key => $value ) {
320
			if ( is_callable( array( $object, "set_{$type}_{$key}" ) ) ) {
321
				$address[ "{$type}_{$key}" ] = $value;
322
			}
323
		}
324
		return $address;
325
	}
326
327
	/**
328
	 * Parse customer ID.
329
	 *
330
	 * @throws \WC_REST_Exception Will throw an exception if the customer is invalid.
331
	 * @param int $customer_id Customer ID to set.
332
	 * @return int
333
	 */
334
	protected function parse_customer_id_field( $customer_id ) {
335
		if ( 0 !== $customer_id ) {
336
			// Make sure customer exists.
337
			if ( false === get_user_by( 'id', $customer_id ) ) {
338
				throw new \WC_REST_Exception( 'woocommerce_rest_invalid_customer_id', __( 'Customer ID is invalid.', 'woocommerce' ), 400 );
339
			}
340
341
			// Make sure customer is part of blog.
342
			if ( is_multisite() && ! is_user_member_of_blog( $customer_id ) ) {
343
				add_user_to_blog( get_current_blog_id(), $customer_id, 'customer' );
344
			}
345
		}
346
		return $customer_id;
347
	}
348
349
	/**
350
	 * Calculate coupons.
351
	 *
352
	 * @throws \WC_REST_Exception When fails to set any item.
353
	 *
354
	 * @param \WC_Order $order   Order data.
355
	 * @return bool
356
	 */
357
	protected function calculate_coupons( &$order ) {
358
		$coupon_lines = $this->get_param( 'coupon_lines', false );
359
360
		if ( ! is_array( $coupon_lines ) ) {
361
			return false;
362
		}
363
364
		// Remove all coupons first to ensure calculation is correct.
365
		foreach ( $order->get_items( 'coupon' ) as $coupon ) {
366
			$order->remove_coupon( $coupon->get_code() );
0 ignored issues
show
Bug introduced by
The method get_code() does not exist on WC_Order_Item. Did you maybe mean get_order()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

366
			$order->remove_coupon( $coupon->/** @scrutinizer ignore-call */ get_code() );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
367
		}
368
369
		foreach ( $coupon_lines as $item ) {
370
			if ( is_array( $item ) ) {
371
				if ( empty( $item['id'] ) ) {
372
					if ( empty( $item['code'] ) ) {
373
						throw new \WC_REST_Exception( 'woocommerce_rest_invalid_coupon', __( 'Coupon code is required.', 'woocommerce' ), 400 );
374
					}
375
376
					$results = $order->apply_coupon( wc_clean( $item['code'] ) );
0 ignored issues
show
Bug introduced by
It seems like wc_clean($item['code']) can also be of type array; however, parameter $raw_coupon of WC_Abstract_Order::apply_coupon() does only seem to accept WC_Coupon|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

376
					$results = $order->apply_coupon( /** @scrutinizer ignore-type */ wc_clean( $item['code'] ) );
Loading history...
377
378
					if ( is_wp_error( $results ) ) {
379
						throw new \WC_REST_Exception( 'woocommerce_rest_' . $results->get_error_code(), $results->get_error_message(), 400 );
380
					}
381
				}
382
			}
383
		}
384
385
		return true;
386
	}
387
}
388