class-wc-rest-report-top-sellers-controller.php
4.59 KB
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
<?php
/**
* REST API Reports controller
*
* Handles requests to the reports/top_sellers endpoint.
*
* @author WooThemes
* @category API
* @package WooCommerce/API
* @since 2.6.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* REST API Report Top Sellers controller class.
*
* @package WooCommerce/API
* @extends WC_REST_Report_Sales_Controller
*/
class WC_REST_Report_Top_Sellers_Controller extends WC_REST_Report_Sales_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc/v1';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'reports/top_sellers';
/**
* Get sales reports.
*
* @param WP_REST_Request $request
* @return array|WP_Error
*/
public function get_items( $request ) {
// Set date filtering.
$filter = array(
'period' => $request['period'],
'date_min' => $request['date_min'],
'date_max' => $request['date_max'],
);
$this->setup_report( $filter );
$report_data = $this->report->get_order_report_data( array(
'data' => array(
'_product_id' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => '',
'name' => 'product_id',
),
'_qty' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => 'SUM',
'name' => 'order_item_qty',
)
),
'order_by' => 'order_item_qty DESC',
'group_by' => 'product_id',
'limit' => isset( $filter['limit'] ) ? absint( $filter['limit'] ) : 12,
'query_type' => 'get_results',
'filter_range' => true,
) );
$top_sellers = array();
foreach ( $report_data as $item ) {
$product = wc_get_product( $item->product_id );
if ( $product ) {
$top_sellers[] = array(
'name' => $product->get_title(),
'product_id' => (int) $item->product_id,
'quantity' => wc_stock_amount( $item->order_item_qty ),
);
}
}
$data = array();
foreach ( $top_sellers as $top_seller ) {
$item = $this->prepare_item_for_response( (object) $top_seller, $request );
$data[] = $this->prepare_response_for_collection( $item );
}
return rest_ensure_response( $data );
}
/**
* Prepare a report sales object for serialization.
*
* @param stdClass $top_seller
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response $response Response data.
*/
public function prepare_item_for_response( $top_seller, $request ) {
$data = array(
'name' => $top_seller->name,
'product_id' => $top_seller->product_id,
'quantity' => $top_seller->quantity,
);
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
$response->add_links( array(
'about' => array(
'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ),
),
'product' => array(
'href' => rest_url( sprintf( '/%s/products/%s', $this->namespace, $top_seller->product_id ) ),
),
) );
/**
* Filter a report top sellers returned from the API.
*
* Allows modification of the report top sellers data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param stdClass $top_seller The original report object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_report_top_sellers', $response, $top_seller, $request );
}
/**
* 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' => 'top_sellers_report',
'type' => 'object',
'properties' => array(
'name' => array(
'description' => __( 'Product name.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view' ),
'readonly' => true,
),
'product_id' => array(
'description' => __( 'Product ID.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view' ),
'readonly' => true,
),
'quantity' => array(
'description' => __( 'Total number of purchases.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view' ),
'readonly' => true,
),
),
);
return $this->add_additional_fields_schema( $schema );
}
}