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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
<?php /** * REST API Reports products controller * * Handles requests to the /reports/products endpoint. */
namespace Automattic\WooCommerce\Admin\API\Reports\Variations;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface; use Automattic\WooCommerce\Admin\API\Reports\ExportableTraits; use Automattic\WooCommerce\Admin\API\Reports\GenericController; use Automattic\WooCommerce\Admin\API\Reports\GenericQuery; use Automattic\WooCommerce\Admin\API\Reports\OrderAwareControllerTrait;
/** * REST API Reports products controller class. * * @internal * @extends GenericController */ class Controller extends GenericController implements ExportableInterface {
// The controller does not use this trait. It's here for API backward compatibility. use OrderAwareControllerTrait;
/** * Exportable traits. */ use ExportableTraits;
/** * Route base. * * @var string */ protected $rest_base = 'reports/variations';
/** * Mapping between external parameter name and name used in query class. * * @var array */ protected $param_mapping = array( 'variations' => 'variation_includes', 'products' => 'product_includes', );
/** * Get data from `'variations'` GenericQuery. * * @override GenericController::get_datastore_data() * * @param array $query_args Query arguments. * @return mixed Results from the data store. */ protected function get_datastore_data( $query_args = array() ) { $query = new GenericQuery( $query_args, 'variations' ); return $query->get_data(); }
/** * Prepare a report data item for serialization. * * @param array $report Report data item as returned from Data Store. * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function prepare_item_for_response( $report, $request ) { // Wrap the data in a response object. $response = parent::prepare_item_for_response( $report, $request );
$response->add_links( $this->prepare_links( $report ) );
/** * Filter a report returned from the API. * * Allows modification of the report data right before it is returned. * * @since 6.5.0 * * @param WP_REST_Response $response The response object. * @param object $report The original report object. * @param WP_REST_Request $request Request used to generate the response. */ return apply_filters( 'woocommerce_rest_prepare_report_variations', $response, $report, $request ); }
/** * Maps query arguments from the REST request. * * @param array $request Request array. * @return array */ protected function prepare_reports_query( $request ) { $args = array(); /** * Experimental: Filter the list of parameters provided when querying data from the data store. * * @ignore * * @param array $collection_params List of parameters. * * @since 6.5.0 */ $collection_params = apply_filters( 'experimental_woocommerce_analytics_variations_collection_params', $this->get_collection_params() ); $registered = array_keys( $collection_params ); foreach ( $registered as $param_name ) { if ( isset( $request[ $param_name ] ) ) { if ( isset( $this->param_mapping[ $param_name ] ) ) { $args[ $this->param_mapping[ $param_name ] ] = $request[ $param_name ]; } else { $args[ $param_name ] = $request[ $param_name ]; } } } return $args; }
/** * Prepare links for the request. * * @param array $object Object data. * @return array Links for the given post. */ protected function prepare_links( $object ) { $links = array( 'product' => array( 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, 'products', $object['product_id'] ) ), ), 'variation' => array( 'href' => rest_url( sprintf( '/%s/%s/%d/%s/%d', $this->namespace, 'products', $object['product_id'], 'variation', $object['variation_id'] ) ), ), );
return $links; }
/** * Get the Report's schema, conforming to JSON Schema. * * @return array */ public function get_item_schema() { $schema = array( '$schema' => 'http://json-schema.org/draft-04/schema#', 'title' => 'report_varitations', 'type' => 'object', 'properties' => array( 'product_id' => array( 'type' => 'integer', 'readonly' => true, 'context' => array( 'view', 'edit' ), 'description' => __( 'Product ID.', 'woocommerce' ), ), 'variation_id' => array( 'type' => 'integer', 'readonly' => true, 'context' => array( 'view', 'edit' ), 'description' => __( 'Product ID.', 'woocommerce' ), ), 'items_sold' => array( 'type' => 'integer', 'readonly' => true, 'context' => array( 'view', 'edit' ), 'description' => __( 'Number of items sold.', 'woocommerce' ), ), 'net_revenue' => array( 'type' => 'number', 'readonly' => true, 'context' => array( 'view', 'edit' ), 'description' => __( 'Total Net sales of all items sold.', 'woocommerce' ), ), 'orders_count' => array( 'type' => 'integer', 'readonly' => true, 'context' => array( 'view', 'edit' ), 'description' => __( 'Number of orders product appeared in.', 'woocommerce' ), ), 'extended_info' => array( 'name' => array( 'type' => 'string', 'readonly' => true, 'context' => array( 'view', 'edit' ), 'description' => __( 'Product name.', 'woocommerce' ), ), 'price' => array( 'type' => 'number', 'readonly' => true, 'context' => array( 'view', 'edit' ), 'description' => __( 'Product price.', 'woocommerce' ), ), 'image' => array( 'type' => 'string', 'readonly' => true, 'context' => array( 'view', 'edit' ), 'description' => __( 'Product image.', 'woocommerce' ), ), 'permalink' => array( 'type' => 'string', 'readonly' => true, 'context' => array( 'view', 'edit' ), 'description' => __( 'Product link.', 'woocommerce' ), ), 'attributes' => array( 'type' => 'array', 'readonly' => true, 'context' => array( 'view', 'edit' ), 'description' => __( 'Product attributes.', 'woocommerce' ), ), 'stock_status' => array( 'type' => 'string', 'readonly' => true, 'context' => array( 'view', 'edit' ), 'description' => __( 'Product inventory status.', 'woocommerce' ), ), 'stock_quantity' => array( 'type' => 'integer', 'readonly' => true, 'context' => array( 'view', 'edit' ), 'description' => __( 'Product inventory quantity.', 'woocommerce' ), ), 'low_stock_amount' => array( 'type' => 'integer', 'readonly' => true, 'context' => array( 'view', 'edit' ), 'description' => __( 'Product inventory threshold for low stock.', 'woocommerce' ), ), ), ), );
return $this->add_additional_fields_schema( $schema ); }
/** * Get the query params for collections. * * @return array */ public function get_collection_params() { $params = parent::get_collection_params(); $params['orderby']['enum'] = $this->apply_custom_orderby_filters( array( 'date', 'net_revenue', 'orders_count', 'items_sold', 'sku', ) ); $params['match'] = array( 'description' => __( 'Indicates whether all the conditions should be true for the resulting set, or if any one of them is sufficient. Match affects the following parameters: status_is, status_is_not, product_includes, product_excludes, coupon_includes, coupon_excludes, customer, categories', 'woocommerce' ), 'type' => 'string', 'default' => 'all', 'enum' => array( 'all', 'any', ), 'validate_callback' => 'rest_validate_request_arg', ); $params['product_includes'] = array( 'description' => __( 'Limit result set to items that have the specified parent product(s).', 'woocommerce' ), 'type' => 'array', 'items' => array( 'type' => 'integer', ), 'default' => array(), 'sanitize_callback' => 'wp_parse_id_list', 'validate_callback' => 'rest_validate_request_arg', ); $params['product_excludes'] = array( 'description' => __( 'Limit result set to items that don\'t have the specified parent product(s).', 'woocommerce' ), 'type' => 'array', 'items' => array( 'type' => 'integer', ), 'default' => array(), 'validate_callback' => 'rest_validate_request_arg', 'sanitize_callback' => 'wp_parse_id_list', ); $params['variations'] = array( 'description' => __( 'Limit result to items with specified variation ids.', 'woocommerce' ), 'type' => 'array', 'sanitize_callback' => 'wp_parse_id_list', 'validate_callback' => 'rest_validate_request_arg', 'items' => array( 'type' => 'integer', ), ); $params['extended_info'] = array( 'description' => __( 'Add additional piece of info about each variation to the report.', 'woocommerce' ), 'type' => 'boolean', 'default' => false, 'sanitize_callback' => 'wc_string_to_bool', 'validate_callback' => 'rest_validate_request_arg', ); $params['attribute_is'] = array( 'description' => __( 'Limit result set to variations that include the specified attributes.', 'woocommerce' ), 'type' => 'array', 'items' => array( 'type' => 'array', ), 'default' => array(), 'validate_callback' => 'rest_validate_request_arg', ); $params['attribute_is_not'] = array( 'description' => __( 'Limit result set to variations that don\'t include the specified attributes.', 'woocommerce' ), 'type' => 'array', 'items' => array( 'type' => 'array', ), 'default' => array(), 'validate_callback' => 'rest_validate_request_arg', ); $params['category_includes'] = array( 'description' => __( 'Limit result set to variations in the specified categories.', 'woocommerce' ), 'type' => 'array', 'sanitize_callback' => 'wp_parse_id_list', 'validate_callback' => 'rest_validate_request_arg', 'items' => array( 'type' => 'integer', ), ); $params['category_excludes'] = array( 'description' => __( 'Limit result set to variations not in the specified categories.', 'woocommerce' ), 'type' => 'array', 'sanitize_callback' => 'wp_parse_id_list', 'validate_callback' => 'rest_validate_request_arg', 'items' => array( 'type' => 'integer', ), ); $params['products'] = array( 'description' => __( 'Limit result to items with specified product ids.', 'woocommerce' ), 'type' => 'array', 'sanitize_callback' => 'wp_parse_id_list', 'validate_callback' => 'rest_validate_request_arg', 'items' => array( 'type' => 'integer', ), );
return $params; }
/** * Get stock status column export value. * * @param array $status Stock status from report row. * @return string */ protected function get_stock_status( $status ) { $statuses = wc_get_product_stock_status_options();
return isset( $statuses[ $status ] ) ? $statuses[ $status ] : ''; }
/** * Get the column names for export. * * @return array Key value pair of Column ID => Label. */ public function get_export_columns() { $export_columns = array( 'product_name' => __( 'Product / Variation title', 'woocommerce' ), 'sku' => __( 'SKU', 'woocommerce' ), 'items_sold' => __( 'Items sold', 'woocommerce' ), 'net_revenue' => __( 'N. Revenue', 'woocommerce' ), 'orders_count' => __( 'Orders', 'woocommerce' ), );
if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) { $export_columns['stock_status'] = __( 'Status', 'woocommerce' ); $export_columns['stock'] = __( 'Stock', 'woocommerce' ); }
return $export_columns; }
/** * Get the column values for export. * * @param array $item Single report item/row. * @return array Key value pair of Column ID => Row Value. */ public function prepare_item_for_export( $item ) { $export_item = array( 'product_name' => $item['extended_info']['name'], 'sku' => $item['extended_info']['sku'], 'items_sold' => $item['items_sold'], 'net_revenue' => self::csv_number_format( $item['net_revenue'] ), 'orders_count' => $item['orders_count'], );
if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) { $export_item['stock_status'] = $this->get_stock_status( $item['extended_info']['stock_status'] ); $export_item['stock'] = $item['extended_info']['stock_quantity']; }
return $export_item; } }
|