PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.2.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.2.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / classes / order-collection.php

order-collection.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.2.0, at includes/classes/order-collection.php

136 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace StoreEngine\Classes;
4
5 use StoreEngine\Classes\Exceptions\StoreEngineInvalidArgumentException;
6 use StoreEngine\Utils\Helper;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * @method array<Order|Refund> get_results()
14 * @method Order|Refund next_result()
15 */
16 class OrderCollection extends AbstractCollection {
17 protected string $table = 'storeengine_orders';
18
19 protected string $object_type = 'order';
20
21 protected string $meta_type = 'order';
22
23 protected string $primary_key = 'id';
24
25 protected string $parent_key = 'parent_order_id';
26
27 protected string $returnType = Order::class; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase
28
29 /**
30 * @param array|string $query
31 * @param ?string $type Deprecated, use where with type column (key). Optional. order type. Possible values are order, refund, subscription etc.
32 *
33 * @throws StoreEngineInvalidArgumentException
34 */
35 public function __construct( $query = '', string $type = null ) {
36 if ( $type ) {
37 _deprecated_argument( __METHOD__, '1.5.2', esc_html__( 'Usage of order type argument is deprecated. Use where query param with type column (key).', 'storeengine' ) );
38 $this->must_where = [
39 'relation' => 'AND',
40 [
41 'key' => 'type',
42 'value' => $type,
43 'compare' => '=',
44 ],
45 ];
46 }
47
48 parent::__construct( $query );
49 }
50
51 protected function get_return_type( $result ) {
52 if ( isset( $result->type ) ) {
53 return Helper::get_order_type_class( $result->type );
54 }
55
56 if ( is_numeric( $result ) ) {
57 return Helper::get_class_name_for_order_id( absint( $result ) );
58 }
59
60 return false;
61 }
62
63 /**
64 * Get purchase history for customer.
65 *
66 * @param int $customer_id
67 *
68 * @return array
69 */
70 public static function get_purchase_history( int $customer_id ): array {
71 global $wpdb;
72 // @FIXME sort & limit to return last item.
73 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
74 $results = $wpdb->get_results(
75 $wpdb->prepare(
76 "
77 SELECT product_id, SUM(product_qty) as total_qty
78 FROM {$wpdb->prefix}storeengine_order_product_lookup
79 WHERE order_id IN (
80 SELECT id
81 FROM {$wpdb->prefix}storeengine_orders
82 WHERE customer_id = %d
83 )
84 GROUP BY product_id
85 ",
86 $customer_id
87 ),
88 ARRAY_A
89 );
90 // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
91
92 $products = [];
93 foreach ( $results as $product ) {
94 $products[] = [
95 'product_id' => $product['product_id'],
96 'product_name' => get_the_title( $product['product_id'] ),
97 'total_quantity' => $product['total_qty'],
98 ];
99 }
100
101 return $products;
102 }
103
104 /**
105 * Get customer's total spent based on existing order data.
106 *
107 * @param int $customer_id
108 *
109 * @return float
110 */
111 public static function get_total_spent( int $customer_id ): float {
112 global $wpdb;
113
114 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
115 return (float) $wpdb->get_var(
116 $wpdb->prepare(
117 "
118 SELECT SUM(
119 CASE
120 WHEN status = 'completed' THEN total_amount
121 WHEN status = 'refunded' THEN -total_amount
122 ELSE 0
123 END
124 ) AS total_spent
125 FROM {$wpdb->prefix}storeengine_orders
126 WHERE customer_id = %d;
127 ",
128 $customer_id
129 )
130 );
131 // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
132 }
133 }
134
135 // End of file order-collection.php.
136