WC_REST_Report_Top_Sellers_V1_Controller

REST API Report Top Sellers controller class.

Defined (1)

The class is defined in the following location(s).

/includes/api/v1/class-wc-rest-report-top-sellers-controller.php  
  1. class WC_REST_Report_Top_Sellers_V1_Controller extends WC_REST_Report_Sales_V1_Controller { 
  2.  
  3. /** 
  4. * Endpoint namespace. 
  5. * @var string 
  6. */ 
  7. protected $namespace = 'wc/v1'; 
  8.  
  9. /** 
  10. * Route base. 
  11. * @var string 
  12. */ 
  13. protected $rest_base = 'reports/top_sellers'; 
  14.  
  15. /** 
  16. * Get sales reports. 
  17. * @param WP_REST_Request $request 
  18. * @return array|WP_Error 
  19. */ 
  20. public function get_items( $request ) { 
  21. // Set date filtering. 
  22. $filter = array( 
  23. 'period' => $request['period'],  
  24. 'date_min' => $request['date_min'],  
  25. 'date_max' => $request['date_max'],  
  26. ); 
  27. $this->setup_report( $filter ); 
  28.  
  29. $report_data = $this->report->get_order_report_data( array( 
  30. 'data' => array( 
  31. '_product_id' => array( 
  32. 'type' => 'order_item_meta',  
  33. 'order_item_type' => 'line_item',  
  34. 'function' => '',  
  35. 'name' => 'product_id',  
  36. ),  
  37. '_qty' => array( 
  38. 'type' => 'order_item_meta',  
  39. 'order_item_type' => 'line_item',  
  40. 'function' => 'SUM',  
  41. 'name' => 'order_item_qty',  
  42. ),  
  43. ),  
  44. 'order_by' => 'order_item_qty DESC',  
  45. 'group_by' => 'product_id',  
  46. 'limit' => isset( $filter['limit'] ) ? absint( $filter['limit'] ) : 12,  
  47. 'query_type' => 'get_results',  
  48. 'filter_range' => true,  
  49. ) ); 
  50.  
  51. $top_sellers = array(); 
  52.  
  53. foreach ( $report_data as $item ) { 
  54. $product = wc_get_product( $item->product_id ); 
  55.  
  56. if ( $product ) { 
  57. $top_sellers[] = array( 
  58. 'name' => $product->get_name(),  
  59. 'product_id' => (int) $item->product_id,  
  60. 'quantity' => wc_stock_amount( $item->order_item_qty ),  
  61. ); 
  62.  
  63. $data = array(); 
  64. foreach ( $top_sellers as $top_seller ) { 
  65. $item = $this->prepare_item_for_response( (object) $top_seller, $request ); 
  66. $data[] = $this->prepare_response_for_collection( $item ); 
  67.  
  68. return rest_ensure_response( $data ); 
  69.  
  70. /** 
  71. * Prepare a report sales object for serialization. 
  72. * @param stdClass $top_seller 
  73. * @param WP_REST_Request $request Request object. 
  74. * @return WP_REST_Response $response Response data. 
  75. */ 
  76. public function prepare_item_for_response( $top_seller, $request ) { 
  77. $data = array( 
  78. 'name' => $top_seller->name,  
  79. 'product_id' => $top_seller->product_id,  
  80. 'quantity' => $top_seller->quantity,  
  81. ); 
  82.  
  83. $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 
  84. $data = $this->add_additional_fields_to_object( $data, $request ); 
  85. $data = $this->filter_response_by_context( $data, $context ); 
  86.  
  87. // Wrap the data in a response object. 
  88. $response = rest_ensure_response( $data ); 
  89. $response->add_links( array( 
  90. 'about' => array( 
  91. 'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ),  
  92. ),  
  93. 'product' => array( 
  94. 'href' => rest_url( sprintf( '/%s/products/%s', $this->namespace, $top_seller->product_id ) ),  
  95. ),  
  96. ) ); 
  97.  
  98. /** 
  99. * Filter a report top sellers returned from the API. 
  100. * Allows modification of the report top sellers data right before it is returned. 
  101. * @param WP_REST_Response $response The response object. 
  102. * @param stdClass $top_seller The original report object. 
  103. * @param WP_REST_Request $request Request used to generate the response. 
  104. */ 
  105. return apply_filters( 'woocommerce_rest_prepare_report_top_sellers', $response, $top_seller, $request ); 
  106.  
  107. /** 
  108. * Get the Report's schema, conforming to JSON Schema. 
  109. * @return array 
  110. */ 
  111. public function get_item_schema() { 
  112. $schema = array( 
  113. '$schema' => 'http://json-schema.org/draft-04/schema#',  
  114. 'title' => 'top_sellers_report',  
  115. 'type' => 'object',  
  116. 'properties' => array( 
  117. 'name' => array( 
  118. 'description' => __( 'Product name.', 'woocommerce' ),  
  119. 'type' => 'string',  
  120. 'context' => array( 'view' ),  
  121. 'readonly' => true,  
  122. ),  
  123. 'product_id' => array( 
  124. 'description' => __( 'Product ID.', 'woocommerce' ),  
  125. 'type' => 'integer',  
  126. 'context' => array( 'view' ),  
  127. 'readonly' => true,  
  128. ),  
  129. 'quantity' => array( 
  130. 'description' => __( 'Total number of purchases.', 'woocommerce' ),  
  131. 'type' => 'integer',  
  132. 'context' => array( 'view' ),  
  133. 'readonly' => true,  
  134. ),  
  135. ),  
  136. ); 
  137.  
  138. return $this->add_additional_fields_schema( $schema );