Controller.php
448 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API Reports products controller |
| 4 | * |
| 5 | * Handles requests to the /reports/products endpoint. |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin\API\Reports\Variations; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface; |
| 13 | use Automattic\WooCommerce\Admin\API\Reports\ExportableTraits; |
| 14 | use Automattic\WooCommerce\Admin\API\Reports\GenericController; |
| 15 | use Automattic\WooCommerce\Admin\API\Reports\GenericQuery; |
| 16 | use Automattic\WooCommerce\Admin\API\Reports\OrderAwareControllerTrait; |
| 17 | |
| 18 | |
| 19 | /** |
| 20 | * REST API Reports products controller class. |
| 21 | * |
| 22 | * @internal |
| 23 | * @extends GenericController |
| 24 | */ |
| 25 | class Controller extends GenericController implements ExportableInterface { |
| 26 | |
| 27 | // The controller does not use this trait. It's here for API backward compatibility. |
| 28 | use OrderAwareControllerTrait; |
| 29 | |
| 30 | /** |
| 31 | * Exportable traits. |
| 32 | */ |
| 33 | use ExportableTraits; |
| 34 | |
| 35 | /** |
| 36 | * Route base. |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | protected $rest_base = 'reports/variations'; |
| 41 | |
| 42 | /** |
| 43 | * Mapping between external parameter name and name used in query class. |
| 44 | * |
| 45 | * @var array |
| 46 | */ |
| 47 | protected $param_mapping = array( |
| 48 | 'variations' => 'variation_includes', |
| 49 | 'products' => 'product_includes', |
| 50 | ); |
| 51 | |
| 52 | /** |
| 53 | * Get data from `'variations'` GenericQuery. |
| 54 | * |
| 55 | * @override GenericController::get_datastore_data() |
| 56 | * |
| 57 | * @param array $query_args Query arguments. |
| 58 | * @return mixed Results from the data store. |
| 59 | */ |
| 60 | protected function get_datastore_data( $query_args = array() ) { |
| 61 | $query = new GenericQuery( $query_args, 'variations' ); |
| 62 | return $query->get_data(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Prepare a report data item for serialization. |
| 67 | * |
| 68 | * @param array $report Report data item as returned from Data Store. |
| 69 | * @param WP_REST_Request $request Request object. |
| 70 | * @return WP_REST_Response |
| 71 | */ |
| 72 | public function prepare_item_for_response( $report, $request ) { |
| 73 | // Wrap the data in a response object. |
| 74 | $response = parent::prepare_item_for_response( $report, $request ); |
| 75 | |
| 76 | $response->add_links( $this->prepare_links( $report ) ); |
| 77 | |
| 78 | /** |
| 79 | * Filter a report returned from the API. |
| 80 | * |
| 81 | * Allows modification of the report data right before it is returned. |
| 82 | * |
| 83 | * @since 6.5.0 |
| 84 | * |
| 85 | * @param WP_REST_Response $response The response object. |
| 86 | * @param object $report The original report object. |
| 87 | * @param WP_REST_Request $request Request used to generate the response. |
| 88 | */ |
| 89 | return apply_filters( 'woocommerce_rest_prepare_report_variations', $response, $report, $request ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Maps query arguments from the REST request. |
| 94 | * |
| 95 | * @param array $request Request array. |
| 96 | * @return array |
| 97 | */ |
| 98 | protected function prepare_reports_query( $request ) { |
| 99 | $args = array(); |
| 100 | /** |
| 101 | * Experimental: Filter the list of parameters provided when querying data from the data store. |
| 102 | * |
| 103 | * @ignore |
| 104 | * |
| 105 | * @param array $collection_params List of parameters. |
| 106 | * |
| 107 | * @since 6.5.0 |
| 108 | */ |
| 109 | $collection_params = apply_filters( |
| 110 | 'experimental_woocommerce_analytics_variations_collection_params', |
| 111 | $this->get_collection_params() |
| 112 | ); |
| 113 | $registered = array_keys( $collection_params ); |
| 114 | foreach ( $registered as $param_name ) { |
| 115 | if ( isset( $request[ $param_name ] ) ) { |
| 116 | if ( isset( $this->param_mapping[ $param_name ] ) ) { |
| 117 | $args[ $this->param_mapping[ $param_name ] ] = $request[ $param_name ]; |
| 118 | } else { |
| 119 | $args[ $param_name ] = $request[ $param_name ]; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | return $args; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Prepare links for the request. |
| 128 | * |
| 129 | * @param array $object Object data. |
| 130 | * @return array Links for the given post. |
| 131 | */ |
| 132 | protected function prepare_links( $object ) { |
| 133 | $links = array( |
| 134 | 'product' => array( |
| 135 | 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, 'products', $object['product_id'] ) ), |
| 136 | ), |
| 137 | 'variation' => array( |
| 138 | 'href' => rest_url( sprintf( '/%s/%s/%d/%s/%d', $this->namespace, 'products', $object['product_id'], 'variation', $object['variation_id'] ) ), |
| 139 | ), |
| 140 | ); |
| 141 | |
| 142 | return $links; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Get the Report's schema, conforming to JSON Schema. |
| 147 | * |
| 148 | * @return array |
| 149 | */ |
| 150 | public function get_item_schema() { |
| 151 | $schema = array( |
| 152 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 153 | 'title' => 'report_varitations', |
| 154 | 'type' => 'object', |
| 155 | 'properties' => array( |
| 156 | 'product_id' => array( |
| 157 | 'type' => 'integer', |
| 158 | 'readonly' => true, |
| 159 | 'context' => array( 'view', 'edit' ), |
| 160 | 'description' => __( 'Product ID.', 'woocommerce' ), |
| 161 | ), |
| 162 | 'variation_id' => array( |
| 163 | 'type' => 'integer', |
| 164 | 'readonly' => true, |
| 165 | 'context' => array( 'view', 'edit' ), |
| 166 | 'description' => __( 'Product ID.', 'woocommerce' ), |
| 167 | ), |
| 168 | 'items_sold' => array( |
| 169 | 'type' => 'integer', |
| 170 | 'readonly' => true, |
| 171 | 'context' => array( 'view', 'edit' ), |
| 172 | 'description' => __( 'Number of items sold.', 'woocommerce' ), |
| 173 | ), |
| 174 | 'net_revenue' => array( |
| 175 | 'type' => 'number', |
| 176 | 'readonly' => true, |
| 177 | 'context' => array( 'view', 'edit' ), |
| 178 | 'description' => __( 'Total Net sales of all items sold.', 'woocommerce' ), |
| 179 | ), |
| 180 | 'orders_count' => array( |
| 181 | 'type' => 'integer', |
| 182 | 'readonly' => true, |
| 183 | 'context' => array( 'view', 'edit' ), |
| 184 | 'description' => __( 'Number of orders product appeared in.', 'woocommerce' ), |
| 185 | ), |
| 186 | 'extended_info' => array( |
| 187 | 'name' => array( |
| 188 | 'type' => 'string', |
| 189 | 'readonly' => true, |
| 190 | 'context' => array( 'view', 'edit' ), |
| 191 | 'description' => __( 'Product name.', 'woocommerce' ), |
| 192 | ), |
| 193 | 'price' => array( |
| 194 | 'type' => 'number', |
| 195 | 'readonly' => true, |
| 196 | 'context' => array( 'view', 'edit' ), |
| 197 | 'description' => __( 'Product price.', 'woocommerce' ), |
| 198 | ), |
| 199 | 'image' => array( |
| 200 | 'type' => 'string', |
| 201 | 'readonly' => true, |
| 202 | 'context' => array( 'view', 'edit' ), |
| 203 | 'description' => __( 'Product image.', 'woocommerce' ), |
| 204 | ), |
| 205 | 'permalink' => array( |
| 206 | 'type' => 'string', |
| 207 | 'readonly' => true, |
| 208 | 'context' => array( 'view', 'edit' ), |
| 209 | 'description' => __( 'Product link.', 'woocommerce' ), |
| 210 | ), |
| 211 | 'attributes' => array( |
| 212 | 'type' => 'array', |
| 213 | 'readonly' => true, |
| 214 | 'context' => array( 'view', 'edit' ), |
| 215 | 'description' => __( 'Product attributes.', 'woocommerce' ), |
| 216 | ), |
| 217 | 'stock_status' => array( |
| 218 | 'type' => 'string', |
| 219 | 'readonly' => true, |
| 220 | 'context' => array( 'view', 'edit' ), |
| 221 | 'description' => __( 'Product inventory status.', 'woocommerce' ), |
| 222 | ), |
| 223 | 'stock_quantity' => array( |
| 224 | 'type' => 'integer', |
| 225 | 'readonly' => true, |
| 226 | 'context' => array( 'view', 'edit' ), |
| 227 | 'description' => __( 'Product inventory quantity.', 'woocommerce' ), |
| 228 | ), |
| 229 | 'low_stock_amount' => array( |
| 230 | 'type' => 'integer', |
| 231 | 'readonly' => true, |
| 232 | 'context' => array( 'view', 'edit' ), |
| 233 | 'description' => __( 'Product inventory threshold for low stock.', 'woocommerce' ), |
| 234 | ), |
| 235 | ), |
| 236 | ), |
| 237 | ); |
| 238 | |
| 239 | return $this->add_additional_fields_schema( $schema ); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Get the query params for collections. |
| 244 | * |
| 245 | * @return array |
| 246 | */ |
| 247 | public function get_collection_params() { |
| 248 | $params = parent::get_collection_params(); |
| 249 | $params['orderby']['enum'] = $this->apply_custom_orderby_filters( |
| 250 | array( |
| 251 | 'date', |
| 252 | 'net_revenue', |
| 253 | 'orders_count', |
| 254 | 'items_sold', |
| 255 | 'sku', |
| 256 | ) |
| 257 | ); |
| 258 | $params['match'] = array( |
| 259 | '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' ), |
| 260 | 'type' => 'string', |
| 261 | 'default' => 'all', |
| 262 | 'enum' => array( |
| 263 | 'all', |
| 264 | 'any', |
| 265 | ), |
| 266 | 'validate_callback' => 'rest_validate_request_arg', |
| 267 | ); |
| 268 | $params['product_includes'] = array( |
| 269 | 'description' => __( 'Limit result set to items that have the specified parent product(s).', 'woocommerce' ), |
| 270 | 'type' => 'array', |
| 271 | 'items' => array( |
| 272 | 'type' => 'integer', |
| 273 | ), |
| 274 | 'default' => array(), |
| 275 | 'sanitize_callback' => 'wp_parse_id_list', |
| 276 | 'validate_callback' => 'rest_validate_request_arg', |
| 277 | ); |
| 278 | $params['product_excludes'] = array( |
| 279 | 'description' => __( 'Limit result set to items that don\'t have the specified parent product(s).', 'woocommerce' ), |
| 280 | 'type' => 'array', |
| 281 | 'items' => array( |
| 282 | 'type' => 'integer', |
| 283 | ), |
| 284 | 'default' => array(), |
| 285 | 'validate_callback' => 'rest_validate_request_arg', |
| 286 | 'sanitize_callback' => 'wp_parse_id_list', |
| 287 | ); |
| 288 | $params['variations'] = array( |
| 289 | 'description' => __( 'Limit result to items with specified variation ids.', 'woocommerce' ), |
| 290 | 'type' => 'array', |
| 291 | 'sanitize_callback' => 'wp_parse_id_list', |
| 292 | 'validate_callback' => 'rest_validate_request_arg', |
| 293 | 'items' => array( |
| 294 | 'type' => 'integer', |
| 295 | ), |
| 296 | ); |
| 297 | $params['extended_info'] = array( |
| 298 | 'description' => __( 'Add additional piece of info about each variation to the report.', 'woocommerce' ), |
| 299 | 'type' => 'boolean', |
| 300 | 'default' => false, |
| 301 | 'sanitize_callback' => 'wc_string_to_bool', |
| 302 | 'validate_callback' => 'rest_validate_request_arg', |
| 303 | ); |
| 304 | $params['attribute_is'] = array( |
| 305 | 'description' => __( 'Limit result set to variations that include the specified attributes.', 'woocommerce' ), |
| 306 | 'type' => 'array', |
| 307 | 'items' => array( |
| 308 | 'type' => 'array', |
| 309 | ), |
| 310 | 'default' => array(), |
| 311 | 'validate_callback' => 'rest_validate_request_arg', |
| 312 | ); |
| 313 | $params['attribute_is_not'] = array( |
| 314 | 'description' => __( 'Limit result set to variations that don\'t include the specified attributes.', 'woocommerce' ), |
| 315 | 'type' => 'array', |
| 316 | 'items' => array( |
| 317 | 'type' => 'array', |
| 318 | ), |
| 319 | 'default' => array(), |
| 320 | 'validate_callback' => 'rest_validate_request_arg', |
| 321 | ); |
| 322 | $params['category_includes'] = array( |
| 323 | 'description' => __( 'Limit result set to variations in the specified categories.', 'woocommerce' ), |
| 324 | 'type' => 'array', |
| 325 | 'sanitize_callback' => 'wp_parse_id_list', |
| 326 | 'validate_callback' => 'rest_validate_request_arg', |
| 327 | 'items' => array( |
| 328 | 'type' => 'integer', |
| 329 | ), |
| 330 | ); |
| 331 | $params['category_excludes'] = array( |
| 332 | 'description' => __( 'Limit result set to variations not in the specified categories.', 'woocommerce' ), |
| 333 | 'type' => 'array', |
| 334 | 'sanitize_callback' => 'wp_parse_id_list', |
| 335 | 'validate_callback' => 'rest_validate_request_arg', |
| 336 | 'items' => array( |
| 337 | 'type' => 'integer', |
| 338 | ), |
| 339 | ); |
| 340 | $params['products'] = array( |
| 341 | 'description' => __( 'Limit result to items with specified product ids.', 'woocommerce' ), |
| 342 | 'type' => 'array', |
| 343 | 'sanitize_callback' => 'wp_parse_id_list', |
| 344 | 'validate_callback' => 'rest_validate_request_arg', |
| 345 | 'items' => array( |
| 346 | 'type' => 'integer', |
| 347 | ), |
| 348 | ); |
| 349 | |
| 350 | return $params; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Get stock status column export value. |
| 355 | * |
| 356 | * @param array $status Stock status from report row. |
| 357 | * @return string |
| 358 | */ |
| 359 | protected function get_stock_status( $status ) { |
| 360 | $statuses = wc_get_product_stock_status_options(); |
| 361 | |
| 362 | return isset( $statuses[ $status ] ) ? $statuses[ $status ] : ''; |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Get the column names for export. |
| 367 | * |
| 368 | * @return array Key value pair of Column ID => Label. |
| 369 | */ |
| 370 | public function get_export_columns() { |
| 371 | $export_columns = array( |
| 372 | 'product_name' => __( 'Product / Variation title', 'woocommerce' ), |
| 373 | 'sku' => __( 'SKU', 'woocommerce' ), |
| 374 | 'items_sold' => __( 'Items sold', 'woocommerce' ), |
| 375 | 'net_revenue' => __( 'N. Revenue', 'woocommerce' ), |
| 376 | 'orders_count' => __( 'Orders', 'woocommerce' ), |
| 377 | ); |
| 378 | |
| 379 | if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) { |
| 380 | $export_columns['stock_status'] = __( 'Status', 'woocommerce' ); |
| 381 | $export_columns['stock'] = __( 'Stock', 'woocommerce' ); |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Filter to add or remove column names from the variations report for |
| 386 | * export. |
| 387 | * |
| 388 | * @since 10.7.0 |
| 389 | * @param array $export_columns Key value pair of column ID and label. |
| 390 | */ |
| 391 | return apply_filters( 'woocommerce_report_variations_export_columns', $export_columns ); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Get the column values for export. |
| 396 | * |
| 397 | * @param array $item Single report item/row. |
| 398 | * @return array Key value pair of Column ID => Row Value. |
| 399 | */ |
| 400 | public function prepare_item_for_export( $item ) { |
| 401 | $product_name = $item['extended_info']['name']; |
| 402 | /** |
| 403 | * Filter the separator used in the product variation title. |
| 404 | * |
| 405 | * @since 10.2.0 |
| 406 | * @param string $separator The separator. |
| 407 | * @param \WC_Product $product The product object. |
| 408 | * @return string The separator. |
| 409 | */ |
| 410 | $separator = apply_filters( 'woocommerce_product_variation_title_attributes_separator', ' - ', new \WC_Product() ); |
| 411 | if ( ! empty( $item['extended_info']['attributes'] ) && strpos( $product_name, $separator ) === false ) { |
| 412 | $attributes = array(); |
| 413 | foreach ( $item['extended_info']['attributes'] as $attribute ) { |
| 414 | if ( empty( $attribute['option'] ) ) { |
| 415 | // translators: %s: the attribute name. |
| 416 | $attributes[] = sprintf( __( 'Any %s', 'woocommerce' ), ucfirst( $attribute['name'] ) ); |
| 417 | } else { |
| 418 | $attributes[] = $attribute['option']; |
| 419 | } |
| 420 | } |
| 421 | $product_name .= $separator . implode( ', ', $attributes ); |
| 422 | } |
| 423 | |
| 424 | $export_item = array( |
| 425 | 'product_name' => $product_name, |
| 426 | 'sku' => $item['extended_info']['sku'], |
| 427 | 'items_sold' => $item['items_sold'], |
| 428 | 'net_revenue' => self::csv_number_format( $item['net_revenue'] ), |
| 429 | 'orders_count' => $item['orders_count'], |
| 430 | ); |
| 431 | |
| 432 | if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) { |
| 433 | $export_item['stock_status'] = $this->get_stock_status( $item['extended_info']['stock_status'] ); |
| 434 | $export_item['stock'] = $item['extended_info']['stock_quantity']; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Filter to prepare extra columns in the export item for the variations |
| 439 | * report. |
| 440 | * |
| 441 | * @since 10.7.0 |
| 442 | * @param array $export_item Key value pair of column ID and row value. |
| 443 | * @param array $item The original report item. |
| 444 | */ |
| 445 | return apply_filters( 'woocommerce_report_variations_prepare_export_item', $export_item, $item ); |
| 446 | } |
| 447 | } |
| 448 |