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
|
<?php /** * REST API Orders Controller * * Handles requests to /orders/* */
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\Controller as ReportsController; use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore; use Automattic\WooCommerce\Utilities\OrderUtil;
/** * Orders controller. * * @internal * @extends WC_REST_Orders_Controller */ class Orders extends \WC_REST_Orders_Controller { /** * Endpoint namespace. * * @var string */ protected $namespace = 'wc-analytics';
/** * Get the query params for collections. * * @return array */ public function get_collection_params() { $params = parent::get_collection_params(); // This needs to remain a string to support extensions that filter Order Number. $params['number'] = array( 'description' => __( 'Limit result set to orders matching part of an order number.', 'woocommerce' ), 'type' => 'string', 'validate_callback' => 'rest_validate_request_arg', ); // Fix the default 'status' value until it can be patched in core. $params['status']['default'] = array( 'any' );
// Analytics settings may affect the allowed status list. $params['status']['items']['enum'] = ReportsController::get_order_statuses();
return $params; }
/** * Prepare objects query. * * @param WP_REST_Request $request Full details about the request. * @return array */ protected function prepare_objects_query( $request ) { $args = parent::prepare_objects_query( $request );
if ( ! empty( $request['number'] ) ) { $args = $this->search_partial_order_number( $request['number'], $args ); }
return $args; }
/** * Helper method to allow searching by partial order number. * * @param int $number Partial order number match. * @param array $args List of arguments for the request. * * @return array Modified args with partial order search included. */ private function search_partial_order_number( $number, $args ) { global $wpdb;
$partial_number = trim( $number ); $limit = intval( $args['posts_per_page'] ); if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { $order_table_name = OrdersTableDataStore::get_orders_table_name(); // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $orders_table_name is hardcoded. $order_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM $order_table_name WHERE type = 'shop_order' AND id LIKE %s LIMIT %d", $wpdb->esc_like( absint( $partial_number ) ) . '%', $limit ) ); // phpcs:enable } else { $order_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'shop_order' AND ID LIKE %s LIMIT %d", $wpdb->esc_like( absint( $partial_number ) ) . '%', $limit ) ); }
// Force WP_Query return empty if don't found any order. $order_ids = empty( $order_ids ) ? array( 0 ) : $order_ids; $args['post__in'] = $order_ids;
return $args; }
/** * Get product IDs, names, and quantity from order ID. * * @param array $order_id ID of order. * @return array */ protected function get_products_by_order_id( $order_id ) { global $wpdb; $order_items_table = $wpdb->prefix . 'woocommerce_order_items'; $order_itemmeta_table = $wpdb->prefix . 'woocommerce_order_itemmeta'; $products = $wpdb->get_results( $wpdb->prepare( // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared "SELECT order_id, order_itemmeta.meta_value as product_id, order_itemmeta_2.meta_value as product_quantity, order_itemmeta_3.meta_value as variation_id, {$wpdb->posts}.post_title as product_name FROM {$order_items_table} order_items LEFT JOIN {$order_itemmeta_table} order_itemmeta on order_items.order_item_id = order_itemmeta.order_item_id LEFT JOIN {$order_itemmeta_table} order_itemmeta_2 on order_items.order_item_id = order_itemmeta_2.order_item_id LEFT JOIN {$order_itemmeta_table} order_itemmeta_3 on order_items.order_item_id = order_itemmeta_3.order_item_id LEFT JOIN {$wpdb->posts} on {$wpdb->posts}.ID = order_itemmeta.meta_value WHERE order_id = ( %d ) AND order_itemmeta.meta_key = '_product_id' AND order_itemmeta_2.meta_key = '_qty' AND order_itemmeta_3.meta_key = '_variation_id' GROUP BY product_id ", // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared $order_id ), ARRAY_A );
return $products; }
/** * Get customer data from customer_id. * * @param array $customer_id ID of customer. * @return array */ protected function get_customer_by_id( $customer_id ) { global $wpdb;
$customer_lookup_table = $wpdb->prefix . 'wc_customer_lookup';
$customer = $wpdb->get_row( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared "SELECT * FROM {$customer_lookup_table} WHERE customer_id = ( %d )", $customer_id ), ARRAY_A );
return $customer; }
/** * Get formatted item data. * * @param WC_Data $object WC_Data instance. * @return array */ protected function get_formatted_item_data( $object ) { $extra_fields = array( 'customer', 'products' ); $fields = false; // Determine if the response fields were specified. if ( ! empty( $this->request['_fields'] ) ) { $fields = wp_parse_list( $this->request['_fields'] );
if ( 0 === count( $fields ) ) { $fields = false; } else { $fields = array_map( 'trim', $fields ); } }
// Initially skip line items if we can. $using_order_class_override = is_a( $object, '\Automattic\WooCommerce\Admin\Overrides\Order' ); if ( $using_order_class_override ) { $data = $object->get_data_without_line_items(); } else { $data = $object->get_data(); }
$extra_fields = false === $fields ? array() : array_intersect( $extra_fields, $fields ); $format_decimal = array( 'discount_total', 'discount_tax', 'shipping_total', 'shipping_tax', 'shipping_total', 'shipping_tax', 'cart_tax', 'total', 'total_tax' ); $format_date = array( 'date_created', 'date_modified', 'date_completed', 'date_paid' ); $format_line_items = array( 'line_items', 'tax_lines', 'shipping_lines', 'fee_lines', 'coupon_lines' );
// Add extra data as necessary. $extra_data = array(); foreach ( $extra_fields as $field ) { switch ( $field ) { case 'customer': $extra_data['customer'] = $this->get_customer_by_id( $data['customer_id'] ); break; case 'products': $extra_data['products'] = $this->get_products_by_order_id( $object->get_id() ); break; } } // Format decimal values. foreach ( $format_decimal as $key ) { $data[ $key ] = wc_format_decimal( $data[ $key ], $this->request['dp'] ); }
// format total with order currency. if ( $object instanceof \WC_Order ) { $data['total_formatted'] = wp_strip_all_tags( html_entity_decode( $object->get_formatted_order_total() ), true ); }
// Format date values. foreach ( $format_date as $key ) { $datetime = $data[ $key ]; $data[ $key ] = wc_rest_prepare_date_response( $datetime, false ); $data[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime ); }
// Format the order status. $data['status'] = OrderUtil::remove_status_prefix( $data['status'] );
// Format requested line items. $formatted_line_items = array();
foreach ( $format_line_items as $key ) { if ( false === $fields || in_array( $key, $fields, true ) ) { if ( $using_order_class_override ) { $line_item_data = $object->get_line_item_data( $key ); } else { $line_item_data = $data[ $key ]; } $formatted_line_items[ $key ] = array_values( array_map( array( $this, 'get_order_item_data' ), $line_item_data ) ); } }
// Refunds. $data['refunds'] = array(); foreach ( $object->get_refunds() as $refund ) { $data['refunds'][] = array( 'id' => $refund->get_id(), 'reason' => $refund->get_reason() ? $refund->get_reason() : '', 'total' => '-' . wc_format_decimal( $refund->get_amount(), $this->request['dp'] ), ); }
return array_merge( array( 'id' => $object->get_id(), 'parent_id' => $data['parent_id'], 'number' => $data['number'], 'order_key' => $data['order_key'], 'created_via' => $data['created_via'], 'version' => $data['version'], 'status' => $data['status'], 'currency' => $data['currency'], 'date_created' => $data['date_created'], 'date_created_gmt' => $data['date_created_gmt'], 'date_modified' => $data['date_modified'], 'date_modified_gmt' => $data['date_modified_gmt'], 'discount_total' => $data['discount_total'], 'discount_tax' => $data['discount_tax'], 'shipping_total' => $data['shipping_total'], 'shipping_tax' => $data['shipping_tax'], 'cart_tax' => $data['cart_tax'], 'total' => $data['total'], 'total_formatted' => isset( $data['total_formatted'] ) ? $data['total_formatted'] : $data['total'], 'total_tax' => $data['total_tax'], 'prices_include_tax' => $data['prices_include_tax'], 'customer_id' => $data['customer_id'], 'customer_ip_address' => $data['customer_ip_address'], 'customer_user_agent' => $data['customer_user_agent'], 'customer_note' => $data['customer_note'], 'billing' => $data['billing'], 'shipping' => $data['shipping'], 'payment_method' => $data['payment_method'], 'payment_method_title' => $data['payment_method_title'], 'transaction_id' => $data['transaction_id'], 'date_paid' => $data['date_paid'], 'date_paid_gmt' => $data['date_paid_gmt'], 'date_completed' => $data['date_completed'], 'date_completed_gmt' => $data['date_completed_gmt'], 'cart_hash' => $data['cart_hash'], 'meta_data' => $data['meta_data'], 'refunds' => $data['refunds'], ), $formatted_line_items, $extra_data ); } }
|