PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.4.14
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.4.14
1.4.14 1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 1.3.47 1.3.46 1.3.45 All 177 releases
disco / app / Analytics / Queries / SummaryQuery.php

SummaryQuery.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.4.14, at app/Analytics/Queries/SummaryQuery.php

141 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * SummaryQuery — queries for the /analytics/summary endpoint.
5 *
6 * @package Disco
7 * @subpackage Disco\App\Analytics\Queries
8 * @since 1.3.23
9 */
10
11 namespace Disco\App\Analytics\Queries;
12
13 /**
14 * All queries for the summary endpoint, scoped to completed orders only.
15 *
16 * Every method adds `o.status = 'wc-completed'` so raw totals are not
17 * inflated by pending/refunded/failed orders.
18 */
19 class SummaryQuery extends BaseQuery {
20
21 /**
22 * WooCommerce status value for completed orders.
23 */
24 private const STATUS_COMPLETED = 'wc-completed';
25
26 /**
27 * Returns the number of campaigns whose status is currently '1' (active).
28 *
29 * Not date-filtered — reflects live DB state. Counts directly in SQL
30 * instead of loading every campaign row into PHP; the intent IS NOT NULL
31 * guard mirrors Campaign::get_campaigns(), which skips rows without intent.
32 */
33 public function get_active_campaign_count(): int {
34 $tables = $this->get_tables();
35
36 return $this->run_count(
37 "SELECT COUNT(*) FROM {$tables['campaigns']} WHERE status = %s AND intent IS NOT NULL",
38 array( '1' )
39 );
40 }
41
42 /**
43 * Returns total_orders and net_sales for ALL completed shop orders in a period.
44 *
45 * Does NOT filter by disco_campaign — site-wide WooCommerce totals.
46 *
47 * @param array $period { from: string Y-m-d, to: string Y-m-d }.
48 * @return array { total_orders: int, net_sales: float }
49 */
50 public function get_all_order_metrics( array $period ): array {
51 global $wpdb;
52
53 $from_datetime = $period['from'] . ' 00:00:00';
54 $to_datetime = $period['to'] . ' 23:59:59';
55
56 $sql = "SELECT
57 COUNT(*) AS total_orders,
58 COALESCE(SUM(order_stats.net_total), 0) AS net_sales
59 FROM {$wpdb->prefix}wc_order_stats order_stats
60 WHERE order_stats.status = %s
61 AND order_stats.parent_id = 0
62 AND order_stats.date_created BETWEEN %s AND %s";
63
64 $row = $this->run_row( $sql, array( self::STATUS_COMPLETED, $from_datetime, $to_datetime ) );
65
66 return array(
67 'total_orders' => (int) ( $row->total_orders ?? 0 ),
68 'net_sales' => round( (float) ( $row->net_sales ?? 0 ), 2 ),
69 );
70 }
71
72 /**
73 * Returns disco_orders count and discount_sales for completed disco orders in a period.
74 *
75 * Scoped to wc-completed orders only.
76 *
77 * @param array $period { from: string Y-m-d, to: string Y-m-d }.
78 * @return array { orders_count: int, revenue: float }
79 */
80 public function get_disco_order_metrics( array $period ): array {
81 global $wpdb;
82
83 $tables = $this->get_tables();
84 $clauses = $this->get_order_clauses( $tables );
85 $order_id_column = $tables['order_id_col'];
86 $from_datetime = $period['from'] . ' 00:00:00';
87 $to_datetime = $period['to'] . ' 23:59:59';
88
89 $dedup = $this->get_campaign_dedup_condition( $tables );
90
91 $sql = "SELECT
92 COUNT(DISTINCT order_meta.{$order_id_column}) AS orders_count,
93 SUM({$clauses['total_expr']} - COALESCE(order_stats.shipping_total, 0)) AS revenue
94 FROM {$tables['order_meta']} order_meta
95 JOIN {$tables['orders']} o ON o.ID = order_meta.{$order_id_column} AND {$clauses['status_where']}
96 LEFT JOIN {$wpdb->prefix}wc_order_stats order_stats ON order_stats.order_id = order_meta.{$order_id_column}
97 {$clauses['total_join']}
98 WHERE order_meta.meta_key = %s
99 AND o.status = %s
100 AND {$clauses['date_col']} BETWEEN %s AND %s
101 AND {$dedup}";
102
103 $row = $this->run_row( $sql, array( 'disco_campaign', self::STATUS_COMPLETED, $from_datetime, $to_datetime ) );
104
105 return array(
106 'orders_count' => (int) ( $row->orders_count ?? 0 ),
107 'revenue' => round( (float) ( $row->revenue ?? 0 ), 2 ),
108 );
109 }
110
111 /**
112 * Returns the distinct customer count from completed disco orders in a period.
113 *
114 * Counts by billing_email so guest checkouts are included.
115 * Scoped to wc-completed orders only.
116 *
117 * @param array $period { from: string Y-m-d, to: string Y-m-d }.
118 */
119 public function get_disco_customer_count( array $period ): int {
120 $tables = $this->get_tables();
121 $clauses = $this->get_order_clauses( $tables );
122 $order_id_column = $tables['order_id_col'];
123 $from_datetime = $period['from'] . ' 00:00:00';
124 $to_datetime = $period['to'] . ' 23:59:59';
125
126 $dedup = $this->get_campaign_dedup_condition( $tables );
127
128 $sql = "SELECT COUNT(DISTINCT o.billing_email)
129 FROM {$tables['order_meta']} order_meta
130 JOIN {$tables['orders']} o ON o.ID = order_meta.{$order_id_column} AND {$clauses['status_where']}
131 WHERE order_meta.meta_key = %s
132 AND o.status = %s
133 AND {$clauses['date_col']} BETWEEN %s AND %s
134 AND o.billing_email != ''
135 AND {$dedup}";
136
137 return $this->run_count( $sql, array( 'disco_campaign', self::STATUS_COMPLETED, $from_datetime, $to_datetime ) );
138 }
139
140 }
141