User.php
1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php declare( strict_types=1 );
namespace ACA\WC\Helper;
final class User {
/**
* @param int $user_id
* @param string|array|null $status
*
* @return array
*/
public function get_totals_for_user( $user_id, $status = null ) {
$totals = [];
foreach ( $this->get_orders_by_user( (int) $user_id, $status ) as $order ) {
if ( ! $order->get_total() ) {
continue;
}
$currency = $order->get_currency();
if ( ! isset( $totals[ $currency ] ) ) {
$totals[ $currency ] = 0;
}
$totals[ $currency ] += $order->get_total();
}
return $totals;
}
/**
* @param int $user_id
* @param string|array $status
*
* @return int[]
*/
public function get_order_ids_by_user( $user_id, $status ) {
$args = [
'fields' => 'ids',
'post_type' => 'shop_order',
'posts_per_page' => -1,
'post_status' => 'any',
'meta_query' => [
[
'key' => '_customer_user',
'value' => (int) $user_id,
],
],
];
if ( $status ) {
$args['post_status'] = $status;
}
$order_ids = get_posts( $args );
if ( ! $order_ids ) {
return [];
}
return $order_ids;
}
/**
* @param int $user_id
* @param string|array $status
*
* @return \WC_Order[]|array
*/
public function get_orders_by_user( $user_id, $status = [ 'wc-completed', 'wc-processing' ] ) {
$orders = [];
foreach ( $this->get_order_ids_by_user( (int) $user_id, $status ) as $order_id ) {
$orders[] = wc_get_order( $order_id );
}
return $orders;
}
}