| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Classes; |
| 4 |
|
| 5 |
use StoreEngine\Utils\Helper; |
| 6 |
use WP_User; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
class Customers { |
| 13 |
|
| 14 |
public function get_customers( array $query ) { |
| 15 |
// @XXX Why getting only user's with orders. |
| 16 |
// Why not listing customer registered directly without purchase history. |
| 17 |
$users = get_users( array_merge( $query, array( |
| 18 |
'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 19 |
array( |
| 20 |
'key' => 'storeengine_total_orders', |
| 21 |
'value' => 0, |
| 22 |
'compare' => '>=', |
| 23 |
'type' => 'NUMERIC', |
| 24 |
), |
| 25 |
), |
| 26 |
) ) ); |
| 27 |
$customers = array(); |
| 28 |
foreach ( $users as $user ) { |
| 29 |
$customer = new Customer(); |
| 30 |
$customer->set_data( $user ); |
| 31 |
$customers[] = $customer; |
| 32 |
} |
| 33 |
|
| 34 |
return $customers; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Get top customers based total spent. |
| 39 |
* |
| 40 |
* @return Customer[] |
| 41 |
*/ |
| 42 |
public function get_top_customers(): array { |
| 43 |
$users = get_users( [ |
| 44 |
'meta_key' => 'storeengine_total_spent', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 45 |
'orderby' => 'meta_value_num', |
| 46 |
'order' => 'DESC', |
| 47 |
'number' => 10, |
| 48 |
'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 49 |
[ |
| 50 |
'key' => 'storeengine_total_spent', |
| 51 |
'value' => 0, |
| 52 |
'compare' => '>', |
| 53 |
'type' => 'NUMERIC', |
| 54 |
], |
| 55 |
], |
| 56 |
] ); |
| 57 |
|
| 58 |
$top_customers = array(); |
| 59 |
foreach ( $users as $user ) { |
| 60 |
$customer = new Customer(); |
| 61 |
$customer->set_data( $user ); |
| 62 |
$top_customers[] = $customer; |
| 63 |
} |
| 64 |
|
| 65 |
return $top_customers; |
| 66 |
} |
| 67 |
|
| 68 |
public function get_new_customers_count( string $start_date, string $end_date ) { |
| 69 |
// @TODO replace with WP_User_Query or direct sql for performance. |
| 70 |
$users = get_users( [ |
| 71 |
'fields' => 'ID', // For performance improvement (temp see above todo). |
| 72 |
'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 73 |
[ |
| 74 |
'key' => 'storeengine_total_orders', |
| 75 |
'value' => 0, |
| 76 |
'compare' => '>=', |
| 77 |
'type' => 'NUMERIC', |
| 78 |
], |
| 79 |
], |
| 80 |
'date_query' => [ |
| 81 |
[ |
| 82 |
'before' => $end_date, |
| 83 |
'after' => $start_date, |
| 84 |
'inclusive' => true, |
| 85 |
], |
| 86 |
], |
| 87 |
] ); |
| 88 |
|
| 89 |
return count( $users ); |
| 90 |
} |
| 91 |
} |
| 92 |
|