class-wc-rest-report-top-sellers-controller.php 4.6 KB

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