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.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 / Variations / Stats / Segmenter.php
woocommerce / src / Admin / API / Reports / Variations / Stats Last commit date
Controller.php 1 year ago DataStore.php 4 weeks ago Query.php 1 year ago Segmenter.php 2 years ago
Segmenter.php
181 lines
1 <?php
2 /**
3 * Class for adding segmenting support without cluttering the data stores.
4 */
5
6 namespace Automattic\WooCommerce\Admin\API\Reports\Variations\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 'items_sold' => "SUM($products_table.product_qty) as items_sold",
29 'net_revenue' => "SUM($products_table.product_net_revenue ) AS net_revenue",
30 'orders_count' => "COUNT( DISTINCT $products_table.order_id ) AS orders_count",
31 'variations_count' => "COUNT( DISTINCT $products_table.variation_id ) AS variations_count",
32 );
33
34 return $columns_mapping;
35 }
36
37 /**
38 * Calculate segments for totals where the segmenting property is bound to product (e.g. category, product_id, variation_id).
39 *
40 * @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'.
41 * @param string $segmenting_from FROM part of segmenting SQL query.
42 * @param string $segmenting_where WHERE part of segmenting SQL query.
43 * @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
44 * @param string $segmenting_dimension_name Name of the segmenting dimension.
45 * @param string $table_name Name of SQL table which is the stats table for orders.
46 * @param array $totals_query Array of SQL clauses for totals query.
47 * @param string $unique_orders_table Name of temporary SQL table that holds unique orders.
48 *
49 * @return array
50 */
51 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 ) {
52 global $wpdb;
53
54 $product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup';
55
56 // 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).
57 // Product-level numbers.
58 /* phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared */
59 $segments_products = $wpdb->get_results(
60 "SELECT
61 $segmenting_groupby AS $segmenting_dimension_name
62 {$segmenting_selections['product_level']}
63 FROM
64 $table_name
65 $segmenting_from
66 {$totals_query['from_clause']}
67 WHERE
68 1=1
69 {$totals_query['where_time_clause']}
70 {$totals_query['where_clause']}
71 $segmenting_where
72 GROUP BY
73 $segmenting_groupby",
74 ARRAY_A
75 );
76 /* phpcs:enable */
77
78 $totals_segments = $this->merge_segment_totals_results( $segmenting_dimension_name, $segments_products, array() );
79 return $totals_segments;
80 }
81
82 /**
83 * Calculate segments for intervals where the segmenting property is bound to product (e.g. category, product_id, variation_id).
84 *
85 * @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'.
86 * @param string $segmenting_from FROM part of segmenting SQL query.
87 * @param string $segmenting_where WHERE part of segmenting SQL query.
88 * @param string $segmenting_groupby GROUP BY part of segmenting SQL query.
89 * @param string $segmenting_dimension_name Name of the segmenting dimension.
90 * @param string $table_name Name of SQL table which is the stats table for orders.
91 * @param array $intervals_query Array of SQL clauses for intervals query.
92 * @param string $unique_orders_table Name of temporary SQL table that holds unique orders.
93 *
94 * @return array
95 */
96 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 ) {
97 global $wpdb;
98
99 $product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup';
100
101 // LIMIT offset, rowcount needs to be updated to a multiple of the number of segments.
102 preg_match( '/LIMIT (\d+)\s?,\s?(\d+)/', $intervals_query['limit'], $limit_parts );
103 $segment_count = count( $this->get_all_segments() );
104 $orig_offset = intval( $limit_parts[1] );
105 $orig_rowcount = intval( $limit_parts[2] );
106 $segmenting_limit = $wpdb->prepare( 'LIMIT %d, %d', $orig_offset * $segment_count, $orig_rowcount * $segment_count );
107
108 // 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).
109 // Product-level numbers.
110 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
111 $segments_products = $wpdb->get_results(
112 "SELECT
113 {$intervals_query['select_clause']} AS time_interval,
114 $segmenting_groupby AS $segmenting_dimension_name
115 {$segmenting_selections['product_level']}
116 FROM
117 $table_name
118 $segmenting_from
119 {$intervals_query['from_clause']}
120 WHERE
121 1=1
122 {$intervals_query['where_time_clause']}
123 {$intervals_query['where_clause']}
124 $segmenting_where
125 GROUP BY
126 time_interval, $segmenting_groupby
127 $segmenting_limit",
128 // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
129 ARRAY_A
130 );
131
132 $intervals_segments = $this->merge_segment_intervals_results( $segmenting_dimension_name, $segments_products, array() );
133 return $intervals_segments;
134 }
135
136 /**
137 * Return array of segments formatted for REST response.
138 *
139 * @param string $type Type of segments to return--'totals' or 'intervals'.
140 * @param array $query_params SQL query parameter array.
141 * @param string $table_name Name of main SQL table for the data store (used as basis for JOINS).
142 *
143 * @return array|null
144 * @throws \Automattic\WooCommerce\Admin\API\Reports\ParameterException In case of segmenting by variations, when no parent product is specified.
145 */
146 protected function get_segments( $type, $query_params, $table_name ) {
147 global $wpdb;
148 if ( ! isset( $this->query_args['segmentby'] ) || '' === $this->query_args['segmentby'] ) {
149 return array();
150 }
151 $segments = null;
152 $product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup';
153 $unique_orders_table = 'uniq_orders';
154 $segmenting_where = '';
155
156 // Product, variation, and category are bound to product, so here product segmenting table is required,
157 // while coupon and customer are bound to order, so we don't need the extra JOIN for those.
158 // This also means that segment selections need to be calculated differently.
159 if ( 'variation' === $this->query_args['segmentby'] ) {
160 $product_level_columns = $this->get_segment_selections_product_level( $product_segmenting_table );
161 $segmenting_selections = array(
162 'product_level' => $this->prepare_selections( $product_level_columns ),
163 );
164 $this->report_columns = $product_level_columns;
165 $segmenting_from = '';
166 $segmenting_groupby = $product_segmenting_table . '.variation_id';
167 $segmenting_dimension_name = 'variation_id';
168
169 // Restrict our search space for variation comparisons.
170 if ( isset( $this->query_args['variation_includes'] ) ) {
171 $variation_ids = implode( ',', $this->get_all_segments() );
172 $segmenting_where = " AND $product_segmenting_table.variation_id IN ( $variation_ids )";
173 }
174
175 $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 );
176 }
177
178 return $segments;
179 }
180 }
181