|
|
@@ -470,6 +470,136 @@ class StudiouWC_DB_Manager {
|
|
|
";
|
|
|
}
|
|
|
|
|
|
+ public function get_order_customers_list($date_from, $date_to, $statuses, $per_page, $paged, $orderby, $order) {
|
|
|
+ global $wpdb;
|
|
|
+
|
|
|
+ if (empty($statuses)) {
|
|
|
+ return array();
|
|
|
+ }
|
|
|
+
|
|
|
+ $statuses_placeholder = implode(',', array_fill(0, count($statuses), '%s'));
|
|
|
+ $offset = ($paged - 1) * $per_page;
|
|
|
+
|
|
|
+ if ($this->is_hpos_enabled()) {
|
|
|
+ $sql = $this->get_hpos_customers_query($statuses_placeholder);
|
|
|
+ } else {
|
|
|
+ $sql = $this->get_legacy_customers_query($statuses_placeholder);
|
|
|
+ }
|
|
|
+
|
|
|
+ $sql .= $this->get_customers_order_clause($orderby, $order);
|
|
|
+ $sql .= " LIMIT %d OFFSET %d";
|
|
|
+
|
|
|
+ $query_params = array_merge(
|
|
|
+ array($date_from, $date_to),
|
|
|
+ $statuses,
|
|
|
+ array($per_page, $offset)
|
|
|
+ );
|
|
|
+
|
|
|
+ return $wpdb->get_results($wpdb->prepare($sql, $query_params), ARRAY_A);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function get_order_customers_total_items($date_from, $date_to, $statuses) {
|
|
|
+ global $wpdb;
|
|
|
+
|
|
|
+ if (empty($statuses)) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ $statuses_placeholder = implode(',', array_fill(0, count($statuses), '%s'));
|
|
|
+
|
|
|
+ if ($this->is_hpos_enabled()) {
|
|
|
+ $sql = "
|
|
|
+ SELECT COUNT(DISTINCT addr.email)
|
|
|
+ FROM {$wpdb->prefix}wc_orders o
|
|
|
+ INNER JOIN {$wpdb->prefix}wc_order_addresses addr ON o.id = addr.order_id AND addr.address_type = 'billing'
|
|
|
+ WHERE o.date_created_gmt >= %s
|
|
|
+ AND o.date_created_gmt <= %s
|
|
|
+ AND o.status IN ($statuses_placeholder)
|
|
|
+ AND addr.email != ''
|
|
|
+ ";
|
|
|
+ } else {
|
|
|
+ $sql = "
|
|
|
+ SELECT COUNT(DISTINCT pm_email.meta_value)
|
|
|
+ FROM {$wpdb->posts} p
|
|
|
+ INNER JOIN {$wpdb->postmeta} pm_email ON p.ID = pm_email.post_id AND pm_email.meta_key = '_billing_email'
|
|
|
+ WHERE p.post_date >= %s
|
|
|
+ AND p.post_date <= %s
|
|
|
+ AND p.post_status IN ($statuses_placeholder)
|
|
|
+ AND p.post_type = 'shop_order'
|
|
|
+ AND pm_email.meta_value != ''
|
|
|
+ ";
|
|
|
+ }
|
|
|
+
|
|
|
+ $query_params = array_merge(
|
|
|
+ array($date_from, $date_to),
|
|
|
+ $statuses
|
|
|
+ );
|
|
|
+
|
|
|
+ return $wpdb->get_var($wpdb->prepare($sql, $query_params));
|
|
|
+ }
|
|
|
+
|
|
|
+ private function get_hpos_customers_query($statuses_placeholder) {
|
|
|
+ global $wpdb;
|
|
|
+
|
|
|
+ return "
|
|
|
+ SELECT
|
|
|
+ MAX(CONCAT(addr.first_name, ' ', addr.last_name)) as customer_name,
|
|
|
+ addr.email as customer_email,
|
|
|
+ MAX(addr.phone) as customer_phone,
|
|
|
+ COUNT(DISTINCT o.id) as orders_count,
|
|
|
+ SUM(o.total_amount) as total_price
|
|
|
+ FROM {$wpdb->prefix}wc_orders o
|
|
|
+ INNER JOIN {$wpdb->prefix}wc_order_addresses addr ON o.id = addr.order_id AND addr.address_type = 'billing'
|
|
|
+ WHERE o.date_created_gmt >= %s
|
|
|
+ AND o.date_created_gmt <= %s
|
|
|
+ AND o.status IN ($statuses_placeholder)
|
|
|
+ AND addr.email != ''
|
|
|
+ GROUP BY addr.email
|
|
|
+ ";
|
|
|
+ }
|
|
|
+
|
|
|
+ private function get_legacy_customers_query($statuses_placeholder) {
|
|
|
+ global $wpdb;
|
|
|
+
|
|
|
+ return "
|
|
|
+ SELECT
|
|
|
+ MAX(CONCAT(pm_fn.meta_value, ' ', pm_ln.meta_value)) as customer_name,
|
|
|
+ pm_email.meta_value as customer_email,
|
|
|
+ MAX(pm_phone.meta_value) as customer_phone,
|
|
|
+ COUNT(DISTINCT p.ID) as orders_count,
|
|
|
+ SUM(CAST(pm_total.meta_value AS DECIMAL(10,2))) as total_price
|
|
|
+ FROM {$wpdb->posts} p
|
|
|
+ INNER JOIN {$wpdb->postmeta} pm_email ON p.ID = pm_email.post_id AND pm_email.meta_key = '_billing_email'
|
|
|
+ LEFT JOIN {$wpdb->postmeta} pm_fn ON p.ID = pm_fn.post_id AND pm_fn.meta_key = '_billing_first_name'
|
|
|
+ LEFT JOIN {$wpdb->postmeta} pm_ln ON p.ID = pm_ln.post_id AND pm_ln.meta_key = '_billing_last_name'
|
|
|
+ LEFT JOIN {$wpdb->postmeta} pm_phone ON p.ID = pm_phone.post_id AND pm_phone.meta_key = '_billing_phone'
|
|
|
+ LEFT JOIN {$wpdb->postmeta} pm_total ON p.ID = pm_total.post_id AND pm_total.meta_key = '_order_total'
|
|
|
+ WHERE p.post_date >= %s
|
|
|
+ AND p.post_date <= %s
|
|
|
+ AND p.post_status IN ($statuses_placeholder)
|
|
|
+ AND p.post_type = 'shop_order'
|
|
|
+ AND pm_email.meta_value != ''
|
|
|
+ GROUP BY pm_email.meta_value
|
|
|
+ ";
|
|
|
+ }
|
|
|
+
|
|
|
+ private function get_customers_order_clause($orderby, $order) {
|
|
|
+ $order = ($order === 'DESC') ? 'DESC' : 'ASC';
|
|
|
+
|
|
|
+ switch ($orderby) {
|
|
|
+ case 'customer_name':
|
|
|
+ return " ORDER BY customer_name $order";
|
|
|
+ case 'customer_email':
|
|
|
+ return " ORDER BY customer_email $order";
|
|
|
+ case 'orders_count':
|
|
|
+ return " ORDER BY orders_count $order";
|
|
|
+ case 'total_price':
|
|
|
+ return " ORDER BY total_price $order";
|
|
|
+ default:
|
|
|
+ return " ORDER BY customer_name $order";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private function get_order_clause($orderby, $order) {
|
|
|
$order = ($order === 'DESC') ? 'DESC' : 'ASC';
|
|
|
|