Controller.php
521 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API Reports orders controller |
| 4 | * |
| 5 | * Handles requests to the /reports/orders endpoint. |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin\API\Reports\Orders; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface; |
| 13 | use Automattic\WooCommerce\Admin\API\Reports\GenericController; |
| 14 | use Automattic\WooCommerce\Admin\API\Reports\OrderAwareControllerTrait; |
| 15 | |
| 16 | /** |
| 17 | * REST API Reports orders controller class. |
| 18 | * |
| 19 | * @internal |
| 20 | * @extends \Automattic\WooCommerce\Admin\API\Reports\GenericController |
| 21 | */ |
| 22 | class Controller extends GenericController implements ExportableInterface { |
| 23 | |
| 24 | use OrderAwareControllerTrait; |
| 25 | |
| 26 | /** |
| 27 | * Route base. |
| 28 | * |
| 29 | * @var string |
| 30 | */ |
| 31 | protected $rest_base = 'reports/orders'; |
| 32 | |
| 33 | /** |
| 34 | * Get data from Orders\Query. |
| 35 | * |
| 36 | * @override GenericController::get_datastore_data() |
| 37 | * |
| 38 | * @param array $query_args Query arguments. |
| 39 | * @return mixed Results from the data store. |
| 40 | */ |
| 41 | protected function get_datastore_data( $query_args = array() ) { |
| 42 | $query = new Query( $query_args ); |
| 43 | return $query->get_data(); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Maps query arguments from the REST request. |
| 48 | * |
| 49 | * @param array $request Request array. |
| 50 | * @return array |
| 51 | */ |
| 52 | protected function prepare_reports_query( $request ) { |
| 53 | $args = array(); |
| 54 | $args['before'] = $request['before']; |
| 55 | $args['after'] = $request['after']; |
| 56 | $args['page'] = $request['page']; |
| 57 | $args['per_page'] = $request['per_page']; |
| 58 | $args['orderby'] = $request['orderby']; |
| 59 | $args['order'] = $request['order']; |
| 60 | $args['product_includes'] = (array) $request['product_includes']; |
| 61 | $args['product_excludes'] = (array) $request['product_excludes']; |
| 62 | $args['variation_includes'] = (array) $request['variation_includes']; |
| 63 | $args['variation_excludes'] = (array) $request['variation_excludes']; |
| 64 | $args['coupon_includes'] = (array) $request['coupon_includes']; |
| 65 | $args['coupon_excludes'] = (array) $request['coupon_excludes']; |
| 66 | $args['tax_rate_includes'] = (array) $request['tax_rate_includes']; |
| 67 | $args['tax_rate_excludes'] = (array) $request['tax_rate_excludes']; |
| 68 | $args['status_is'] = (array) $request['status_is']; |
| 69 | $args['status_is_not'] = (array) $request['status_is_not']; |
| 70 | $args['customer_type'] = $request['customer_type']; |
| 71 | $args['extended_info'] = $request['extended_info']; |
| 72 | $args['refunds'] = $request['refunds']; |
| 73 | $args['match'] = $request['match']; |
| 74 | $args['order_includes'] = $request['order_includes']; |
| 75 | $args['order_excludes'] = $request['order_excludes']; |
| 76 | $args['attribute_is'] = (array) $request['attribute_is']; |
| 77 | $args['attribute_is_not'] = (array) $request['attribute_is_not']; |
| 78 | $args['force_cache_refresh'] = $request['force_cache_refresh']; |
| 79 | |
| 80 | return $args; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Prepare a report data item for serialization. |
| 85 | * |
| 86 | * @param array $report Report data item as returned from Data Store. |
| 87 | * @param \WP_REST_Request $request Request object. |
| 88 | * @return \WP_REST_Response |
| 89 | */ |
| 90 | public function prepare_item_for_response( $report, $request ) { |
| 91 | $report['order_number'] = $this->get_order_number( $report['order_id'] ); |
| 92 | $report['total_formatted'] = $this->get_total_formatted( $report['order_id'] ); |
| 93 | // Wrap the data in a response object. |
| 94 | $response = parent::prepare_item_for_response( $report, $request ); |
| 95 | $response->add_links( $this->prepare_links( $report ) ); |
| 96 | |
| 97 | /** |
| 98 | * Filter a report returned from the API. |
| 99 | * |
| 100 | * Allows modification of the report data right before it is returned. |
| 101 | * |
| 102 | * @param WP_REST_Response $response The response object. |
| 103 | * @param object $report The original report object. |
| 104 | * @param WP_REST_Request $request Request used to generate the response. |
| 105 | */ |
| 106 | return apply_filters( 'woocommerce_rest_prepare_report_orders', $response, $report, $request ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Prepare links for the request. |
| 111 | * |
| 112 | * @param WC_Reports_Query $object Object data. |
| 113 | * @return array |
| 114 | */ |
| 115 | protected function prepare_links( $object ) { |
| 116 | $links = array( |
| 117 | 'order' => array( |
| 118 | 'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $object['order_id'] ) ), |
| 119 | ), |
| 120 | ); |
| 121 | |
| 122 | return $links; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get the Report's schema, conforming to JSON Schema. |
| 127 | * |
| 128 | * @return array |
| 129 | */ |
| 130 | public function get_item_schema() { |
| 131 | $schema = array( |
| 132 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 133 | 'title' => 'report_orders', |
| 134 | 'type' => 'object', |
| 135 | 'properties' => array( |
| 136 | 'order_id' => array( |
| 137 | 'description' => __( 'Order ID.', 'woocommerce' ), |
| 138 | 'type' => 'integer', |
| 139 | 'context' => array( 'view', 'edit' ), |
| 140 | 'readonly' => true, |
| 141 | ), |
| 142 | 'order_number' => array( |
| 143 | 'description' => __( 'Order Number.', 'woocommerce' ), |
| 144 | 'type' => 'string', |
| 145 | 'context' => array( 'view', 'edit' ), |
| 146 | 'readonly' => true, |
| 147 | ), |
| 148 | 'date_created' => array( |
| 149 | 'description' => __( "Date the order was created, in the site's timezone.", 'woocommerce' ), |
| 150 | 'type' => 'date-time', |
| 151 | 'context' => array( 'view', 'edit' ), |
| 152 | 'readonly' => true, |
| 153 | ), |
| 154 | 'date_created_gmt' => array( |
| 155 | 'description' => __( 'Date the order was created, as GMT.', 'woocommerce' ), |
| 156 | 'type' => 'date-time', |
| 157 | 'context' => array( 'view', 'edit' ), |
| 158 | 'readonly' => true, |
| 159 | ), |
| 160 | 'status' => array( |
| 161 | 'description' => __( 'Order status.', 'woocommerce' ), |
| 162 | 'type' => 'string', |
| 163 | 'context' => array( 'view', 'edit' ), |
| 164 | 'readonly' => true, |
| 165 | ), |
| 166 | 'customer_id' => array( |
| 167 | 'description' => __( 'Customer ID.', 'woocommerce' ), |
| 168 | 'type' => 'integer', |
| 169 | 'context' => array( 'view', 'edit' ), |
| 170 | 'readonly' => true, |
| 171 | ), |
| 172 | 'num_items_sold' => array( |
| 173 | 'description' => __( 'Number of items sold.', 'woocommerce' ), |
| 174 | 'type' => 'integer', |
| 175 | 'context' => array( 'view', 'edit' ), |
| 176 | 'readonly' => true, |
| 177 | ), |
| 178 | 'net_total' => array( |
| 179 | 'description' => __( 'Net total revenue.', 'woocommerce' ), |
| 180 | 'type' => 'float', |
| 181 | 'context' => array( 'view', 'edit' ), |
| 182 | 'readonly' => true, |
| 183 | ), |
| 184 | 'total_formatted' => array( |
| 185 | 'description' => __( 'Net total revenue (formatted).', 'woocommerce' ), |
| 186 | 'type' => 'string', |
| 187 | 'context' => array( 'view', 'edit' ), |
| 188 | 'readonly' => true, |
| 189 | ), |
| 190 | 'customer_type' => array( |
| 191 | 'description' => __( 'Returning or new customer.', 'woocommerce' ), |
| 192 | 'type' => 'string', |
| 193 | 'context' => array( 'view', 'edit' ), |
| 194 | 'readonly' => true, |
| 195 | ), |
| 196 | 'extended_info' => array( |
| 197 | 'products' => array( |
| 198 | 'type' => 'array', |
| 199 | 'readonly' => true, |
| 200 | 'context' => array( 'view', 'edit' ), |
| 201 | 'description' => __( 'List of order product IDs, names, quantities.', 'woocommerce' ), |
| 202 | ), |
| 203 | 'coupons' => array( |
| 204 | 'type' => 'array', |
| 205 | 'readonly' => true, |
| 206 | 'context' => array( 'view', 'edit' ), |
| 207 | 'description' => __( 'List of order coupons.', 'woocommerce' ), |
| 208 | ), |
| 209 | 'customer' => array( |
| 210 | 'type' => 'object', |
| 211 | 'readonly' => true, |
| 212 | 'context' => array( 'view', 'edit' ), |
| 213 | 'description' => __( 'Order customer information.', 'woocommerce' ), |
| 214 | ), |
| 215 | 'attribution' => array( |
| 216 | 'type' => 'object', |
| 217 | 'readonly' => true, |
| 218 | 'context' => array( 'view', 'edit' ), |
| 219 | 'description' => __( 'Order attribution information.', 'woocommerce' ), |
| 220 | ), |
| 221 | ), |
| 222 | ), |
| 223 | ); |
| 224 | |
| 225 | return $this->add_additional_fields_schema( $schema ); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Get the query params for collections. |
| 230 | * |
| 231 | * @return array |
| 232 | */ |
| 233 | public function get_collection_params() { |
| 234 | $params = parent::get_collection_params(); |
| 235 | $params['per_page']['minimum'] = 0; |
| 236 | $params['orderby']['enum'] = $this->apply_custom_orderby_filters( |
| 237 | array( |
| 238 | 'date', |
| 239 | 'num_items_sold', |
| 240 | 'net_total', |
| 241 | ) |
| 242 | ); |
| 243 | $params['product_includes'] = array( |
| 244 | 'description' => __( 'Limit result set to items that have the specified product(s) assigned.', 'woocommerce' ), |
| 245 | 'type' => 'array', |
| 246 | 'items' => array( |
| 247 | 'type' => 'integer', |
| 248 | ), |
| 249 | 'default' => array(), |
| 250 | 'sanitize_callback' => 'wp_parse_id_list', |
| 251 | 'validate_callback' => 'rest_validate_request_arg', |
| 252 | ); |
| 253 | $params['product_excludes'] = array( |
| 254 | 'description' => __( 'Limit result set to items that don\'t have the specified product(s) assigned.', 'woocommerce' ), |
| 255 | 'type' => 'array', |
| 256 | 'items' => array( |
| 257 | 'type' => 'integer', |
| 258 | ), |
| 259 | 'default' => array(), |
| 260 | 'validate_callback' => 'rest_validate_request_arg', |
| 261 | 'sanitize_callback' => 'wp_parse_id_list', |
| 262 | ); |
| 263 | $params['variation_includes'] = array( |
| 264 | 'description' => __( 'Limit result set to items that have the specified variation(s) assigned.', 'woocommerce' ), |
| 265 | 'type' => 'array', |
| 266 | 'items' => array( |
| 267 | 'type' => 'integer', |
| 268 | ), |
| 269 | 'default' => array(), |
| 270 | 'sanitize_callback' => 'wp_parse_id_list', |
| 271 | 'validate_callback' => 'rest_validate_request_arg', |
| 272 | ); |
| 273 | $params['variation_excludes'] = array( |
| 274 | 'description' => __( 'Limit result set to items that don\'t have the specified variation(s) assigned.', 'woocommerce' ), |
| 275 | 'type' => 'array', |
| 276 | 'items' => array( |
| 277 | 'type' => 'integer', |
| 278 | ), |
| 279 | 'default' => array(), |
| 280 | 'validate_callback' => 'rest_validate_request_arg', |
| 281 | 'sanitize_callback' => 'wp_parse_id_list', |
| 282 | ); |
| 283 | $params['coupon_includes'] = array( |
| 284 | 'description' => __( 'Limit result set to items that have the specified coupon(s) assigned.', 'woocommerce' ), |
| 285 | 'type' => 'array', |
| 286 | 'items' => array( |
| 287 | 'type' => 'integer', |
| 288 | ), |
| 289 | 'default' => array(), |
| 290 | 'sanitize_callback' => 'wp_parse_id_list', |
| 291 | 'validate_callback' => 'rest_validate_request_arg', |
| 292 | ); |
| 293 | $params['coupon_excludes'] = array( |
| 294 | 'description' => __( 'Limit result set to items that don\'t have the specified coupon(s) assigned.', 'woocommerce' ), |
| 295 | 'type' => 'array', |
| 296 | 'items' => array( |
| 297 | 'type' => 'integer', |
| 298 | ), |
| 299 | 'default' => array(), |
| 300 | 'validate_callback' => 'rest_validate_request_arg', |
| 301 | 'sanitize_callback' => 'wp_parse_id_list', |
| 302 | ); |
| 303 | $params['tax_rate_includes'] = array( |
| 304 | 'description' => __( 'Limit result set to items that have the specified tax rate(s) assigned.', 'woocommerce' ), |
| 305 | 'type' => 'array', |
| 306 | 'items' => array( |
| 307 | 'type' => 'integer', |
| 308 | ), |
| 309 | 'default' => array(), |
| 310 | 'sanitize_callback' => 'wp_parse_id_list', |
| 311 | 'validate_callback' => 'rest_validate_request_arg', |
| 312 | ); |
| 313 | $params['tax_rate_excludes'] = array( |
| 314 | 'description' => __( 'Limit result set to items that don\'t have the specified tax rate(s) assigned.', 'woocommerce' ), |
| 315 | 'type' => 'array', |
| 316 | 'items' => array( |
| 317 | 'type' => 'integer', |
| 318 | ), |
| 319 | 'default' => array(), |
| 320 | 'validate_callback' => 'rest_validate_request_arg', |
| 321 | 'sanitize_callback' => 'wp_parse_id_list', |
| 322 | ); |
| 323 | $params['status_is'] = array( |
| 324 | 'description' => __( 'Limit result set to items that have the specified order status.', 'woocommerce' ), |
| 325 | 'type' => 'array', |
| 326 | 'sanitize_callback' => 'wp_parse_slug_list', |
| 327 | 'validate_callback' => 'rest_validate_request_arg', |
| 328 | 'items' => array( |
| 329 | 'enum' => self::get_order_statuses(), |
| 330 | 'type' => 'string', |
| 331 | ), |
| 332 | ); |
| 333 | $params['status_is_not'] = array( |
| 334 | 'description' => __( 'Limit result set to items that don\'t have the specified order status.', 'woocommerce' ), |
| 335 | 'type' => 'array', |
| 336 | 'sanitize_callback' => 'wp_parse_slug_list', |
| 337 | 'validate_callback' => 'rest_validate_request_arg', |
| 338 | 'items' => array( |
| 339 | 'enum' => self::get_order_statuses(), |
| 340 | 'type' => 'string', |
| 341 | ), |
| 342 | ); |
| 343 | $params['customer_type'] = array( |
| 344 | 'description' => __( 'Limit result set to returning or new customers.', 'woocommerce' ), |
| 345 | 'type' => 'string', |
| 346 | 'default' => '', |
| 347 | 'enum' => array( |
| 348 | '', |
| 349 | 'returning', |
| 350 | 'new', |
| 351 | ), |
| 352 | 'validate_callback' => 'rest_validate_request_arg', |
| 353 | ); |
| 354 | $params['refunds'] = array( |
| 355 | 'description' => __( 'Limit result set to specific types of refunds.', 'woocommerce' ), |
| 356 | 'type' => 'string', |
| 357 | 'default' => '', |
| 358 | 'enum' => array( |
| 359 | '', |
| 360 | 'all', |
| 361 | 'partial', |
| 362 | 'full', |
| 363 | 'none', |
| 364 | ), |
| 365 | 'validate_callback' => 'rest_validate_request_arg', |
| 366 | ); |
| 367 | $params['extended_info'] = array( |
| 368 | 'description' => __( 'Add additional piece of info about each coupon to the report.', 'woocommerce' ), |
| 369 | 'type' => 'boolean', |
| 370 | 'default' => false, |
| 371 | 'sanitize_callback' => 'wc_string_to_bool', |
| 372 | 'validate_callback' => 'rest_validate_request_arg', |
| 373 | ); |
| 374 | $params['order_includes'] = array( |
| 375 | 'description' => __( 'Limit result set to items that have the specified order ids.', 'woocommerce' ), |
| 376 | 'type' => 'array', |
| 377 | 'sanitize_callback' => 'wp_parse_id_list', |
| 378 | 'validate_callback' => 'rest_validate_request_arg', |
| 379 | 'items' => array( |
| 380 | 'type' => 'integer', |
| 381 | ), |
| 382 | ); |
| 383 | $params['order_excludes'] = array( |
| 384 | 'description' => __( 'Limit result set to items that don\'t have the specified order ids.', 'woocommerce' ), |
| 385 | 'type' => 'array', |
| 386 | 'sanitize_callback' => 'wp_parse_id_list', |
| 387 | 'validate_callback' => 'rest_validate_request_arg', |
| 388 | 'items' => array( |
| 389 | 'type' => 'integer', |
| 390 | ), |
| 391 | ); |
| 392 | $params['attribute_is'] = array( |
| 393 | 'description' => __( 'Limit result set to orders that include products with the specified attributes.', 'woocommerce' ), |
| 394 | 'type' => 'array', |
| 395 | 'items' => array( |
| 396 | 'type' => 'array', |
| 397 | ), |
| 398 | 'default' => array(), |
| 399 | 'validate_callback' => 'rest_validate_request_arg', |
| 400 | ); |
| 401 | $params['attribute_is_not'] = array( |
| 402 | 'description' => __( 'Limit result set to orders that don\'t include products with the specified attributes.', 'woocommerce' ), |
| 403 | 'type' => 'array', |
| 404 | 'items' => array( |
| 405 | 'type' => 'array', |
| 406 | ), |
| 407 | 'default' => array(), |
| 408 | 'validate_callback' => 'rest_validate_request_arg', |
| 409 | ); |
| 410 | |
| 411 | return $params; |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Get customer name column export value. |
| 416 | * |
| 417 | * @param array $customer Customer from report row. |
| 418 | * @return string |
| 419 | */ |
| 420 | protected function get_customer_name( $customer ) { |
| 421 | return $customer['first_name'] . ' ' . $customer['last_name']; |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Get products column export value. |
| 426 | * |
| 427 | * @param array $products Products from report row. |
| 428 | * @return string |
| 429 | */ |
| 430 | protected function get_products( $products ) { |
| 431 | $products_list = array(); |
| 432 | |
| 433 | foreach ( $products as $product ) { |
| 434 | $products_list[] = sprintf( |
| 435 | /* translators: 1: numeric product quantity, 2: name of product */ |
| 436 | __( '%1$s× %2$s', 'woocommerce' ), |
| 437 | $product['quantity'], |
| 438 | $product['name'] |
| 439 | ); |
| 440 | } |
| 441 | |
| 442 | return implode( ', ', $products_list ); |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * Get coupons column export value. |
| 447 | * |
| 448 | * @param array $coupons Coupons from report row. |
| 449 | * @return string |
| 450 | */ |
| 451 | protected function get_coupons( $coupons ) { |
| 452 | return implode( ', ', wp_list_pluck( $coupons, 'code' ) ); |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Get the column names for export. |
| 457 | * |
| 458 | * @return array Key value pair of Column ID => Label. |
| 459 | */ |
| 460 | public function get_export_columns() { |
| 461 | $export_columns = array( |
| 462 | 'date_created' => __( 'Date', 'woocommerce' ), |
| 463 | 'order_number' => __( 'Order #', 'woocommerce' ), |
| 464 | 'total_formatted' => __( 'N. Revenue (formatted)', 'woocommerce' ), |
| 465 | 'status' => __( 'Status', 'woocommerce' ), |
| 466 | 'customer_name' => __( 'Customer', 'woocommerce' ), |
| 467 | 'customer_type' => __( 'Customer type', 'woocommerce' ), |
| 468 | 'products' => __( 'Product(s)', 'woocommerce' ), |
| 469 | 'num_items_sold' => __( 'Items sold', 'woocommerce' ), |
| 470 | 'coupons' => __( 'Coupon(s)', 'woocommerce' ), |
| 471 | 'net_total' => __( 'Net Sales', 'woocommerce' ), |
| 472 | 'attribution' => __( 'Attribution', 'woocommerce' ), |
| 473 | ); |
| 474 | |
| 475 | /** |
| 476 | * Filter to add or remove column names from the orders report for |
| 477 | * export. |
| 478 | * |
| 479 | * @since 1.6.0 |
| 480 | */ |
| 481 | return apply_filters( |
| 482 | 'woocommerce_report_orders_export_columns', |
| 483 | $export_columns |
| 484 | ); |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * Get the column values for export. |
| 489 | * |
| 490 | * @param array $item Single report item/row. |
| 491 | * @return array Key value pair of Column ID => Row Value. |
| 492 | */ |
| 493 | public function prepare_item_for_export( $item ) { |
| 494 | $export_item = array( |
| 495 | 'date_created' => $item['date'], |
| 496 | 'order_number' => $item['order_number'], |
| 497 | 'total_formatted' => $item['total_formatted'], |
| 498 | 'status' => $item['status'], |
| 499 | 'customer_name' => isset( $item['extended_info']['customer'] ) ? $this->get_customer_name( $item['extended_info']['customer'] ) : null, |
| 500 | 'customer_type' => $item['customer_type'], |
| 501 | 'products' => isset( $item['extended_info']['products'] ) ? $this->get_products( $item['extended_info']['products'] ) : null, |
| 502 | 'num_items_sold' => $item['num_items_sold'], |
| 503 | 'coupons' => isset( $item['extended_info']['coupons'] ) ? $this->get_coupons( $item['extended_info']['coupons'] ) : null, |
| 504 | 'net_total' => $item['net_total'], |
| 505 | 'attribution' => $item['extended_info']['attribution']['origin'], |
| 506 | ); |
| 507 | |
| 508 | /** |
| 509 | * Filter to prepare extra columns in the export item for the orders |
| 510 | * report. |
| 511 | * |
| 512 | * @since 1.6.0 |
| 513 | */ |
| 514 | return apply_filters( |
| 515 | 'woocommerce_report_orders_prepare_export_item', |
| 516 | $export_item, |
| 517 | $item |
| 518 | ); |
| 519 | } |
| 520 | } |
| 521 |