|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Contains Discount calculation class |
|
4
|
|
|
* |
|
5
|
|
|
* @since 1.0.15 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
defined( 'ABSPATH' ) || exit; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Discount class. |
|
12
|
|
|
* |
|
13
|
|
|
* @since 1.0.15 |
|
14
|
|
|
* |
|
15
|
|
|
*/ |
|
16
|
|
|
class WPInv_Discount extends GetPaid_Data { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Which data store to load. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $data_store_name = 'discount'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* This is the name of this object type. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $object_type = 'discount'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Discount Data array. This is the core item data exposed in APIs. |
|
34
|
|
|
* |
|
35
|
|
|
* @since 1.0.19 |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $data = array( |
|
39
|
|
|
'status' => 'draft', |
|
40
|
|
|
'version' => '', |
|
41
|
|
|
'date_created' => null, |
|
42
|
|
|
'date_modified' => null, |
|
43
|
|
|
'name' => 'no-name', |
|
44
|
|
|
'description' => '', |
|
45
|
|
|
'author' => 1, |
|
46
|
|
|
'code' => null, |
|
47
|
|
|
'type' => 'percent', |
|
48
|
|
|
'expiration' => null, |
|
49
|
|
|
'start' => null, |
|
50
|
|
|
'items' => array(), |
|
51
|
|
|
'excluded_items' => array(), |
|
52
|
|
|
'required_items' => array(), |
|
53
|
|
|
'uses' => 0, |
|
54
|
|
|
'max_uses' => null, |
|
55
|
|
|
'is_recurring' => null, |
|
56
|
|
|
'is_single_use' => null, |
|
57
|
|
|
'min_total' => null, |
|
58
|
|
|
'max_total' => null, |
|
59
|
|
|
'amount' => null, |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Stores meta in cache for future reads. |
|
64
|
|
|
* |
|
65
|
|
|
* A group must be set to to enable caching. |
|
66
|
|
|
* |
|
67
|
|
|
* @var string |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $cache_group = 'getpaid_discounts'; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Stores a reference to the original WP_Post object |
|
73
|
|
|
* |
|
74
|
|
|
* @var WP_Post |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $post = null; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get the discount if ID is passed, otherwise the discount is new and empty. |
|
80
|
|
|
* |
|
81
|
|
|
* @param int|array|string|WPInv_Discount|WP_Post $discount discount data, object, ID or code. |
|
82
|
|
|
*/ |
|
83
|
|
|
public function __construct( $discount = 0 ) { |
|
84
|
|
|
parent::__construct( $discount ); |
|
85
|
|
|
|
|
86
|
|
|
if ( is_numeric( $discount ) && 'wpi_discount' === get_post_type( $discount ) ) { |
|
|
|
|
|
|
87
|
|
|
$this->set_id( $discount ); |
|
|
|
|
|
|
88
|
|
|
} elseif ( $discount instanceof self ) { |
|
89
|
|
|
$this->set_id( $discount->get_id() ); |
|
90
|
|
|
} elseif ( ! empty( $discount->ID ) ) { |
|
91
|
|
|
$this->set_id( $discount->ID ); |
|
92
|
|
|
} elseif ( is_array( $discount ) ) { |
|
93
|
|
|
$this->set_props( $discount ); |
|
94
|
|
|
|
|
95
|
|
|
if ( isset( $discount['ID'] ) ) { |
|
96
|
|
|
$this->set_id( $discount['ID'] ); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
} elseif ( is_scalar( $discount ) && $discount = self::get_discount_id_by_code( $discount ) ) { |
|
100
|
|
|
$this->set_id( $discount ); |
|
101
|
|
|
} else { |
|
102
|
|
|
$this->set_object_read( true ); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// Load the datastore. |
|
106
|
|
|
$this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
107
|
|
|
|
|
108
|
|
|
if ( $this->get_id() > 0 ) { |
|
109
|
|
|
$this->post = get_post( $this->get_id() ); |
|
|
|
|
|
|
110
|
|
|
$this->ID = $this->get_id(); |
|
|
|
|
|
|
111
|
|
|
$this->data_store->read( $this ); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Fetch a discount from the db/cache |
|
118
|
|
|
* |
|
119
|
|
|
* |
|
120
|
|
|
* @static |
|
121
|
|
|
* @param string $field The field to query against: 'ID', 'discount_code' |
|
122
|
|
|
* @param string|int $value The field value |
|
123
|
|
|
* @deprecated |
|
124
|
|
|
* @since 1.0.15 |
|
125
|
|
|
* @return array|bool array of discount details on success. False otherwise. |
|
126
|
|
|
*/ |
|
127
|
|
|
public static function get_data_by( $field, $value ) { |
|
128
|
|
|
|
|
129
|
|
|
if ( 'id' == strtolower( $field ) ) { |
|
130
|
|
|
// Make sure the value is numeric to avoid casting objects, for example, |
|
131
|
|
|
// to int 1. |
|
132
|
|
|
if ( ! is_numeric( $value ) ) |
|
133
|
|
|
return false; |
|
134
|
|
|
$value = intval( $value ); |
|
135
|
|
|
if ( $value < 1 ) |
|
136
|
|
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
if ( ! $value || ! is_string( $field ) ) { |
|
140
|
|
|
return false; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$field = trim( $field ); |
|
144
|
|
|
|
|
145
|
|
|
// prepare query args |
|
146
|
|
|
switch ( strtolower( $field ) ) { |
|
147
|
|
|
case 'id': |
|
148
|
|
|
$discount_id = $value; |
|
149
|
|
|
$args = array( 'include' => array( $value ) ); |
|
150
|
|
|
break; |
|
151
|
|
|
case 'discount_code': |
|
152
|
|
|
case 'code': |
|
153
|
|
|
$value = trim( $value ); |
|
154
|
|
|
$discount_id = wp_cache_get( $value, 'WPInv_Discount_Codes' ); |
|
155
|
|
|
$args = array( 'meta_key' => '_wpi_discount_code', 'meta_value' => $value ); |
|
156
|
|
|
break; |
|
157
|
|
|
case 'name': |
|
158
|
|
|
$discount_id = 0; |
|
159
|
|
|
$args = array( 'name' => trim( $value ) ); |
|
160
|
|
|
break; |
|
161
|
|
|
default: |
|
162
|
|
|
$args = apply_filters( "wpinv_discount_get_data_by_{$field}_args", null, $value ); |
|
163
|
|
|
if ( ! is_array( $args ) ) { |
|
164
|
|
|
return apply_filters( "wpinv_discount_get_data_by_$field", false, $value ); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
// Check if there is a cached value. |
|
170
|
|
|
if ( ! empty( $discount_id ) && $discount = wp_cache_get( (int) $discount_id, 'WPInv_Discounts' ) ) { |
|
171
|
|
|
return $discount; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$args = array_merge( |
|
175
|
|
|
$args, |
|
176
|
|
|
array( |
|
177
|
|
|
'post_type' => 'wpi_discount', |
|
178
|
|
|
'posts_per_page' => 1, |
|
179
|
|
|
'post_status' => array( 'publish', 'pending', 'draft', 'expired' ) |
|
180
|
|
|
) |
|
181
|
|
|
); |
|
182
|
|
|
|
|
183
|
|
|
$discount = get_posts( $args ); |
|
184
|
|
|
|
|
185
|
|
|
if( empty( $discount ) ) { |
|
186
|
|
|
return false; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
$discount = $discount[0]; |
|
190
|
|
|
|
|
191
|
|
|
// Prepare the return data. |
|
192
|
|
|
$return = array( |
|
193
|
|
|
'ID' => $discount->ID, |
|
194
|
|
|
'code' => get_post_meta( $discount->ID, '_wpi_discount_code', true ), |
|
195
|
|
|
'amount' => get_post_meta( $discount->ID, '_wpi_discount_amount', true ), |
|
196
|
|
|
'date_created' => $discount->post_date, |
|
197
|
|
|
'date_modified' => $discount->post_modified, |
|
198
|
|
|
'status' => $discount->post_status, |
|
199
|
|
|
'start' => get_post_meta( $discount->ID, '_wpi_discount_start', true ), |
|
200
|
|
|
'expiration' => get_post_meta( $discount->ID, '_wpi_discount_expiration', true ), |
|
201
|
|
|
'type' => get_post_meta( $discount->ID, '_wpi_discount_type', true ), |
|
202
|
|
|
'description' => $discount->post_excerpt, |
|
203
|
|
|
'uses' => get_post_meta( $discount->ID, '_wpi_discount_uses', true ), |
|
204
|
|
|
'is_single_use' => get_post_meta( $discount->ID, '_wpi_discount_is_single_use', true ), |
|
205
|
|
|
'items' => get_post_meta( $discount->ID, '_wpi_discount_items', true ), |
|
206
|
|
|
'excluded_items' => get_post_meta( $discount->ID, '_wpi_discount_excluded_items', true ), |
|
207
|
|
|
'required_items' => get_post_meta( $discount->ID, '_wpi_discount_required_items', true ), |
|
208
|
|
|
'max_uses' => get_post_meta( $discount->ID, '_wpi_discount_max_uses', true ), |
|
209
|
|
|
'is_recurring' => get_post_meta( $discount->ID, '_wpi_discount_is_recurring', true ), |
|
210
|
|
|
'min_total' => get_post_meta( $discount->ID, '_wpi_discount_min_total', true ), |
|
211
|
|
|
'max_total' => get_post_meta( $discount->ID, '_wpi_discount_max_total', true ), |
|
212
|
|
|
); |
|
213
|
|
|
|
|
214
|
|
|
$return = apply_filters( 'wpinv_discount_properties', $return ); |
|
215
|
|
|
|
|
216
|
|
|
// Update the cache with our data |
|
217
|
|
|
wp_cache_add( $discount->ID, $return, 'WPInv_Discounts' ); |
|
218
|
|
|
wp_cache_add( $return['code'], $discount->ID, 'WPInv_Discount_Codes' ); |
|
219
|
|
|
|
|
220
|
|
|
return $return; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Given a discount code, it returns a discount id. |
|
225
|
|
|
* |
|
226
|
|
|
* |
|
227
|
|
|
* @static |
|
228
|
|
|
* @param string $discount_code |
|
229
|
|
|
* @since 1.0.15 |
|
230
|
|
|
* @return int |
|
231
|
|
|
*/ |
|
232
|
|
|
public static function get_discount_id_by_code( $discount_code ) { |
|
233
|
|
|
|
|
234
|
|
|
// Trim the code. |
|
235
|
|
|
$discount_code = trim( $discount_code ); |
|
236
|
|
|
|
|
237
|
|
|
// Ensure a value has been passed. |
|
238
|
|
|
if ( empty( $discount_code ) ) { |
|
239
|
|
|
return 0; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
// Maybe retrieve from the cache. |
|
243
|
|
|
$discount_id = wp_cache_get( $discount_code, 'getpaid_discount_codes' ); |
|
244
|
|
|
if ( ! empty( $discount_id ) ) { |
|
245
|
|
|
return $discount_id; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
// Fetch the first discount codes. |
|
249
|
|
|
$discounts = get_posts( |
|
250
|
|
|
array( |
|
251
|
|
|
'meta_key' => '_wpi_discount_code', |
|
252
|
|
|
'meta_value' => $discount_code, |
|
253
|
|
|
'post_type' => 'wpi_discount', |
|
254
|
|
|
'posts_per_page' => 1, |
|
255
|
|
|
'post_status' => array( 'publish', 'pending', 'draft', 'expired' ), |
|
256
|
|
|
'fields' => 'ids', |
|
257
|
|
|
) |
|
258
|
|
|
); |
|
259
|
|
|
|
|
260
|
|
|
if ( empty( $discounts ) ) { |
|
261
|
|
|
return 0; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
$discount_id = $discounts[0]; |
|
265
|
|
|
|
|
266
|
|
|
// Update the cache with our data |
|
267
|
|
|
wp_cache_add( get_post_meta( $discount_id, '_wpi_discount_code', true ), $discount_id, 'getpaid_discount_codes' ); |
|
|
|
|
|
|
268
|
|
|
|
|
269
|
|
|
return $discount_id; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* Magic method for checking the existence of a certain custom field. |
|
274
|
|
|
* |
|
275
|
|
|
* @since 1.0.15 |
|
276
|
|
|
* @access public |
|
277
|
|
|
* |
|
278
|
|
|
* @return bool Whether the given discount field is set. |
|
279
|
|
|
*/ |
|
280
|
|
|
public function __isset( $key ){ |
|
281
|
|
|
return isset( $this->data[$key] ) || method_exists( $this, "get_$key"); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/* |
|
285
|
|
|
|-------------------------------------------------------------------------- |
|
286
|
|
|
| CRUD methods |
|
287
|
|
|
|-------------------------------------------------------------------------- |
|
288
|
|
|
| |
|
289
|
|
|
| Methods which create, read, update and delete discounts from the database. |
|
290
|
|
|
| |
|
291
|
|
|
*/ |
|
292
|
|
|
|
|
293
|
|
|
/* |
|
294
|
|
|
|-------------------------------------------------------------------------- |
|
295
|
|
|
| Getters |
|
296
|
|
|
|-------------------------------------------------------------------------- |
|
297
|
|
|
*/ |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* Get discount status. |
|
301
|
|
|
* |
|
302
|
|
|
* @since 1.0.19 |
|
303
|
|
|
* @param string $context View or edit context. |
|
304
|
|
|
* @return string |
|
305
|
|
|
*/ |
|
306
|
|
|
public function get_status( $context = 'view' ) { |
|
307
|
|
|
return $this->get_prop( 'status', $context ); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Get plugin version when the discount was created. |
|
312
|
|
|
* |
|
313
|
|
|
* @since 1.0.19 |
|
314
|
|
|
* @param string $context View or edit context. |
|
315
|
|
|
* @return string |
|
316
|
|
|
*/ |
|
317
|
|
|
public function get_version( $context = 'view' ) { |
|
318
|
|
|
return $this->get_prop( 'version', $context ); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Get date when the discount was created. |
|
323
|
|
|
* |
|
324
|
|
|
* @since 1.0.19 |
|
325
|
|
|
* @param string $context View or edit context. |
|
326
|
|
|
* @return string |
|
327
|
|
|
*/ |
|
328
|
|
|
public function get_date_created( $context = 'view' ) { |
|
329
|
|
|
return $this->get_prop( 'date_created', $context ); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* Get GMT date when the discount was created. |
|
334
|
|
|
* |
|
335
|
|
|
* @since 1.0.19 |
|
336
|
|
|
* @param string $context View or edit context. |
|
337
|
|
|
* @return string |
|
338
|
|
|
*/ |
|
339
|
|
|
public function get_date_created_gmt( $context = 'view' ) { |
|
340
|
|
|
$date = $this->get_date_created( $context ); |
|
341
|
|
|
|
|
342
|
|
|
if ( $date ) { |
|
343
|
|
|
$date = get_gmt_from_date( $date ); |
|
344
|
|
|
} |
|
345
|
|
|
return $date; |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
/** |
|
349
|
|
|
* Get date when the discount was last modified. |
|
350
|
|
|
* |
|
351
|
|
|
* @since 1.0.19 |
|
352
|
|
|
* @param string $context View or edit context. |
|
353
|
|
|
* @return string |
|
354
|
|
|
*/ |
|
355
|
|
|
public function get_date_modified( $context = 'view' ) { |
|
356
|
|
|
return $this->get_prop( 'date_modified', $context ); |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
/** |
|
360
|
|
|
* Get GMT date when the discount was last modified. |
|
361
|
|
|
* |
|
362
|
|
|
* @since 1.0.19 |
|
363
|
|
|
* @param string $context View or edit context. |
|
364
|
|
|
* @return string |
|
365
|
|
|
*/ |
|
366
|
|
|
public function get_date_modified_gmt( $context = 'view' ) { |
|
367
|
|
|
$date = $this->get_date_modified( $context ); |
|
368
|
|
|
|
|
369
|
|
|
if ( $date ) { |
|
370
|
|
|
$date = get_gmt_from_date( $date ); |
|
371
|
|
|
} |
|
372
|
|
|
return $date; |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
/** |
|
376
|
|
|
* Get the discount name. |
|
377
|
|
|
* |
|
378
|
|
|
* @since 1.0.19 |
|
379
|
|
|
* @param string $context View or edit context. |
|
380
|
|
|
* @return string |
|
381
|
|
|
*/ |
|
382
|
|
|
public function get_name( $context = 'view' ) { |
|
383
|
|
|
return $this->get_prop( 'name', $context ); |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
/** |
|
387
|
|
|
* Alias of self::get_name(). |
|
388
|
|
|
* |
|
389
|
|
|
* @since 1.0.19 |
|
390
|
|
|
* @param string $context View or edit context. |
|
391
|
|
|
* @return string |
|
392
|
|
|
*/ |
|
393
|
|
|
public function get_title( $context = 'view' ) { |
|
394
|
|
|
return $this->get_name( $context ); |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
/** |
|
398
|
|
|
* Get the discount description. |
|
399
|
|
|
* |
|
400
|
|
|
* @since 1.0.19 |
|
401
|
|
|
* @param string $context View or edit context. |
|
402
|
|
|
* @return string |
|
403
|
|
|
*/ |
|
404
|
|
|
public function get_description( $context = 'view' ) { |
|
405
|
|
|
return $this->get_prop( 'description', $context ); |
|
406
|
|
|
} |
|
407
|
|
|
|
|
408
|
|
|
/** |
|
409
|
|
|
* Alias of self::get_description(). |
|
410
|
|
|
* |
|
411
|
|
|
* @since 1.0.19 |
|
412
|
|
|
* @param string $context View or edit context. |
|
413
|
|
|
* @return string |
|
414
|
|
|
*/ |
|
415
|
|
|
public function get_excerpt( $context = 'view' ) { |
|
416
|
|
|
return $this->get_description( $context ); |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
/** |
|
420
|
|
|
* Alias of self::get_description(). |
|
421
|
|
|
* |
|
422
|
|
|
* @since 1.0.19 |
|
423
|
|
|
* @param string $context View or edit context. |
|
424
|
|
|
* @return string |
|
425
|
|
|
*/ |
|
426
|
|
|
public function get_summary( $context = 'view' ) { |
|
427
|
|
|
return $this->get_description( $context ); |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
/** |
|
431
|
|
|
* Get the owner of the discount. |
|
432
|
|
|
* |
|
433
|
|
|
* @since 1.0.19 |
|
434
|
|
|
* @param string $context View or edit context. |
|
435
|
|
|
* @return string |
|
436
|
|
|
*/ |
|
437
|
|
|
public function get_author( $context = 'view' ) { |
|
438
|
|
|
return (int) $this->get_prop( 'author', $context ); |
|
439
|
|
|
} |
|
440
|
|
|
|
|
441
|
|
|
/** |
|
442
|
|
|
* Get the discount code. |
|
443
|
|
|
* |
|
444
|
|
|
* @since 1.0.19 |
|
445
|
|
|
* @param string $context View or edit context. |
|
446
|
|
|
* @return string |
|
447
|
|
|
*/ |
|
448
|
|
|
public function get_code( $context = 'view' ) { |
|
449
|
|
|
return $this->get_prop( 'code', $context ); |
|
450
|
|
|
} |
|
451
|
|
|
|
|
452
|
|
|
/** |
|
453
|
|
|
* Alias for self::get_code(). |
|
454
|
|
|
* |
|
455
|
|
|
* @since 1.0.19 |
|
456
|
|
|
* @param string $context View or edit context. |
|
457
|
|
|
* @return string |
|
458
|
|
|
*/ |
|
459
|
|
|
public function get_coupon_code( $context = 'view' ) { |
|
460
|
|
|
return $this->get_code( $context ); |
|
461
|
|
|
} |
|
462
|
|
|
|
|
463
|
|
|
/** |
|
464
|
|
|
* Alias for self::get_code(). |
|
465
|
|
|
* |
|
466
|
|
|
* @since 1.0.19 |
|
467
|
|
|
* @param string $context View or edit context. |
|
468
|
|
|
* @return string |
|
469
|
|
|
*/ |
|
470
|
|
|
public function get_discount_code( $context = 'view' ) { |
|
471
|
|
|
return $this->get_code( $context ); |
|
472
|
|
|
} |
|
473
|
|
|
|
|
474
|
|
|
/** |
|
475
|
|
|
* Get the discount's amount. |
|
476
|
|
|
* |
|
477
|
|
|
* @since 1.0.19 |
|
478
|
|
|
* @param string $context View or edit context. |
|
479
|
|
|
* @return float |
|
480
|
|
|
*/ |
|
481
|
|
|
public function get_amount( $context = 'view' ) { |
|
482
|
|
|
return $context == 'view' ? floatval( $this->get_prop( 'amount', $context ) ) : $this->get_prop( 'amount', $context ); |
|
483
|
|
|
} |
|
484
|
|
|
|
|
485
|
|
|
/** |
|
486
|
|
|
* Get the discount's formated amount/rate. |
|
487
|
|
|
* |
|
488
|
|
|
* @since 1.0.19 |
|
489
|
|
|
* @return string |
|
490
|
|
|
*/ |
|
491
|
|
|
public function get_formatted_amount() { |
|
492
|
|
|
|
|
493
|
|
|
if ( $this->is_type( 'flat' ) ) { |
|
494
|
|
|
$rate = wpinv_price( $this->get_amount() ); |
|
495
|
|
|
} else { |
|
496
|
|
|
$rate = $this->get_amount() . '%'; |
|
497
|
|
|
} |
|
498
|
|
|
|
|
499
|
|
|
return apply_filters( 'wpinv_format_discount_rate', $rate, $this->get_type(), $this->get_amount() ); |
|
500
|
|
|
} |
|
501
|
|
|
|
|
502
|
|
|
/** |
|
503
|
|
|
* Get the discount's start date. |
|
504
|
|
|
* |
|
505
|
|
|
* @since 1.0.19 |
|
506
|
|
|
* @param string $context View or edit context. |
|
507
|
|
|
* @return string |
|
508
|
|
|
*/ |
|
509
|
|
|
public function get_start( $context = 'view' ) { |
|
510
|
|
|
return $this->get_prop( 'start', $context ); |
|
511
|
|
|
} |
|
512
|
|
|
|
|
513
|
|
|
/** |
|
514
|
|
|
* Alias for self::get_start(). |
|
515
|
|
|
* |
|
516
|
|
|
* @since 1.0.19 |
|
517
|
|
|
* @param string $context View or edit context. |
|
518
|
|
|
* @return string |
|
519
|
|
|
*/ |
|
520
|
|
|
public function get_start_date( $context = 'view' ) { |
|
521
|
|
|
return $this->get_start( $context ); |
|
522
|
|
|
} |
|
523
|
|
|
|
|
524
|
|
|
/** |
|
525
|
|
|
* Get the discount's expiration date. |
|
526
|
|
|
* |
|
527
|
|
|
* @since 1.0.19 |
|
528
|
|
|
* @param string $context View or edit context. |
|
529
|
|
|
* @return string |
|
530
|
|
|
*/ |
|
531
|
|
|
public function get_expiration( $context = 'view' ) { |
|
532
|
|
|
return $this->get_prop( 'expiration', $context ); |
|
533
|
|
|
} |
|
534
|
|
|
|
|
535
|
|
|
/** |
|
536
|
|
|
* Alias for self::get_expiration(). |
|
537
|
|
|
* |
|
538
|
|
|
* @since 1.0.19 |
|
539
|
|
|
* @param string $context View or edit context. |
|
540
|
|
|
* @return string |
|
541
|
|
|
*/ |
|
542
|
|
|
public function get_expiration_date( $context = 'view' ) { |
|
543
|
|
|
return $this->get_expiration( $context ); |
|
544
|
|
|
} |
|
545
|
|
|
|
|
546
|
|
|
/** |
|
547
|
|
|
* Alias for self::get_expiration(). |
|
548
|
|
|
* |
|
549
|
|
|
* @since 1.0.19 |
|
550
|
|
|
* @param string $context View or edit context. |
|
551
|
|
|
* @return string |
|
552
|
|
|
*/ |
|
553
|
|
|
public function get_end_date( $context = 'view' ) { |
|
554
|
|
|
return $this->get_expiration( $context ); |
|
555
|
|
|
} |
|
556
|
|
|
|
|
557
|
|
|
/** |
|
558
|
|
|
* Get the discount's type. |
|
559
|
|
|
* |
|
560
|
|
|
* @since 1.0.19 |
|
561
|
|
|
* @param string $context View or edit context. |
|
562
|
|
|
* @return string |
|
563
|
|
|
*/ |
|
564
|
|
|
public function get_type( $context = 'view' ) { |
|
565
|
|
|
return $this->get_prop( 'type', $context ); |
|
566
|
|
|
} |
|
567
|
|
|
|
|
568
|
|
|
/** |
|
569
|
|
|
* Get the number of times a discount has been used. |
|
570
|
|
|
* |
|
571
|
|
|
* @since 1.0.19 |
|
572
|
|
|
* @param string $context View or edit context. |
|
573
|
|
|
* @return int |
|
574
|
|
|
*/ |
|
575
|
|
|
public function get_uses( $context = 'view' ) { |
|
576
|
|
|
return (int) $this->get_prop( 'uses', $context ); |
|
577
|
|
|
} |
|
578
|
|
|
|
|
579
|
|
|
/** |
|
580
|
|
|
* Get the discount's usage, i.e uses / max uses. |
|
581
|
|
|
* |
|
582
|
|
|
* @since 1.0.19 |
|
583
|
|
|
* @return string |
|
584
|
|
|
*/ |
|
585
|
|
|
public function get_usage() { |
|
586
|
|
|
|
|
587
|
|
|
if ( ! $this->has_limit() ) { |
|
588
|
|
|
return $this->get_uses() . ' / ' . ' ∞'; |
|
589
|
|
|
} |
|
590
|
|
|
|
|
591
|
|
|
return $this->get_uses() . ' / ' . (int) $this->get_max_uses(); |
|
592
|
|
|
|
|
593
|
|
|
} |
|
594
|
|
|
|
|
595
|
|
|
/** |
|
596
|
|
|
* Get the maximum number of time a discount can be used. |
|
597
|
|
|
* |
|
598
|
|
|
* @since 1.0.19 |
|
599
|
|
|
* @param string $context View or edit context. |
|
600
|
|
|
* @return int |
|
601
|
|
|
*/ |
|
602
|
|
|
public function get_max_uses( $context = 'view' ) { |
|
603
|
|
|
$max_uses = $this->get_prop( 'max_uses', $context ); |
|
604
|
|
|
return empty( $max_uses ) ? null : $max_uses; |
|
605
|
|
|
} |
|
606
|
|
|
|
|
607
|
|
|
/** |
|
608
|
|
|
* Checks if this is a single use discount or not. |
|
609
|
|
|
* |
|
610
|
|
|
* @since 1.0.19 |
|
611
|
|
|
* @param string $context View or edit context. |
|
612
|
|
|
* @return bool |
|
613
|
|
|
*/ |
|
614
|
|
|
public function get_is_single_use( $context = 'view' ) { |
|
615
|
|
|
return $this->get_prop( 'is_single_use', $context ); |
|
616
|
|
|
} |
|
617
|
|
|
|
|
618
|
|
|
/** |
|
619
|
|
|
* Get the items that can be used with this discount. |
|
620
|
|
|
* |
|
621
|
|
|
* @since 1.0.19 |
|
622
|
|
|
* @param string $context View or edit context. |
|
623
|
|
|
* @return array |
|
624
|
|
|
*/ |
|
625
|
|
|
public function get_items( $context = 'view' ) { |
|
626
|
|
|
return array_filter( wp_parse_id_list( $this->get_prop( 'items', $context ) ) ); |
|
627
|
|
|
} |
|
628
|
|
|
|
|
629
|
|
|
/** |
|
630
|
|
|
* Alias for self::get_items(). |
|
631
|
|
|
* |
|
632
|
|
|
* @since 1.0.19 |
|
633
|
|
|
* @param string $context View or edit context. |
|
634
|
|
|
* @return array |
|
635
|
|
|
*/ |
|
636
|
|
|
public function get_allowed_items( $context = 'view' ) { |
|
637
|
|
|
return $this->get_items( $context ); |
|
638
|
|
|
} |
|
639
|
|
|
|
|
640
|
|
|
/** |
|
641
|
|
|
* Get the items that are not allowed to use this discount. |
|
642
|
|
|
* |
|
643
|
|
|
* @since 1.0.19 |
|
644
|
|
|
* @param string $context View or edit context. |
|
645
|
|
|
* @return array |
|
646
|
|
|
*/ |
|
647
|
|
|
public function get_excluded_items( $context = 'view' ) { |
|
648
|
|
|
return array_filter( wp_parse_id_list( $this->get_prop( 'excluded_items', $context ) ) ); |
|
649
|
|
|
} |
|
650
|
|
|
|
|
651
|
|
|
/** |
|
652
|
|
|
* Get the items that are required to be in the cart before using this discount. |
|
653
|
|
|
* |
|
654
|
|
|
* @since 1.0.19 |
|
655
|
|
|
* @param string $context View or edit context. |
|
656
|
|
|
* @return array |
|
657
|
|
|
*/ |
|
658
|
|
|
public function get_required_items( $context = 'view' ) { |
|
659
|
|
|
return array_filter( wp_parse_id_list( $this->get_prop( 'required_items', $context ) ) ); |
|
660
|
|
|
} |
|
661
|
|
|
|
|
662
|
|
|
/** |
|
663
|
|
|
* Checks if this is a recurring discount or not. |
|
664
|
|
|
* |
|
665
|
|
|
* @since 1.0.19 |
|
666
|
|
|
* @param string $context View or edit context. |
|
667
|
|
|
* @return int|string|bool |
|
668
|
|
|
*/ |
|
669
|
|
|
public function get_is_recurring( $context = 'view' ) { |
|
670
|
|
|
return $this->get_prop( 'is_recurring', $context ); |
|
671
|
|
|
} |
|
672
|
|
|
|
|
673
|
|
|
/** |
|
674
|
|
|
* Get's the minimum total amount allowed for this discount. |
|
675
|
|
|
* |
|
676
|
|
|
* @since 1.0.19 |
|
677
|
|
|
* @param string $context View or edit context. |
|
678
|
|
|
* @return float |
|
679
|
|
|
*/ |
|
680
|
|
|
public function get_min_total( $context = 'view' ) { |
|
681
|
|
|
$minimum = $this->get_prop( 'min_total', $context ); |
|
682
|
|
|
return empty( $minimum ) ? null : $minimum; |
|
683
|
|
|
} |
|
684
|
|
|
|
|
685
|
|
|
/** |
|
686
|
|
|
* Alias for self::get_min_total(). |
|
687
|
|
|
* |
|
688
|
|
|
* @since 1.0.19 |
|
689
|
|
|
* @param string $context View or edit context. |
|
690
|
|
|
* @return float |
|
691
|
|
|
*/ |
|
692
|
|
|
public function get_minimum_total( $context = 'view' ) { |
|
693
|
|
|
return $this->get_min_total( $context ); |
|
694
|
|
|
} |
|
695
|
|
|
|
|
696
|
|
|
/** |
|
697
|
|
|
* Get's the maximum total amount allowed for this discount. |
|
698
|
|
|
* |
|
699
|
|
|
* @since 1.0.19 |
|
700
|
|
|
* @param string $context View or edit context. |
|
701
|
|
|
* @return float |
|
702
|
|
|
*/ |
|
703
|
|
|
public function get_max_total( $context = 'view' ) { |
|
704
|
|
|
$maximum = $this->get_prop( 'max_total', $context ); |
|
705
|
|
|
return empty( $maximum ) ? null : $maximum; |
|
706
|
|
|
} |
|
707
|
|
|
|
|
708
|
|
|
/** |
|
709
|
|
|
* Alias for self::get_max_total(). |
|
710
|
|
|
* |
|
711
|
|
|
* @since 1.0.19 |
|
712
|
|
|
* @param string $context View or edit context. |
|
713
|
|
|
* @return float |
|
714
|
|
|
*/ |
|
715
|
|
|
public function get_maximum_total( $context = 'view' ) { |
|
716
|
|
|
return $this->get_max_total( $context ); |
|
717
|
|
|
} |
|
718
|
|
|
|
|
719
|
|
|
/** |
|
720
|
|
|
* Magic method for accessing discount properties. |
|
721
|
|
|
* |
|
722
|
|
|
* @since 1.0.15 |
|
723
|
|
|
* @access public |
|
724
|
|
|
* |
|
725
|
|
|
* @param string $key Discount data to retrieve |
|
726
|
|
|
* @param string $context View or edit context. |
|
727
|
|
|
* @return mixed Value of the given discount property (if set). |
|
728
|
|
|
*/ |
|
729
|
|
|
public function get( $key, $context = 'view' ) { |
|
730
|
|
|
return $this->get_prop( $key, $context ); |
|
731
|
|
|
} |
|
732
|
|
|
|
|
733
|
|
|
/* |
|
734
|
|
|
|-------------------------------------------------------------------------- |
|
735
|
|
|
| Setters |
|
736
|
|
|
|-------------------------------------------------------------------------- |
|
737
|
|
|
| |
|
738
|
|
|
| Functions for setting discount data. These should not update anything in the |
|
739
|
|
|
| database itself and should only change what is stored in the class |
|
740
|
|
|
| object. |
|
741
|
|
|
*/ |
|
742
|
|
|
|
|
743
|
|
|
/** |
|
744
|
|
|
* Sets discount status. |
|
745
|
|
|
* |
|
746
|
|
|
* @since 1.0.19 |
|
747
|
|
|
* @param string $status New status. |
|
748
|
|
|
* @return array details of change. |
|
749
|
|
|
*/ |
|
750
|
|
|
public function set_status( $status ) { |
|
751
|
|
|
$old_status = $this->get_status(); |
|
752
|
|
|
|
|
753
|
|
|
$this->set_prop( 'status', $status ); |
|
754
|
|
|
|
|
755
|
|
|
return array( |
|
756
|
|
|
'from' => $old_status, |
|
757
|
|
|
'to' => $status, |
|
758
|
|
|
); |
|
759
|
|
|
} |
|
760
|
|
|
|
|
761
|
|
|
/** |
|
762
|
|
|
* Set plugin version when the discount was created. |
|
763
|
|
|
* |
|
764
|
|
|
* @since 1.0.19 |
|
765
|
|
|
*/ |
|
766
|
|
|
public function set_version( $value ) { |
|
767
|
|
|
$this->set_prop( 'version', $value ); |
|
768
|
|
|
} |
|
769
|
|
|
|
|
770
|
|
|
/** |
|
771
|
|
|
* Set date when the discount was created. |
|
772
|
|
|
* |
|
773
|
|
|
* @since 1.0.19 |
|
774
|
|
|
* @param string $value Value to set. |
|
775
|
|
|
* @return bool Whether or not the date was set. |
|
776
|
|
|
*/ |
|
777
|
|
|
public function set_date_created( $value ) { |
|
778
|
|
|
$date = strtotime( $value ); |
|
779
|
|
|
|
|
780
|
|
|
if ( $date ) { |
|
781
|
|
|
$this->set_prop( 'date_created', date( 'Y-m-d H:i:s', $date ) ); |
|
782
|
|
|
return true; |
|
783
|
|
|
} |
|
784
|
|
|
|
|
785
|
|
|
return false; |
|
786
|
|
|
|
|
787
|
|
|
} |
|
788
|
|
|
|
|
789
|
|
|
/** |
|
790
|
|
|
* Set date when the discount was last modified. |
|
791
|
|
|
* |
|
792
|
|
|
* @since 1.0.19 |
|
793
|
|
|
* @param string $value Value to set. |
|
794
|
|
|
* @return bool Whether or not the date was set. |
|
795
|
|
|
*/ |
|
796
|
|
|
public function set_date_modified( $value ) { |
|
797
|
|
|
$date = strtotime( $value ); |
|
798
|
|
|
|
|
799
|
|
|
if ( $date ) { |
|
800
|
|
|
$this->set_prop( 'date_modified', date( 'Y-m-d H:i:s', $date ) ); |
|
801
|
|
|
return true; |
|
802
|
|
|
} |
|
803
|
|
|
|
|
804
|
|
|
return false; |
|
805
|
|
|
|
|
806
|
|
|
} |
|
807
|
|
|
|
|
808
|
|
|
/** |
|
809
|
|
|
* Set the discount name. |
|
810
|
|
|
* |
|
811
|
|
|
* @since 1.0.19 |
|
812
|
|
|
* @param string $value New name. |
|
813
|
|
|
*/ |
|
814
|
|
|
public function set_name( $value ) { |
|
815
|
|
|
$name = sanitize_text_field( $value ); |
|
816
|
|
|
$this->set_prop( 'name', $name ); |
|
817
|
|
|
} |
|
818
|
|
|
|
|
819
|
|
|
/** |
|
820
|
|
|
* Alias of self::set_name(). |
|
821
|
|
|
* |
|
822
|
|
|
* @since 1.0.19 |
|
823
|
|
|
* @param string $value New name. |
|
824
|
|
|
*/ |
|
825
|
|
|
public function set_title( $value ) { |
|
826
|
|
|
$this->set_name( $value ); |
|
827
|
|
|
} |
|
828
|
|
|
|
|
829
|
|
|
/** |
|
830
|
|
|
* Set the discount description. |
|
831
|
|
|
* |
|
832
|
|
|
* @since 1.0.19 |
|
833
|
|
|
* @param string $value New description. |
|
834
|
|
|
*/ |
|
835
|
|
|
public function set_description( $value ) { |
|
836
|
|
|
$description = wp_kses_post( $value ); |
|
837
|
|
|
return $this->set_prop( 'description', $description ); |
|
|
|
|
|
|
838
|
|
|
} |
|
839
|
|
|
|
|
840
|
|
|
/** |
|
841
|
|
|
* Alias of self::set_description(). |
|
842
|
|
|
* |
|
843
|
|
|
* @since 1.0.19 |
|
844
|
|
|
* @param string $value New description. |
|
845
|
|
|
*/ |
|
846
|
|
|
public function set_excerpt( $value ) { |
|
847
|
|
|
$this->set_description( $value ); |
|
848
|
|
|
} |
|
849
|
|
|
|
|
850
|
|
|
/** |
|
851
|
|
|
* Alias of self::set_description(). |
|
852
|
|
|
* |
|
853
|
|
|
* @since 1.0.19 |
|
854
|
|
|
* @param string $value New description. |
|
855
|
|
|
*/ |
|
856
|
|
|
public function set_summary( $value ) { |
|
857
|
|
|
$this->set_description( $value ); |
|
858
|
|
|
} |
|
859
|
|
|
|
|
860
|
|
|
/** |
|
861
|
|
|
* Set the owner of the discount. |
|
862
|
|
|
* |
|
863
|
|
|
* @since 1.0.19 |
|
864
|
|
|
* @param int $value New author. |
|
865
|
|
|
*/ |
|
866
|
|
|
public function set_author( $value ) { |
|
867
|
|
|
$this->set_prop( 'author', (int) $value ); |
|
868
|
|
|
} |
|
869
|
|
|
|
|
870
|
|
|
/** |
|
871
|
|
|
* Sets the discount code. |
|
872
|
|
|
* |
|
873
|
|
|
* @since 1.0.19 |
|
874
|
|
|
* @param string $value New discount code. |
|
875
|
|
|
*/ |
|
876
|
|
|
public function set_code( $value ) { |
|
877
|
|
|
$code = sanitize_text_field( $value ); |
|
878
|
|
|
$this->set_prop( 'code', $code ); |
|
879
|
|
|
} |
|
880
|
|
|
|
|
881
|
|
|
/** |
|
882
|
|
|
* Alias of self::set_code(). |
|
883
|
|
|
* |
|
884
|
|
|
* @since 1.0.19 |
|
885
|
|
|
* @param string $value New discount code. |
|
886
|
|
|
*/ |
|
887
|
|
|
public function set_coupon_code( $value ) { |
|
888
|
|
|
$this->set_code( $value ); |
|
889
|
|
|
} |
|
890
|
|
|
|
|
891
|
|
|
/** |
|
892
|
|
|
* Alias of self::set_code(). |
|
893
|
|
|
* |
|
894
|
|
|
* @since 1.0.19 |
|
895
|
|
|
* @param string $value New discount code. |
|
896
|
|
|
*/ |
|
897
|
|
|
public function set_discount_code( $value ) { |
|
898
|
|
|
$this->set_code( $value ); |
|
899
|
|
|
} |
|
900
|
|
|
|
|
901
|
|
|
/** |
|
902
|
|
|
* Sets the discount amount. |
|
903
|
|
|
* |
|
904
|
|
|
* @since 1.0.19 |
|
905
|
|
|
* @param float $value New discount code. |
|
906
|
|
|
*/ |
|
907
|
|
|
public function set_amount( $value ) { |
|
908
|
|
|
$amount = floatval( wpinv_sanitize_amount( $value ) ); |
|
909
|
|
|
$this->set_prop( 'amount', $amount ); |
|
910
|
|
|
} |
|
911
|
|
|
|
|
912
|
|
|
/** |
|
913
|
|
|
* Sets the discount's start date. |
|
914
|
|
|
* |
|
915
|
|
|
* @since 1.0.19 |
|
916
|
|
|
* @param float $value New start date. |
|
917
|
|
|
*/ |
|
918
|
|
|
public function set_start( $value ) { |
|
919
|
|
|
$date = strtotime( $value ); |
|
920
|
|
|
|
|
921
|
|
|
if ( $date ) { |
|
922
|
|
|
$this->set_prop( 'start', date( 'Y-m-d H:i', $date ) ); |
|
923
|
|
|
return true; |
|
924
|
|
|
} |
|
925
|
|
|
|
|
926
|
|
|
$this->set_prop( 'start', '' ); |
|
927
|
|
|
|
|
928
|
|
|
return false; |
|
929
|
|
|
} |
|
930
|
|
|
|
|
931
|
|
|
/** |
|
932
|
|
|
* Alias of self::set_start(). |
|
933
|
|
|
* |
|
934
|
|
|
* @since 1.0.19 |
|
935
|
|
|
* @param string $value New start date. |
|
936
|
|
|
*/ |
|
937
|
|
|
public function set_start_date( $value ) { |
|
938
|
|
|
$this->set_start( $value ); |
|
|
|
|
|
|
939
|
|
|
} |
|
940
|
|
|
|
|
941
|
|
|
/** |
|
942
|
|
|
* Sets the discount's expiration date. |
|
943
|
|
|
* |
|
944
|
|
|
* @since 1.0.19 |
|
945
|
|
|
* @param float $value New expiration date. |
|
946
|
|
|
*/ |
|
947
|
|
|
public function set_expiration( $value ) { |
|
948
|
|
|
$date = strtotime( $value ); |
|
949
|
|
|
|
|
950
|
|
|
if ( $date ) { |
|
951
|
|
|
$this->set_prop( 'expiration', date( 'Y-m-d H:i', $date ) ); |
|
952
|
|
|
return true; |
|
953
|
|
|
} |
|
954
|
|
|
|
|
955
|
|
|
$this->set_prop( 'expiration', '' ); |
|
956
|
|
|
return false; |
|
957
|
|
|
} |
|
958
|
|
|
|
|
959
|
|
|
/** |
|
960
|
|
|
* Alias of self::set_expiration(). |
|
961
|
|
|
* |
|
962
|
|
|
* @since 1.0.19 |
|
963
|
|
|
* @param string $value New expiration date. |
|
964
|
|
|
*/ |
|
965
|
|
|
public function set_expiration_date( $value ) { |
|
966
|
|
|
$this->set_expiration( $value ); |
|
|
|
|
|
|
967
|
|
|
} |
|
968
|
|
|
|
|
969
|
|
|
/** |
|
970
|
|
|
* Alias of self::set_expiration(). |
|
971
|
|
|
* |
|
972
|
|
|
* @since 1.0.19 |
|
973
|
|
|
* @param string $value New expiration date. |
|
974
|
|
|
*/ |
|
975
|
|
|
public function set_end_date( $value ) { |
|
976
|
|
|
$this->set_expiration( $value ); |
|
|
|
|
|
|
977
|
|
|
} |
|
978
|
|
|
|
|
979
|
|
|
/** |
|
980
|
|
|
* Sets the discount type. |
|
981
|
|
|
* |
|
982
|
|
|
* @since 1.0.19 |
|
983
|
|
|
* @param string $value New discount type. |
|
984
|
|
|
*/ |
|
985
|
|
|
public function set_type( $value ) { |
|
986
|
|
|
if ( $value && array_key_exists( sanitize_text_field( $value ), wpinv_get_discount_types() ) ) { |
|
987
|
|
|
$this->set_prop( 'type', sanitize_text_field( $value ) ); |
|
988
|
|
|
} |
|
989
|
|
|
} |
|
990
|
|
|
|
|
991
|
|
|
/** |
|
992
|
|
|
* Sets the number of times a discount has been used. |
|
993
|
|
|
* |
|
994
|
|
|
* @since 1.0.19 |
|
995
|
|
|
* @param int $value usage count. |
|
996
|
|
|
*/ |
|
997
|
|
|
public function set_uses( $value ) { |
|
998
|
|
|
|
|
999
|
|
|
$value = (int) $value; |
|
1000
|
|
|
|
|
1001
|
|
|
if ( $value < 0 ) { |
|
1002
|
|
|
$value = 0; |
|
1003
|
|
|
} |
|
1004
|
|
|
|
|
1005
|
|
|
$this->set_prop( 'uses', (int) $value ); |
|
1006
|
|
|
} |
|
1007
|
|
|
|
|
1008
|
|
|
/** |
|
1009
|
|
|
* Sets the maximum number of times a discount can be used. |
|
1010
|
|
|
* |
|
1011
|
|
|
* @since 1.0.19 |
|
1012
|
|
|
* @param int $value maximum usage count. |
|
1013
|
|
|
*/ |
|
1014
|
|
|
public function set_max_uses( $value ) { |
|
1015
|
|
|
$this->set_prop( 'max_uses', absint( $value ) ); |
|
1016
|
|
|
} |
|
1017
|
|
|
|
|
1018
|
|
|
/** |
|
1019
|
|
|
* Sets if this is a single use discount or not. |
|
1020
|
|
|
* |
|
1021
|
|
|
* @since 1.0.19 |
|
1022
|
|
|
* @param int|bool $value is single use. |
|
1023
|
|
|
*/ |
|
1024
|
|
|
public function set_is_single_use( $value ) { |
|
1025
|
|
|
$this->set_prop( 'is_single_use', (bool) $value ); |
|
1026
|
|
|
} |
|
1027
|
|
|
|
|
1028
|
|
|
/** |
|
1029
|
|
|
* Sets the items that can be used with this discount. |
|
1030
|
|
|
* |
|
1031
|
|
|
* @since 1.0.19 |
|
1032
|
|
|
* @param array $value items. |
|
1033
|
|
|
*/ |
|
1034
|
|
|
public function set_items( $value ) { |
|
1035
|
|
|
$this->set_prop( 'items', array_filter( wp_parse_id_list( $value ) ) ); |
|
1036
|
|
|
} |
|
1037
|
|
|
|
|
1038
|
|
|
/** |
|
1039
|
|
|
* Alias for self::set_items(). |
|
1040
|
|
|
* |
|
1041
|
|
|
* @since 1.0.19 |
|
1042
|
|
|
* @param array $value items. |
|
1043
|
|
|
*/ |
|
1044
|
|
|
public function set_allowed_items( $value ) { |
|
1045
|
|
|
$this->set_items( $value ); |
|
1046
|
|
|
} |
|
1047
|
|
|
|
|
1048
|
|
|
/** |
|
1049
|
|
|
* Sets the items that can not be used with this discount. |
|
1050
|
|
|
* |
|
1051
|
|
|
* @since 1.0.19 |
|
1052
|
|
|
* @param array $value items. |
|
1053
|
|
|
*/ |
|
1054
|
|
|
public function set_excluded_items( $value ) { |
|
1055
|
|
|
$this->set_prop( 'excluded_items', array_filter( wp_parse_id_list( $value ) ) ); |
|
1056
|
|
|
} |
|
1057
|
|
|
|
|
1058
|
|
|
/** |
|
1059
|
|
|
* Sets the items that are required to be in the cart before using this discount. |
|
1060
|
|
|
* |
|
1061
|
|
|
* @since 1.0.19 |
|
1062
|
|
|
* @param array $value items. |
|
1063
|
|
|
*/ |
|
1064
|
|
|
public function set_required_items( $value ) { |
|
1065
|
|
|
$this->set_prop( 'required_items', array_filter( wp_parse_id_list( $value ) ) ); |
|
1066
|
|
|
} |
|
1067
|
|
|
|
|
1068
|
|
|
/** |
|
1069
|
|
|
* Sets if this is a recurring discounts or not. |
|
1070
|
|
|
* |
|
1071
|
|
|
* @since 1.0.19 |
|
1072
|
|
|
* @param int|bool $value is recurring. |
|
1073
|
|
|
*/ |
|
1074
|
|
|
public function set_is_recurring( $value ) { |
|
1075
|
|
|
$this->set_prop( 'is_recurring', (bool) $value ); |
|
1076
|
|
|
} |
|
1077
|
|
|
|
|
1078
|
|
|
/** |
|
1079
|
|
|
* Sets the minimum total that can not be used with this discount. |
|
1080
|
|
|
* |
|
1081
|
|
|
* @since 1.0.19 |
|
1082
|
|
|
* @param float $value minimum total. |
|
1083
|
|
|
*/ |
|
1084
|
|
|
public function set_min_total( $value ) { |
|
1085
|
|
|
$this->set_prop( 'min_total', (float) wpinv_sanitize_amount( $value ) ); |
|
1086
|
|
|
} |
|
1087
|
|
|
|
|
1088
|
|
|
/** |
|
1089
|
|
|
* Alias for self::set_min_total(). |
|
1090
|
|
|
* |
|
1091
|
|
|
* @since 1.0.19 |
|
1092
|
|
|
* @param float $value minimum total. |
|
1093
|
|
|
*/ |
|
1094
|
|
|
public function set_minimum_total( $value ) { |
|
1095
|
|
|
$this->set_min_total( $value ); |
|
1096
|
|
|
} |
|
1097
|
|
|
|
|
1098
|
|
|
/** |
|
1099
|
|
|
* Sets the maximum total that can not be used with this discount. |
|
1100
|
|
|
* |
|
1101
|
|
|
* @since 1.0.19 |
|
1102
|
|
|
* @param float $value maximum total. |
|
1103
|
|
|
*/ |
|
1104
|
|
|
public function set_max_total( $value ) { |
|
1105
|
|
|
$this->set_prop( 'max_total', (float) wpinv_sanitize_amount( $value ) ); |
|
1106
|
|
|
} |
|
1107
|
|
|
|
|
1108
|
|
|
/** |
|
1109
|
|
|
* Alias for self::set_max_total(). |
|
1110
|
|
|
* |
|
1111
|
|
|
* @since 1.0.19 |
|
1112
|
|
|
* @param float $value maximum total. |
|
1113
|
|
|
*/ |
|
1114
|
|
|
public function set_maximum_total( $value ) { |
|
1115
|
|
|
$this->set_max_total( $value ); |
|
1116
|
|
|
} |
|
1117
|
|
|
|
|
1118
|
|
|
/** |
|
1119
|
|
|
* @deprecated |
|
1120
|
|
|
*/ |
|
1121
|
|
|
public function refresh(){} |
|
1122
|
|
|
|
|
1123
|
|
|
/** |
|
1124
|
|
|
* @deprecated |
|
1125
|
|
|
* |
|
1126
|
|
|
*/ |
|
1127
|
|
|
public function update_status( $status = 'publish' ){ |
|
1128
|
|
|
|
|
1129
|
|
|
if ( $this->exists() && $this->get_status() != $status ) { |
|
1130
|
|
|
$this->set_status( $status ); |
|
1131
|
|
|
$this->save(); |
|
1132
|
|
|
} |
|
1133
|
|
|
|
|
1134
|
|
|
} |
|
1135
|
|
|
|
|
1136
|
|
|
/* |
|
1137
|
|
|
|-------------------------------------------------------------------------- |
|
1138
|
|
|
| Conditionals |
|
1139
|
|
|
|-------------------------------------------------------------------------- |
|
1140
|
|
|
| |
|
1141
|
|
|
| Checks if a condition is true or false. |
|
1142
|
|
|
| |
|
1143
|
|
|
*/ |
|
1144
|
|
|
|
|
1145
|
|
|
/** |
|
1146
|
|
|
* Checks whether a discount exists in the database or not |
|
1147
|
|
|
* |
|
1148
|
|
|
* @since 1.0.15 |
|
1149
|
|
|
*/ |
|
1150
|
|
|
public function exists(){ |
|
1151
|
|
|
$id = $this->get_id(); |
|
1152
|
|
|
return ! empty( $id ); |
|
1153
|
|
|
} |
|
1154
|
|
|
|
|
1155
|
|
|
/** |
|
1156
|
|
|
* Checks the discount type. |
|
1157
|
|
|
* |
|
1158
|
|
|
* |
|
1159
|
|
|
* @param string $type the discount type to check against |
|
1160
|
|
|
* @since 1.0.15 |
|
1161
|
|
|
* @return bool |
|
1162
|
|
|
*/ |
|
1163
|
|
|
public function is_type( $type ) { |
|
1164
|
|
|
return $this->get_type() == $type; |
|
1165
|
|
|
} |
|
1166
|
|
|
|
|
1167
|
|
|
/** |
|
1168
|
|
|
* Checks whether the discount is published or not |
|
1169
|
|
|
* |
|
1170
|
|
|
* @since 1.0.15 |
|
1171
|
|
|
* @return bool |
|
1172
|
|
|
*/ |
|
1173
|
|
|
public function is_active() { |
|
1174
|
|
|
return $this->get_status() == 'publish'; |
|
1175
|
|
|
} |
|
1176
|
|
|
|
|
1177
|
|
|
/** |
|
1178
|
|
|
* Checks whether the discount has max uses |
|
1179
|
|
|
* |
|
1180
|
|
|
* @since 1.0.15 |
|
1181
|
|
|
* @return bool |
|
1182
|
|
|
*/ |
|
1183
|
|
|
public function has_limit() { |
|
1184
|
|
|
$limit = $this->get_max_uses(); |
|
1185
|
|
|
return ! empty( $limit ); |
|
1186
|
|
|
} |
|
1187
|
|
|
|
|
1188
|
|
|
/** |
|
1189
|
|
|
* Checks whether the discount has ever been used. |
|
1190
|
|
|
* |
|
1191
|
|
|
* @since 1.0.15 |
|
1192
|
|
|
* @return bool |
|
1193
|
|
|
*/ |
|
1194
|
|
|
public function has_uses() { |
|
1195
|
|
|
return $this->get_uses() > 0; |
|
1196
|
|
|
} |
|
1197
|
|
|
|
|
1198
|
|
|
/** |
|
1199
|
|
|
* Checks whether the discount is has exided the usage limit or not |
|
1200
|
|
|
* |
|
1201
|
|
|
* @since 1.0.15 |
|
1202
|
|
|
* @return bool |
|
1203
|
|
|
*/ |
|
1204
|
|
|
public function has_exceeded_limit() { |
|
1205
|
|
|
|
|
1206
|
|
|
if ( ! $this->has_limit() || ! $this->has_uses() ) { |
|
1207
|
|
|
$exceeded = false ; |
|
1208
|
|
|
} else { |
|
1209
|
|
|
$exceeded = (int) $this->get_max_uses() <= $this->get_uses(); |
|
1210
|
|
|
} |
|
1211
|
|
|
|
|
1212
|
|
|
return apply_filters( 'wpinv_is_discount_maxed_out', $exceeded, $this->get_id(), $this, $this->get_code() ); |
|
1213
|
|
|
} |
|
1214
|
|
|
|
|
1215
|
|
|
/** |
|
1216
|
|
|
* Checks whether the discount has an expiration date. |
|
1217
|
|
|
* |
|
1218
|
|
|
* @since 1.0.15 |
|
1219
|
|
|
* @return bool |
|
1220
|
|
|
*/ |
|
1221
|
|
|
public function has_expiration_date() { |
|
1222
|
|
|
$date = $this->get_expiration_date(); |
|
1223
|
|
|
return ! empty( $date ); |
|
1224
|
|
|
} |
|
1225
|
|
|
|
|
1226
|
|
|
/** |
|
1227
|
|
|
* Checks if the discount is expired |
|
1228
|
|
|
* |
|
1229
|
|
|
* @since 1.0.15 |
|
1230
|
|
|
* @return bool |
|
1231
|
|
|
*/ |
|
1232
|
|
|
public function is_expired() { |
|
1233
|
|
|
$expired = $this->has_expiration_date() ? current_time( 'timestamp' ) > strtotime( $this->get_expiration_date() ) : false; |
|
1234
|
|
|
return apply_filters( 'wpinv_is_discount_expired', $expired, $this->get_id(), $this, $this->get_code() ); |
|
1235
|
|
|
} |
|
1236
|
|
|
|
|
1237
|
|
|
/** |
|
1238
|
|
|
* Checks whether the discount has a start date. |
|
1239
|
|
|
* |
|
1240
|
|
|
* @since 1.0.15 |
|
1241
|
|
|
* @return bool |
|
1242
|
|
|
*/ |
|
1243
|
|
|
public function has_start_date() { |
|
1244
|
|
|
$date = $this->get_start_date(); |
|
1245
|
|
|
return ! empty( $date ); |
|
1246
|
|
|
} |
|
1247
|
|
|
|
|
1248
|
|
|
/** |
|
1249
|
|
|
* Checks the discount start date. |
|
1250
|
|
|
* |
|
1251
|
|
|
* @since 1.0.15 |
|
1252
|
|
|
* @return bool |
|
1253
|
|
|
*/ |
|
1254
|
|
|
public function has_started() { |
|
1255
|
|
|
$started = $this->has_start_date() ? true : current_time( 'timestamp' ) > strtotime( $this->get_start_date() ); |
|
1256
|
|
|
return apply_filters( 'wpinv_is_discount_started', $started, $this->get_id(), $this, $this->get_code() ); |
|
1257
|
|
|
} |
|
1258
|
|
|
|
|
1259
|
|
|
/** |
|
1260
|
|
|
* Checks the discount has allowed items or not. |
|
1261
|
|
|
* |
|
1262
|
|
|
* @since 1.0.15 |
|
1263
|
|
|
* @return bool |
|
1264
|
|
|
*/ |
|
1265
|
|
|
public function has_allowed_items() { |
|
1266
|
|
|
$allowed_items = $this->get_allowed_items(); |
|
1267
|
|
|
return ! empty( $allowed_items ); |
|
1268
|
|
|
} |
|
1269
|
|
|
|
|
1270
|
|
|
/** |
|
1271
|
|
|
* Checks the discount has excluded items or not. |
|
1272
|
|
|
* |
|
1273
|
|
|
* @since 1.0.15 |
|
1274
|
|
|
* @return bool |
|
1275
|
|
|
*/ |
|
1276
|
|
|
public function has_excluded_items() { |
|
1277
|
|
|
$excluded_items = $this->get_excluded_items(); |
|
1278
|
|
|
return ! empty( $excluded_items ); |
|
1279
|
|
|
} |
|
1280
|
|
|
|
|
1281
|
|
|
/** |
|
1282
|
|
|
* Check if a discount is valid for a given item id. |
|
1283
|
|
|
* |
|
1284
|
|
|
* @param int|int[] $item_ids |
|
1285
|
|
|
* @since 1.0.15 |
|
1286
|
|
|
* @return boolean |
|
1287
|
|
|
*/ |
|
1288
|
|
|
public function is_valid_for_items( $item_ids ) { |
|
1289
|
|
|
|
|
1290
|
|
|
$item_ids = array_filter( wp_parse_id_list( $item_ids ) ); |
|
1291
|
|
|
$included = array_intersect( $item_ids, $this->get_allowed_items() ); |
|
1292
|
|
|
$excluded = array_intersect( $item_ids, $this->get_excluded_items() ); |
|
1293
|
|
|
|
|
1294
|
|
|
if ( $this->has_excluded_items() && ! empty( $excluded ) ) { |
|
1295
|
|
|
return false; |
|
1296
|
|
|
} |
|
1297
|
|
|
|
|
1298
|
|
|
if ( $this->has_allowed_items() && empty( $included ) ) { |
|
1299
|
|
|
return false; |
|
1300
|
|
|
} |
|
1301
|
|
|
|
|
1302
|
|
|
return true; |
|
1303
|
|
|
} |
|
1304
|
|
|
|
|
1305
|
|
|
/** |
|
1306
|
|
|
* Checks the discount has required items or not. |
|
1307
|
|
|
* |
|
1308
|
|
|
* @since 1.0.15 |
|
1309
|
|
|
* @return bool |
|
1310
|
|
|
*/ |
|
1311
|
|
|
public function has_required_items() { |
|
1312
|
|
|
$required_items = $this->get_required_items(); |
|
1313
|
|
|
return ! empty( $required_items ); |
|
1314
|
|
|
} |
|
1315
|
|
|
|
|
1316
|
|
|
/** |
|
1317
|
|
|
* Checks if the required items are met |
|
1318
|
|
|
* |
|
1319
|
|
|
* @param int|int[] $item_ids |
|
1320
|
|
|
* @since 1.0.15 |
|
1321
|
|
|
* @return boolean |
|
1322
|
|
|
*/ |
|
1323
|
|
|
public function is_required_items_met( $item_ids ) { |
|
1324
|
|
|
|
|
1325
|
|
|
if ( ! $this->has_required_items() ) { |
|
1326
|
|
|
return true; |
|
1327
|
|
|
} |
|
1328
|
|
|
|
|
1329
|
|
|
return ! array_diff( $this->get_required_items(), array_filter( wp_parse_id_list( $item_ids ) ) ); |
|
1330
|
|
|
} |
|
1331
|
|
|
|
|
1332
|
|
|
/** |
|
1333
|
|
|
* Check if a discount is valid for the given amount |
|
1334
|
|
|
* |
|
1335
|
|
|
* @param float $amount The amount to check against |
|
1336
|
|
|
* @since 1.0.15 |
|
1337
|
|
|
* @return boolean |
|
1338
|
|
|
*/ |
|
1339
|
|
|
public function is_valid_for_amount( $amount ) { |
|
1340
|
|
|
return $this->is_minimum_amount_met( $amount ) && $this->is_maximum_amount_met( $amount ); |
|
1341
|
|
|
} |
|
1342
|
|
|
|
|
1343
|
|
|
/** |
|
1344
|
|
|
* Checks if the minimum amount is set |
|
1345
|
|
|
* |
|
1346
|
|
|
* @since 1.0.15 |
|
1347
|
|
|
* @return boolean |
|
1348
|
|
|
*/ |
|
1349
|
|
|
public function has_minimum_amount() { |
|
1350
|
|
|
$minimum = $this->get_minimum_total(); |
|
1351
|
|
|
return ! empty( $minimum ); |
|
1352
|
|
|
} |
|
1353
|
|
|
|
|
1354
|
|
|
/** |
|
1355
|
|
|
* Checks if the minimum amount is met |
|
1356
|
|
|
* |
|
1357
|
|
|
* @param float $amount The amount to check against |
|
1358
|
|
|
* @since 1.0.15 |
|
1359
|
|
|
* @return boolean |
|
1360
|
|
|
*/ |
|
1361
|
|
|
public function is_minimum_amount_met( $amount ) { |
|
1362
|
|
|
$amount = floatval( wpinv_sanitize_amount( $amount ) ); |
|
1363
|
|
|
$min_met= ! ( $this->has_minimum_amount() && $amount < floatval( wpinv_sanitize_amount( $this->get_minimum_total() ) ) ); |
|
1364
|
|
|
return apply_filters( 'wpinv_is_discount_min_met', $min_met, $this->get_id(), $this, $this->get_code(), $amount ); |
|
1365
|
|
|
} |
|
1366
|
|
|
|
|
1367
|
|
|
/** |
|
1368
|
|
|
* Checks if the maximum amount is set |
|
1369
|
|
|
* |
|
1370
|
|
|
* @since 1.0.15 |
|
1371
|
|
|
* @return boolean |
|
1372
|
|
|
*/ |
|
1373
|
|
|
public function has_maximum_amount() { |
|
1374
|
|
|
$maximum = $this->get_maximum_total(); |
|
1375
|
|
|
return ! empty( $maximum ); |
|
1376
|
|
|
} |
|
1377
|
|
|
|
|
1378
|
|
|
/** |
|
1379
|
|
|
* Checks if the maximum amount is met |
|
1380
|
|
|
* |
|
1381
|
|
|
* @param float $amount The amount to check against |
|
1382
|
|
|
* @since 1.0.15 |
|
1383
|
|
|
* @return boolean |
|
1384
|
|
|
*/ |
|
1385
|
|
|
public function is_maximum_amount_met( $amount ) { |
|
1386
|
|
|
$amount = floatval( wpinv_sanitize_amount( $amount ) ); |
|
1387
|
|
|
$max_met= ! ( $this->has_maximum_amount() && $amount > floatval( wpinv_sanitize_amount( $this->get_maximum_total() ) ) ); |
|
1388
|
|
|
return apply_filters( 'wpinv_is_discount_max_met', $max_met, $this->get_id(), $this, $this->get_code(), $amount ); |
|
1389
|
|
|
} |
|
1390
|
|
|
|
|
1391
|
|
|
/** |
|
1392
|
|
|
* Checks if the discount is recurring. |
|
1393
|
|
|
* |
|
1394
|
|
|
* @since 1.0.15 |
|
1395
|
|
|
* @return boolean |
|
1396
|
|
|
*/ |
|
1397
|
|
|
public function is_recurring() { |
|
1398
|
|
|
$recurring = $this->get_is_recurring(); |
|
1399
|
|
|
return ! empty( $recurring ); |
|
1400
|
|
|
} |
|
1401
|
|
|
|
|
1402
|
|
|
/** |
|
1403
|
|
|
* Checks if the discount is single use. |
|
1404
|
|
|
* |
|
1405
|
|
|
* @since 1.0.15 |
|
1406
|
|
|
* @return boolean |
|
1407
|
|
|
*/ |
|
1408
|
|
|
public function is_single_use() { |
|
1409
|
|
|
$usage = $this->get_is_single_use(); |
|
1410
|
|
|
return ! empty( $usage ); |
|
1411
|
|
|
} |
|
1412
|
|
|
|
|
1413
|
|
|
/** |
|
1414
|
|
|
* Check if a discount is valid for the given user |
|
1415
|
|
|
* |
|
1416
|
|
|
* @param int|string $user |
|
1417
|
|
|
* @since 1.0.15 |
|
1418
|
|
|
* @return boolean |
|
1419
|
|
|
*/ |
|
1420
|
|
|
public function is_valid_for_user( $user ) { |
|
1421
|
|
|
|
|
1422
|
|
|
// Ensure that the discount is single use. |
|
1423
|
|
|
if ( empty( $user ) || ! $this->is_single_use() ) { |
|
1424
|
|
|
return true; |
|
1425
|
|
|
} |
|
1426
|
|
|
|
|
1427
|
|
|
// Prepare the user id. |
|
1428
|
|
|
$user_id = 0; |
|
1429
|
|
|
if ( is_numeric( $user ) ) { |
|
1430
|
|
|
$user_id = absint( $user ); |
|
1431
|
|
|
} else if ( is_email( $user ) && $user_data = get_user_by( 'email', $user ) ) { |
|
1432
|
|
|
$user_id = $user_data->ID; |
|
1433
|
|
|
} else if ( $user_data = get_user_by( 'login', $user ) ) { |
|
1434
|
|
|
$user_id = $user_data->ID; |
|
1435
|
|
|
} |
|
1436
|
|
|
|
|
1437
|
|
|
// Ensure that we have a user. |
|
1438
|
|
|
if ( empty( $user_id ) ) { |
|
1439
|
|
|
return true; |
|
1440
|
|
|
} |
|
1441
|
|
|
|
|
1442
|
|
|
// Get all payments with matching user id. |
|
1443
|
|
|
$payments = wpinv_get_invoices( array( 'user' => $user_id, 'limit' => false, 'paginate' => false ) ); |
|
1444
|
|
|
$code = strtolower( $this->get_code() ); |
|
1445
|
|
|
|
|
1446
|
|
|
// For each payment... |
|
1447
|
|
|
foreach ( $payments as $payment ) { |
|
1448
|
|
|
|
|
1449
|
|
|
// Only check for paid invoices. |
|
1450
|
|
|
if ( $payment->is_paid() && strtolower( $payment->get_discount_code() ) == $code ) { |
|
1451
|
|
|
return false; |
|
1452
|
|
|
} |
|
1453
|
|
|
|
|
1454
|
|
|
} |
|
1455
|
|
|
|
|
1456
|
|
|
return true; |
|
1457
|
|
|
} |
|
1458
|
|
|
|
|
1459
|
|
|
/** |
|
1460
|
|
|
* Deletes the discount from the database |
|
1461
|
|
|
* |
|
1462
|
|
|
* @since 1.0.15 |
|
1463
|
|
|
* @return boolean |
|
1464
|
|
|
*/ |
|
1465
|
|
|
public function remove() { |
|
1466
|
|
|
return $this->delete(); |
|
1467
|
|
|
} |
|
1468
|
|
|
|
|
1469
|
|
|
/** |
|
1470
|
|
|
* Increases a discount's usage. |
|
1471
|
|
|
* |
|
1472
|
|
|
* @since 1.0.15 |
|
1473
|
|
|
* @param int $by The number of usages to increas by. |
|
1474
|
|
|
* @return int |
|
1475
|
|
|
*/ |
|
1476
|
|
|
public function increase_usage( $by = 1 ) { |
|
1477
|
|
|
|
|
1478
|
|
|
// Abort if zero. |
|
1479
|
|
|
if ( empty( $by ) ) { |
|
1480
|
|
|
return; |
|
1481
|
|
|
} |
|
1482
|
|
|
|
|
1483
|
|
|
// Increase the usage. |
|
1484
|
|
|
$this->set_uses( $this->get_uses() + (int) $by ); |
|
1485
|
|
|
|
|
1486
|
|
|
// Save the discount. |
|
1487
|
|
|
$this->save(); |
|
1488
|
|
|
|
|
1489
|
|
|
// Fire relevant hooks. |
|
1490
|
|
|
if( (int) $by > 0 ) { |
|
1491
|
|
|
do_action( 'wpinv_discount_increase_use_count', $this->get_uses(), $this->get_id(), $this->get_code(), absint( $by ) ); |
|
1492
|
|
|
} else { |
|
1493
|
|
|
do_action( 'wpinv_discount_decrease_use_count', $this->get_uses(), $this->get_id(), $this->get_code(), absint( $by ) ); |
|
1494
|
|
|
} |
|
1495
|
|
|
|
|
1496
|
|
|
// Return the number of times the discount has been used. |
|
1497
|
|
|
return $this->get_uses(); |
|
1498
|
|
|
} |
|
1499
|
|
|
|
|
1500
|
|
|
/** |
|
1501
|
|
|
* Alias of self::__toString() |
|
1502
|
|
|
* |
|
1503
|
|
|
* @since 1.0.15 |
|
1504
|
|
|
* @return string|false |
|
1505
|
|
|
*/ |
|
1506
|
|
|
public function get_data_as_json() { |
|
1507
|
|
|
return $this->__toString(); |
|
1508
|
|
|
} |
|
1509
|
|
|
|
|
1510
|
|
|
/** |
|
1511
|
|
|
* Returns a discount's discounted amount. |
|
1512
|
|
|
* |
|
1513
|
|
|
* @since 1.0.15 |
|
1514
|
|
|
* @param float $amount |
|
1515
|
|
|
* @return float |
|
1516
|
|
|
*/ |
|
1517
|
|
|
public function get_discounted_amount( $amount ) { |
|
1518
|
|
|
|
|
1519
|
|
|
// Convert amount to float. |
|
1520
|
|
|
$amount = (float) $amount; |
|
1521
|
|
|
|
|
1522
|
|
|
// Get discount amount. |
|
1523
|
|
|
$discount_amount = $this->get_amount(); |
|
1524
|
|
|
|
|
1525
|
|
|
if ( empty( $discount_amount ) ) { |
|
1526
|
|
|
return 0; |
|
1527
|
|
|
} |
|
1528
|
|
|
|
|
1529
|
|
|
// Format the amount. |
|
1530
|
|
|
$discount_amount = floatval( wpinv_sanitize_amount( $discount_amount ) ); |
|
1531
|
|
|
|
|
1532
|
|
|
// If this is a percentage discount. |
|
1533
|
|
|
if ( $this->is_type( 'percent' ) ) { |
|
1534
|
|
|
$discount_amount = $amount * ( $discount_amount / 100 ); |
|
1535
|
|
|
} |
|
1536
|
|
|
|
|
1537
|
|
|
// Discount can not be less than zero... |
|
1538
|
|
|
if ( $discount_amount < 0 ) { |
|
1539
|
|
|
$discount_amount = 0; |
|
1540
|
|
|
} |
|
1541
|
|
|
|
|
1542
|
|
|
// ... or more than the amount. |
|
1543
|
|
|
if ( $discount_amount > $amount ) { |
|
1544
|
|
|
$discount_amount = $amount; |
|
1545
|
|
|
} |
|
1546
|
|
|
|
|
1547
|
|
|
return apply_filters( 'wpinv_discount_total_discount_amount', $discount_amount, $amount, $this ); |
|
1548
|
|
|
} |
|
1549
|
|
|
|
|
1550
|
|
|
} |
|
1551
|
|
|
|