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 / Attributes / CustomerHistory.php

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

293 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignoreFile
2 /**
3 * Product Attributes
4 *
5 * @package Disco
6 * @subpackage \App\Attributes
7 */
8
9 namespace Disco\App\Attributes;
10
11 /**
12 * Class Product
13 *
14 * This class provides methods for retrieving various attributes of a WooCommerce product.
15 *
16 * @package Disco
17 * @subpackage \App\Attributes
18 * @author Ohidul Islam <wahid0003@gmail.com>
19 * @link https://webappick.com
20 * @license https://opensource.org/licenses/gpl-license.php GNU Public License
21 * @category Attributes
22 */
23 class CustomerHistory {
24
25 /**
26 * @var int
27 */
28 private $customer_id;
29
30 /**
31 * @var array
32 */
33 private $ids;
34
35 /**
36 * Customer constructor.
37 *
38 * @param array|object $ids Product IDs.
39 * @param int $customer_id Customer ID.
40 */
41 public function __construct( $ids = array(), $customer_id = 0 ) {
42 $this->customer_id = $customer_id;
43
44 if ( is_user_logged_in() ) {
45 $this->customer_id = wp_get_current_user()->ID;
46 }
47
48 if ( is_object( $ids ) ) {
49 $ids = (array) $ids;
50 }
51
52 $this->ids = $ids;
53 }
54
55 /**
56 * Is Customer First Order.
57 *
58 * @return string
59 */
60 public function is_first_order() {
61 $orders = wc_get_customer_order_count( $this->customer_id );
62
63 if ( $orders > 0 ) {
64 return 'no';
65 }
66
67 return 'yes';
68 }
69
70 /**
71 * Get Customer Last Order Date.
72 *
73 * @return string|null
74 */
75 public function last_order_date() {
76 $last_order = wc_get_customer_last_order( $this->customer_id );
77
78 if ( ! $last_order instanceof \WC_Order ) {
79 return null;
80 }
81
82 $last_order_date = $last_order->get_date_created();
83
84 if ( $last_order_date instanceof \WC_DateTime ) {
85 return $last_order_date->date_i18n();
86 }
87
88 return null;
89 }
90
91 /**
92 * Get Customer Last Order Amount.
93 *
94 * @return float
95 */
96 public function last_order_amount() {
97 $last_order = wc_get_customer_last_order( $this->customer_id );
98
99 if ( ! $last_order instanceof \WC_Order ) {
100 return 0.00;
101 }
102
103 return $last_order->get_total();
104 }
105
106 /**
107 * Get Total Order Made By Customer.
108 *
109 * @return int
110 */
111 public function total_order_made() {
112 return wc_get_customer_order_count( $this->customer_id );
113 }
114
115 /**
116 * Get Total Amount Spent By Customer.
117 *
118 * @return string
119 */
120 public function total_spent() {
121 return wc_get_customer_total_spent( $this->customer_id );
122 }
123
124 /**
125 * Get total amount spent by customer
126 *
127 * @return float
128 */
129 public function total_amount_sold() {
130 $orders = wc_get_orders(
131 array(
132 'customer_id' => $this->customer_id,
133 'limit' => -1, // no limit
134 'return' => 'objects',
135 'status' => array_keys( wc_get_order_statuses() ), // all statuses
136 )
137 );
138
139 $total = 0;
140
141 /** @phpstan-ignore-next-line */
142 foreach ( $orders as $order ) {
143 $total += $order->get_total();
144 }
145
146 return $total;
147 }
148
149 /**
150 * Get Total Quantity Sold By Customer.
151 *
152 * @return int
153 */
154 public function total_quantity_sold() {
155 $total = 0;
156
157 $orders = wc_get_orders(
158 array(
159 'customer_id' => $this->customer_id,
160 'limit' => -1,
161 'return' => 'objects',
162 'status' => array_keys( wc_get_order_statuses() ), // include all statuses
163 )
164 );
165
166 if ( ! empty( $orders ) ) {
167 /** @phpstan-ignore-next-line */
168 foreach ( $orders as $order ) {
169 foreach ( $order->get_items() as $item ) {
170 $qty = (int) $item->get_quantity();
171 $total += $qty;
172 }
173 }
174 }
175
176 return (int) $total;
177 }
178
179 /**
180 * Get Total Quantity Sold By Specific Customers.
181 *
182 * @return int
183 */
184 public function total_quantity_sold_by_ids() {
185 $total_sold = 0;
186
187 if ( ! empty( $this->ids ) ) {
188 $quantities = array();
189
190 foreach ( $this->ids as $id ) {
191 $product = wc_get_product( $id );
192
193 if ( ! ( $product instanceof \WC_Product ) ) {
194 continue;
195 }
196
197 $quantities[] = $product->get_total_sales();
198 }
199
200 $total_sold = array_sum( $quantities );
201 }
202
203 return $total_sold;
204 }
205
206 /**
207 * Get Total Net Revenue By Specific Customers.
208 *
209 * @return float|int
210 */
211 public function total_amount_sold_by_ids() {
212 $total_sold = 0;
213
214 if ( ! empty( $this->ids ) ) {
215 $quantities = array();
216
217 foreach ( $this->ids as $id ) {
218 $quantities[] = $this->net_revenue( $id );
219 }
220
221 $total_sold = array_sum( $quantities );
222 }
223
224 return $total_sold;
225 }
226
227 /**
228 * Get products sum of total order quantity.
229 *
230 * @return float|int
231 */
232 public function total_order_made_by_ids() {
233 $total_sold = 0;
234
235 if ( ! empty( $this->ids ) ) {
236 $quantities = array();
237
238 foreach ( $this->ids as $id ) {
239 $quantities[] = $this->order_quantity( $id );
240 }
241
242 $total_sold = array_sum( $quantities );
243 }
244
245 return $total_sold;
246 }
247
248 /**
249 * Get Single product total order quantity.
250 *
251 * @param int $product_id Product ID.
252 * @return float
253 */
254 private function order_quantity( $product_id ) {
255 global $wpdb;
256
257 return (float) $wpdb->get_var(// phpcs:ignore
258 $wpdb->prepare(
259 "
260 SELECT COUNT(DISTINCT(order_id))
261 FROM {$wpdb->prefix}wc_order_product_lookup
262 WHERE product_id = %d AND customer_id = %d
263 ",
264 $product_id,
265 $this->customer_id
266 )
267 );
268 }
269
270 /**
271 * Get Single product total net revenue.
272 *
273 * @param int $product_id Product ID.
274 * @return float
275 */
276 private function net_revenue( $product_id ) {
277 global $wpdb;
278
279 return (float) $wpdb->get_var(// phpcs:ignore
280 $wpdb->prepare(
281 "
282 SELECT SUM(product_net_revenue)
283 FROM {$wpdb->prefix}wc_order_product_lookup
284 WHERE product_id = %d AND customer_id = %d
285 ",
286 $product_id,
287 $this->customer_id
288 )
289 );
290 }
291
292 }
293