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 |