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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Admin / API / Reports / Orders / Stats / Segmenter.php
woocommerce / src / Admin / API / Reports / Orders / Stats Last commit date
Controller.php 1 year ago DataStore.php 1 month ago Query.php 1 year ago Segmenter.php 2 years ago
Segmenter.php
439 lines
1 <?php
2 /**
3 * Class for adding segmenting support without cluttering the data stores.
4 */
5
6 namespace Automattic\WooCommerce\Admin\API\Reports\Orders\Stats;
7
8 defined( 'ABSPATH' ) || exit;
9
10 use Automattic\WooCommerce\Admin\API\Reports\Segmenter as ReportsSegmenter;
11 use Automattic\WooCommerce\Admin\API\Reports\ParameterException;
12
13 /**
14 * Date & time interval and numeric range handling class for Reporting API.
15 */
16 class Segmenter extends ReportsSegmenter {
17
18 /**
19 * Returns column => query mapping to be used for product-related product-level segmenting query
20 * (e.g. products sold, revenue from product X when segmenting by category).
21 *
22 * @param string $products_table Name of SQL table containing the product-level segmenting info.
23 *
24 * @return array Column => SELECT query mapping.
25 */
26 protected function get_segment_selections_product_level( $products_table ) {
27 $columns_mapping = array(
28 'num_items_sold' => "SUM($products_table.product_qty) as num_items_sold",
29 'total_sales' => "SUM($products_table.product_gross_revenue) AS total_sales",
30 'coupons' => 'SUM( coupon_lookup_left_join.discount_amount ) AS coupons',
31 'coupons_count' => 'COUNT( DISTINCT( coupon_lookup_left_join.coupon_id ) ) AS coupons_count',
32 'refunds' => "SUM( CASE WHEN $products_table.product_gross_revenue < 0 THEN $products_table.product_gross_revenue ELSE 0 END ) AS refunds",
33 'taxes' => "SUM($products_table.tax_amount) AS taxes",
34 'shipping' => "SUM($products_table.shipping_amount) AS shipping",
35 'net_revenue' => "SUM($products_table.product_net_revenue) AS net_revenue",
36 );
37
38 return $columns_mapping;
39 }
40
41 /**
42 * Returns column => query mapping to be used for order-related product-level segmenting query
43 * (e.g. avg items per order when segmented by category).
44 *
45 * @param string $unique_orders_table Name of SQL table containing the order-level segmenting info.
46 *
47 * @return array Column => SELECT query mapping.
48 */
49 protected function get_segment_selections_order_level( $unique_orders_table ) {
50 $columns_mapping = array(
51 'orders_count' => "COUNT($unique_orders_table.order_id) AS orders_count",
52 'avg_items_per_order' => "AVG($unique_orders_table.num_items_sold) AS avg_items_per_order",
53 'avg_order_value' => "SUM($unique_orders_table.net_total) / COUNT($unique_orders_table.order_id) AS avg_order_value",
54 'total_customers' => "COUNT( DISTINCT( $unique_orders_table.customer_id ) ) AS total_customers",
55 );
56
57 return $columns_mapping;
58 }
59
60 /**
61 * Returns column => query mapping to be used for order-level segmenting query
62 * (e.g. avg items per order or Net sales when segmented by coupons).
63 *
64 * @param string $order_stats_table Name of SQL table containing the order-level info.
65 * @param array $overrides Array of overrides for default column calculations.
66 *
67 * @return array Column => SELECT query mapping.
68 */
69 protected function segment_selections_orders( $order_stats_table, $overrides = array() ) {
70 $columns_mapping = array(
71 'num_items_sold' => "SUM($order_stats_table.num_items_sold) as num_items_sold",
72 'total_sales' => "SUM($order_stats_table.total_sales) AS total_sales",
73 'coupons' => "SUM($order_stats_table.discount_amount) AS coupons",
74 'coupons_count' => 'COUNT( DISTINCT(coupon_lookup_left_join.coupon_id) ) AS coupons_count',
75 'refunds' => "SUM( CASE WHEN $order_stats_table.parent_id != 0 THEN $order_stats_table.total_sales END ) AS refunds",
76 'taxes' => "SUM($order_stats_table.tax_total) AS taxes",
77 'shipping' => "SUM($order_stats_table.shipping_total) AS shipping",
78 'net_revenue' => "SUM($order_stats_table.net_total) AS net_revenue",
79 'orders_count' => "COUNT($order_stats_table.order_id) AS orders_count",
80 'avg_items_per_order' => "AVG($order_stats_table.num_items_sold) AS avg_items_per_order",
81 'avg_order_value' => "SUM($order_stats_table.net_total) / COUNT($order_stats_table.order_id) AS avg_order_value",
82 'total_customers' => "COUNT( DISTINCT( $order_stats_table.customer_id ) ) AS total_customers",
83 );
84
85 if ( $overrides ) {
86 $columns_mapping = array_merge( $columns_mapping, $overrides );
87 }
88
89 return $columns_mapping;
90 }
91
92 /**
93 * Calculate segments for totals where the segmenting property is bound to product (e.g. category, product_id, variation_id).
94 *
95 * @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'.
96 * @param string $segmenting_from FROM part of segmenting SQL query.
97 * @param string $segmenting_where WHERE part of segmenting SQL query.
98 * @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
99 * @param string $segmenting_dimension_name Name of the segmenting dimension.
100 * @param string $table_name Name of SQL table which is the stats table for orders.
101 * @param array $totals_query Array of SQL clauses for totals query.
102 * @param string $unique_orders_table Name of temporary SQL table that holds unique orders.
103 *
104 * @return array
105 */
106 protected function get_product_related_totals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $totals_query, $unique_orders_table ) {
107 global $wpdb;
108
109 $product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup';
110
111 // Can't get all the numbers from one query, so split it into one query for product-level numbers and one for order-level numbers (which first need to have orders uniqued).
112 // Product-level numbers.
113 $segments_products = $wpdb->get_results(
114 "SELECT
115 $segmenting_groupby AS $segmenting_dimension_name
116 {$segmenting_selections['product_level']}
117 FROM
118 $table_name
119 $segmenting_from
120 {$totals_query['from_clause']}
121 WHERE
122 1=1
123 {$totals_query['where_time_clause']}
124 {$totals_query['where_clause']}
125 $segmenting_where
126 GROUP BY
127 $segmenting_groupby",
128 ARRAY_A
129 ); // phpcs:ignore cache ok, DB call ok, unprepared SQL ok.
130
131 // Order level numbers.
132 // As there can be 2 same product ids (or variation ids) per one order, the orders first need to be uniqued before calculating averages, customer counts, etc.
133 $segments_orders = $wpdb->get_results(
134 "SELECT
135 $unique_orders_table.$segmenting_dimension_name AS $segmenting_dimension_name
136 {$segmenting_selections['order_level']}
137 FROM
138 (
139 SELECT
140 $table_name.order_id,
141 $segmenting_groupby AS $segmenting_dimension_name,
142 MAX( num_items_sold ) AS num_items_sold,
143 MAX( net_total ) as net_total,
144 MAX( returning_customer ) AS returning_customer,
145 MAX( $table_name.customer_id ) as customer_id
146 FROM
147 $table_name
148 $segmenting_from
149 {$totals_query['from_clause']}
150 WHERE
151 1=1
152 {$totals_query['where_time_clause']}
153 {$totals_query['where_clause']}
154 $segmenting_where
155 GROUP BY
156 $product_segmenting_table.order_id, $segmenting_groupby
157 ) AS $unique_orders_table
158 GROUP BY
159 $unique_orders_table.$segmenting_dimension_name",
160 ARRAY_A
161 ); // phpcs:ignore cache ok, DB call ok, unprepared SQL ok.
162
163 $totals_segments = $this->merge_segment_totals_results( $segmenting_dimension_name, $segments_products, $segments_orders );
164 return $totals_segments;
165 }
166
167 /**
168 * Calculate segments for intervals where the segmenting property is bound to product (e.g. category, product_id, variation_id).
169 *
170 * @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'.
171 * @param string $segmenting_from FROM part of segmenting SQL query.
172 * @param string $segmenting_where WHERE part of segmenting SQL query.
173 * @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
174 * @param string $segmenting_dimension_name Name of the segmenting dimension.
175 * @param string $table_name Name of SQL table which is the stats table for orders.
176 * @param array $intervals_query Array of SQL clauses for intervals query.
177 * @param string $unique_orders_table Name of temporary SQL table that holds unique orders.
178 *
179 * @return array
180 */
181 protected function get_product_related_intervals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $intervals_query, $unique_orders_table ) {
182 global $wpdb;
183
184 $product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup';
185
186 // LIMIT offset, rowcount needs to be updated to LIMIT offset, rowcount * max number of segments.
187 $limit_parts = explode( ',', $intervals_query['limit'] );
188 $orig_rowcount = intval( $limit_parts[1] );
189 $segmenting_limit = $limit_parts[0] . ',' . $orig_rowcount * count( $this->get_all_segments() );
190
191 // Can't get all the numbers from one query, so split it into one query for product-level numbers and one for order-level numbers (which first need to have orders uniqued).
192 // Product-level numbers.
193 $segments_products = $wpdb->get_results(
194 "SELECT
195 {$intervals_query['select_clause']} AS time_interval,
196 $segmenting_groupby AS $segmenting_dimension_name
197 {$segmenting_selections['product_level']}
198 FROM
199 $table_name
200 $segmenting_from
201 {$intervals_query['from_clause']}
202 WHERE
203 1=1
204 {$intervals_query['where_time_clause']}
205 {$intervals_query['where_clause']}
206 $segmenting_where
207 GROUP BY
208 time_interval, $segmenting_groupby
209 $segmenting_limit",
210 ARRAY_A
211 ); // phpcs:ignore cache ok, DB call ok, unprepared SQL ok.
212
213 // Order level numbers.
214 // As there can be 2 same product ids (or variation ids) per one order, the orders first need to be uniqued before calculating averages, customer counts, etc.
215 $segments_orders = $wpdb->get_results(
216 "SELECT
217 $unique_orders_table.time_interval AS time_interval,
218 $unique_orders_table.$segmenting_dimension_name AS $segmenting_dimension_name
219 {$segmenting_selections['order_level']}
220 FROM
221 (
222 SELECT
223 MAX( $table_name.date_created ) AS datetime_anchor,
224 {$intervals_query['select_clause']} AS time_interval,
225 $table_name.order_id,
226 $segmenting_groupby AS $segmenting_dimension_name,
227 MAX( num_items_sold ) AS num_items_sold,
228 MAX( net_total ) as net_total,
229 MAX( returning_customer ) AS returning_customer,
230 MAX( $table_name.customer_id ) as customer_id
231 FROM
232 $table_name
233 $segmenting_from
234 {$intervals_query['from_clause']}
235 WHERE
236 1=1
237 {$intervals_query['where_time_clause']}
238 {$intervals_query['where_clause']}
239 $segmenting_where
240 GROUP BY
241 time_interval, $product_segmenting_table.order_id, $segmenting_groupby
242 ) AS $unique_orders_table
243 GROUP BY
244 time_interval, $unique_orders_table.$segmenting_dimension_name
245 $segmenting_limit",
246 ARRAY_A
247 ); // phpcs:ignore cache ok, DB call ok, unprepared SQL ok.
248
249 $intervals_segments = $this->merge_segment_intervals_results( $segmenting_dimension_name, $segments_products, $segments_orders );
250 return $intervals_segments;
251 }
252
253 /**
254 * Calculate segments for totals query where the segmenting property is bound to order (e.g. coupon or customer type).
255 *
256 * @param string $segmenting_select SELECT part of segmenting SQL query.
257 * @param string $segmenting_from FROM part of segmenting SQL query.
258 * @param string $segmenting_where WHERE part of segmenting SQL query.
259 * @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
260 * @param string $table_name Name of SQL table which is the stats table for orders.
261 * @param array $totals_query Array of SQL clauses for intervals query.
262 *
263 * @return array
264 */
265 protected function get_order_related_totals_segments( $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $totals_query ) {
266 global $wpdb;
267
268 $totals_segments = $wpdb->get_results(
269 "SELECT
270 $segmenting_groupby
271 $segmenting_select
272 FROM
273 $table_name
274 $segmenting_from
275 {$totals_query['from_clause']}
276 WHERE
277 1=1
278 {$totals_query['where_time_clause']}
279 {$totals_query['where_clause']}
280 $segmenting_where
281 GROUP BY
282 $segmenting_groupby",
283 ARRAY_A
284 ); // phpcs:ignore cache ok, DB call ok, unprepared SQL ok.
285
286 // Reformat result.
287 $totals_segments = $this->reformat_totals_segments( $totals_segments, $segmenting_groupby );
288 return $totals_segments;
289 }
290
291 /**
292 * Calculate segments for intervals query where the segmenting property is bound to order (e.g. coupon or customer type).
293 *
294 * @param string $segmenting_select SELECT part of segmenting SQL query.
295 * @param string $segmenting_from FROM part of segmenting SQL query.
296 * @param string $segmenting_where WHERE part of segmenting SQL query.
297 * @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
298 * @param string $table_name Name of SQL table which is the stats table for orders.
299 * @param array $intervals_query Array of SQL clauses for intervals query.
300 *
301 * @return array
302 */
303 protected function get_order_related_intervals_segments( $segmenting_select, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $intervals_query ) {
304 global $wpdb;
305 $segmenting_limit = '';
306 $limit_parts = explode( ',', $intervals_query['limit'] );
307 if ( 2 === count( $limit_parts ) ) {
308 $orig_rowcount = intval( $limit_parts[1] );
309 $segmenting_limit = $limit_parts[0] . ',' . $orig_rowcount * count( $this->get_all_segments() );
310 }
311
312 $intervals_segments = $wpdb->get_results(
313 "SELECT
314 MAX($table_name.date_created) AS datetime_anchor,
315 {$intervals_query['select_clause']} AS time_interval,
316 $segmenting_groupby
317 $segmenting_select
318 FROM
319 $table_name
320 $segmenting_from
321 {$intervals_query['from_clause']}
322 WHERE
323 1=1
324 {$intervals_query['where_time_clause']}
325 {$intervals_query['where_clause']}
326 $segmenting_where
327 GROUP BY
328 time_interval, $segmenting_groupby
329 $segmenting_limit",
330 ARRAY_A
331 ); // phpcs:ignore cache ok, DB call ok, unprepared SQL ok.
332
333 // Reformat result.
334 $intervals_segments = $this->reformat_intervals_segments( $intervals_segments, $segmenting_groupby );
335 return $intervals_segments;
336 }
337
338 /**
339 * Return array of segments formatted for REST response.
340 *
341 * @param string $type Type of segments to return--'totals' or 'intervals'.
342 * @param array $query_params SQL query parameter array.
343 * @param string $table_name Name of main SQL table for the data store (used as basis for JOINS).
344 *
345 * @return array
346 * @throws \Automattic\WooCommerce\Admin\API\Reports\ParameterException In case of segmenting by variations, when no parent product is specified.
347 */
348 protected function get_segments( $type, $query_params, $table_name ) {
349 global $wpdb;
350 if ( ! isset( $this->query_args['segmentby'] ) || '' === $this->query_args['segmentby'] ) {
351 return array();
352 }
353
354 $product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup';
355 $unique_orders_table = 'uniq_orders';
356 $segmenting_from = "LEFT JOIN {$wpdb->prefix}wc_order_coupon_lookup AS coupon_lookup_left_join ON ($table_name.order_id = coupon_lookup_left_join.order_id) ";
357 $segmenting_where = '';
358
359 // Product, variation, and category are bound to product, so here product segmenting table is required,
360 // while coupon and customer are bound to order, so we don't need the extra JOIN for those.
361 // This also means that segment selections need to be calculated differently.
362 if ( 'product' === $this->query_args['segmentby'] ) {
363 // @todo How to handle shipping taxes when grouped by product?
364 $product_level_columns = $this->get_segment_selections_product_level( $product_segmenting_table );
365 $order_level_columns = $this->get_segment_selections_order_level( $unique_orders_table );
366 $segmenting_selections = array(
367 'product_level' => $this->prepare_selections( $product_level_columns ),
368 'order_level' => $this->prepare_selections( $order_level_columns ),
369 );
370 $this->report_columns = array_merge( $product_level_columns, $order_level_columns );
371 $segmenting_from .= "INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id)";
372 $segmenting_groupby = $product_segmenting_table . '.product_id';
373 $segmenting_dimension_name = 'product_id';
374
375 $segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
376 } elseif ( 'variation' === $this->query_args['segmentby'] ) {
377 if ( ! isset( $this->query_args['product_includes'] ) || ! is_array( $this->query_args['product_includes'] ) || count( $this->query_args['product_includes'] ) !== 1 ) {
378 throw new ParameterException( 'wc_admin_reports_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'woocommerce' ) );
379 }
380
381 $product_level_columns = $this->get_segment_selections_product_level( $product_segmenting_table );
382 $order_level_columns = $this->get_segment_selections_order_level( $unique_orders_table );
383 $segmenting_selections = array(
384 'product_level' => $this->prepare_selections( $product_level_columns ),
385 'order_level' => $this->prepare_selections( $order_level_columns ),
386 );
387 $this->report_columns = array_merge( $product_level_columns, $order_level_columns );
388 $segmenting_from .= "INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id)";
389 $segmenting_where = "AND $product_segmenting_table.product_id = {$this->query_args['product_includes'][0]}";
390 $segmenting_groupby = $product_segmenting_table . '.variation_id';
391 $segmenting_dimension_name = 'variation_id';
392
393 $segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
394 } elseif ( 'category' === $this->query_args['segmentby'] ) {
395 $product_level_columns = $this->get_segment_selections_product_level( $product_segmenting_table );
396 $order_level_columns = $this->get_segment_selections_order_level( $unique_orders_table );
397 $segmenting_selections = array(
398 'product_level' => $this->prepare_selections( $product_level_columns ),
399 'order_level' => $this->prepare_selections( $order_level_columns ),
400 );
401 $this->report_columns = array_merge( $product_level_columns, $order_level_columns );
402 $segmenting_from .= "
403 INNER JOIN $product_segmenting_table ON ($table_name.order_id = $product_segmenting_table.order_id)
404 LEFT JOIN {$wpdb->term_relationships} ON {$product_segmenting_table}.product_id = {$wpdb->term_relationships}.object_id
405 JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
406 LEFT JOIN {$wpdb->wc_category_lookup} ON {$wpdb->term_taxonomy}.term_id = {$wpdb->wc_category_lookup}.category_id
407 ";
408 $segmenting_where = " AND {$wpdb->wc_category_lookup}.category_tree_id IS NOT NULL";
409 $segmenting_groupby = "{$wpdb->wc_category_lookup}.category_tree_id";
410 $segmenting_dimension_name = 'category_id';
411
412 $segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table );
413 } elseif ( 'coupon' === $this->query_args['segmentby'] ) {
414 // As there can be 2 or more coupons applied per one order, coupon amount needs to be split.
415 $coupon_override = array(
416 'coupons' => 'SUM(coupon_lookup.discount_amount) AS coupons',
417 );
418 $coupon_level_columns = $this->segment_selections_orders( $table_name, $coupon_override );
419 $segmenting_selections = $this->prepare_selections( $coupon_level_columns );
420 $this->report_columns = $coupon_level_columns;
421 $segmenting_from .= "
422 INNER JOIN {$wpdb->prefix}wc_order_coupon_lookup AS coupon_lookup ON ($table_name.order_id = coupon_lookup.order_id)
423 ";
424 $segmenting_groupby = 'coupon_lookup.coupon_id';
425
426 $segments = $this->get_order_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params );
427 } elseif ( 'customer_type' === $this->query_args['segmentby'] ) {
428 $customer_level_columns = $this->segment_selections_orders( $table_name );
429 $segmenting_selections = $this->prepare_selections( $customer_level_columns );
430 $this->report_columns = $customer_level_columns;
431 $segmenting_groupby = "$table_name.returning_customer";
432
433 $segments = $this->get_order_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $table_name, $query_params );
434 }
435
436 return $segments;
437 }
438 }
439