-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathcoupons.class.php
More file actions
791 lines (658 loc) · 22.1 KB
/
coupons.class.php
File metadata and controls
791 lines (658 loc) · 22.1 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
<?php
/**
* uses coupons function, no parameters
* @return boolean if true, all items in the cart do use shipping
*/
function wpsc_uses_coupons() {
global $wpsc_coupons;
if( empty( $wpsc_coupons ) )
$wpsc_coupons = new wpsc_coupons();
if( is_object( $wpsc_coupons ) )
return $wpsc_coupons->uses_coupons();
return false;
}
function wpsc_coupons_error(){
global $wpsc_coupons;
if(isset($wpsc_coupons->errormsg) && $wpsc_coupons->errormsg == true){
return true;
}else{
return false;
}
}
/**
* Coupons class.
*
* Conditional coupons use an 'ALL' logic. Now admins can achieve an 'ANY' logic by adding multiple coupons.
*
* TODO: Implement 'ANY' logic of conditional coupons.
*
* @package wp-e-commerce
* @since 3.7
*/
class wpsc_coupons {
public $code;
public $value;
public $is_percentage;
public $conditions;
public $start_date;
public $active;
public $every_product ;
public $end_date;
public $use_once;
public $is_used;
public $discount;
//for error message
public $errormsg;
/**
* Coupons constractor
*
* Instantiate a coupons object with optional variable $code;
* If there are more than one coupon that matches $code check them all to find the first that is valid, if not are return false.
*
* @param string code (optional) the coupon code you would like to use.
* @return bool True if coupon code exists, False otherwise.
*/
function wpsc_coupons( $code = '' ){
global $wpdb;
if ( empty( $code ) ) {
return false;
}
$this->code = $code;
$coupons_data = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM `" . WPSC_TABLE_COUPON_CODES . "` WHERE coupon_code = %s", $code ) , ARRAY_A );
foreach( $coupons_data as $key => $coupon_data ) {
if ( !empty( $coupon_data ) ) {
$coupon_data = array_merge( array(
'value' => '',
'is-percentage' => '',
'condition' => '',
'is-used' => '',
'active' => '',
'use-once' => '',
'start' => '',
'expiry' => '',
'every_product' => ''
), $coupon_data );
$this->value = (float) $coupon_data['value'];
$this->is_percentage = $coupon_data['is-percentage'];
$this->conditions = unserialize($coupon_data['condition']);
$this->is_used = $coupon_data['is-used'];
$this->active = $coupon_data['active'];
$this->use_once = $coupon_data['use-once'];
$this->start_date = $coupon_data['start'];
$this->end_date = $coupon_data['expiry'];
$this->every_product = $coupon_data['every_product'];
$this->errormsg = false;
$valid = $this->validate_coupon();
$items = $this->get_eligible_items();
if ( empty( $items ) ) {
continue;
}
if ( $valid ) {
return true;
}
}
$this->errormsg = true;
wpsc_delete_customer_meta( 'coupon' );
return false;
}
}
/**
* Coupons validator
*
* Checks if the current coupon is valid to use (Expiry date, Active, Used).
*
* @return bool True if coupon is not expried, used and still active, False otherwise.
*/
function validate_coupon() {
$now = current_time( 'timestamp', true );
$valid = true;
$start_date = strtotime( $this->start_date );
$end_date = strtotime( $this->end_date );
if ( '1' != $this->active ) {
$valid = false;
}
if ( '1' == $this->use_once && '1' == $this->is_used ) {
$valid = false;
}
if ( $start_date && $now < $start_date ) {
$valid = false;
}
if ( $end_date && $now > $end_date ) {
$valid = false;
}
return apply_filters( 'wpsc_coupons_validate_coupon', $valid, $this );
}
/**
* Check whether the coupon has conditions
*
* @since 3.8.9
* @return boolean True if there are conditions
*/
public function has_conditions() {
return ! empty( $this->conditions );
}
/**
* Check if item's name matches condition
*
* @since 3.8.9
* @access private
* @param array $condition Condition arguments
* @param object $cart_item Cart item
* @return boolean
*/
public function _callback_condition_item_name( $condition, $cart_item ) {
$product_data = get_post( $cart_item->product_id );
switch( $condition['logic'] ) {
case 'equal': //Checks if the product name is exactly the same as the condition value
return $product_data->post_title == $condition['value'];
break;
case 'greater'://Checks if the product name is not the same as the condition value
return $product_data->post_title > $condition['value'];
break;
case 'less'://Checks if the product name is not the same as the condition value
return $product_data->post_title < $condition['value'];
break;
case 'contains'://Checks if the product name contains the condition value
return preg_match( "/(.*)" . preg_quote( $condition['value'], '/' ) . "(.*)/", $product_data->post_title );
break;
case 'category'://Checks if the product category is the condition value
$id = $product_data->ID;
if ( $product_data->post_parent )
$id = $product_data->post_parent;
$category_condition = $condition['value'];
if ( false !== strpos( $category_condition, ',' ) ) {
$category_condition = explode( ',', $condition['value'] );
$category_condition = array_map( 'trim', $cond );
}
return has_term( $category_condition, 'wpsc_product_category', $id );
break;
case 'not_contain'://Checks if the product name contains the condition value
return ! preg_match( "/(.*)" . preg_quote( $condition['value'], '/' ) . "(.*)/", $product_data->post_title );
break;
case 'begins'://Checks if the product name begins with condition value
return preg_match( "/^" . preg_quote( $condition['value'], '/' ) . "/", $product_data->post_title );
break;
case 'ends'://Checks if the product name ends with condition value
return preg_match( "/" . preg_quote( $c['value'], '/' ) . "$/", $product_data->post_title );
break;
}
return false;
}
/**
* Check whether item quantity matches condition
*
* @since 3.8.9
* @access private
* @param array $condition Condition arguments
* @param object $cart_item Cart item
* @return boolean
*/
public function _callback_condition_item_quantity( $condition, $cart_item ) {
$value = (int) $condition['value'];
switch( $condition['logic'] ) {
case 'equal': //Checks if the quantity of a product in the cart equals condition value
return $cart_item->quantity == $value;
break;
case 'greater'://Checks if the quantity of a product is greater than the condition value
return $cart_item->quantity > $value;
break;
case 'less'://Checks if the quantity of a product is less than the condition value
return $cart_item->quantity < $value;
break;
case 'contains'://Checks if the product name contains the condition value
return preg_match( "/(.*)" . $value . "(.*)/", $cart_item->quantity );
break;
case 'not_contain'://Checks if the product name contains the condition value
return ! preg_match( "/(.*)" . $value . "(.*)/",$cart_item->quantity );
break;
case 'begins'://Checks if the product name begins with condition value
return preg_match( "/^" . $value ."/", $cart_item->quantity );
break;
case 'ends'://Checks if the product name ends with condition value
return preg_match( "/" . $value . "$/",$product_obj->quantity );
break;
}
return false;
}
/**
* Check whether total quantity matches condition
*
* @since 3.8.9
* @access private
* @param array $condition Condition arguments
* @param object $cart_item Cart item
* @return boolean
*/
public function _callback_condition_total_quantity( $condition, $cart_item ) {
$total_quantity = wpsc_cart_item_count();
$value = (int) $condition['value'];
switch( $condition['logic'] ) {
case 'equal'://Checks if the quantity of products in the cart equals condition value
return $total_quantity == $value;
break;
case 'greater'://Checks if the quantity in the cart is greater than the condition value
return $total_quantity > $value;
break;
case 'less'://Checks if the quantity in the cart is less than the condition value
return $total_quantity < $value;
break;
}
return false;
}
/**
* Checks whether subtotal matches condition
*
* @since 3.8.9
* @access private
* @param array $condition Condition arguments
* @param object $cart_item Cart item
* @return
*/
public function _callback_condition_subtotal_amount( $condition, $cart_item ) {
global $wpsc_cart;
$subtotal = $wpsc_cart->calculate_subtotal();
$value = (float) $condition['value'];
switch( $condition['logic'] ) {
case 'equal'://Checks if the subtotal of products in the cart equals condition value
return $subtotal == $value;
break;
case 'greater'://Checks if the subtotal of the cart is greater than the condition value
return $subtotal > $value;
break;
case 'less'://Checks if the subtotal of the cart is less than the condition value
return $subtotal < $value;
break;
}
return false;
}
/**
* Filter out cart items that don't match coupon conditions
*
* @since 3.8.9
* @access private
* @param object $cart_item Cart item
* @return bool
*/
public function _filter_cart_item_conditions( $cart_item ) {
global $wpsc_cart;
$compare_logic = false;
foreach ( $this->conditions as $condition ) {
$callback = '_callback_condition_' . $condition['property'];
if ( is_callable( array( $this, $callback ) ) ) {
$result = $this->$callback( $condition, $cart_item );
} else {
/* This allows for a function outside of this class to override a custom condition. */
if ( function_exists( $callback ) ) {
$result = $callback( $condition, $cart_item );
} else {
/* This allows for a plugin to create a condition callback for the condition. Perk: doesn't have to follow $callback nomenclature. */
$result = apply_filters( 'wpsc_coupon_conditions_default_callback', false, $callback, $condition, $cart_item );
}
}
if ( ! $result ) {
switch ( $condition['operator'] ) {
case 'or':
$compare_logic = $compare_logic || apply_filters( 'wpsc_coupon_compare_logic', false, $condition, $cart_item );
break;
case 'and':
$compare_logic = $compare_logic && apply_filters( 'wpsc_coupon_compare_logic', false, $condition, $cart_item );
break;
default:
$compare_logic = apply_filters( 'wpsc_coupon_compare_logic', false, $condition, $cart_item );
}
} else {
switch ( $condition['operator'] ) {
case 'or':
$compare_logic = $compare_logic || $result;
break;
case 'and':
$compare_logic = $compare_logic && $result;
break;
default:
$compare_logic = $result;
}
}
}
return $compare_logic;
}
/**
* Get cart items that match coupon conditions
*
* @since 3.8.9
* @access private
* @return array Array containing eligible cart items
*/
public function get_eligible_items() {
global $wpsc_cart;
// cache product objects if we have a "item name" condition
if ( in_array( 'item_name', $this->conditions ) ) {
$ids = $wpsc_cart->get_items( array( 'fields' => 'product_id' ) );
get_posts( array(
'post_type' => 'wpsc-product',
'numberposts' => -1,
'post__in' => array( $ids ),
) );
}
// sort the items by total price so that we can use this in $this->calculate_discount_conditions()
$orderby = apply_filters( 'wpsc_coupon_select_item_orderby', 'unit_price' );
$order = apply_filters( 'wpsc_coupon_select_item_order' , 'ASC' );
$cart_items = $wpsc_cart->get_items( array( 'orderby' => $orderby, 'order' => $order ) );
$cart_items = array_filter( $cart_items, array( $this, '_filter_cart_item_conditions' ) );
return $cart_items;
}
/**
* Calculate the subtotal of the items passed in as argument
*
* @since 3.8.9
* @access private
* @param array $items Array of items
* @return float Subtotal
*/
private function calculate_subtotal( $items ) {
$total = 0;
foreach ( $items as $item ) {
$total += $item->total_price;
}
return $total;
}
/**
* Get the total quantity of the items passed in as argument
*
* @since 3.8.9
* @access private
* @param array $items Array of items
* @return float Subtotal
*/
private function get_total_quantity( $items ) {
$total = 0;
foreach ( $items as $item ) {
$total += $item->quantity;
}
return $total;
}
/**
* Calculate the discount amount, taking coupon conditions into consideration
*
* @since 3.8.9
* @access private
* @return float Discount amount
*/
private function calculate_discount_conditions() {
global $wpsc_cart;
// findout whether the cart meet the conditions
$items = $this->get_eligible_items();
if ( empty( $items ) )
return 0;
// if this is free shipping, return the total shipping regardless of whether "Apply on all
// products" is checked or not
if ( $this->is_free_shipping() )
return $this->calculate_free_shipping();
// if "Apply on all products" is checked, discount amount should be based on the total values
// of eligible cart items
if ( $this->applies_to_all_items() ) {
if ( $this->is_percentage() ) {
$subtotal = $this->calculate_subtotal( $items );
$discount = $this->value * $subtotal / 100;
} else {
$discount = $this->value * $this->get_total_quantity( $items );
}
return $discount;
}
// if "Apply on all products" is not checked and the coupon is percentage, the discount
// amount should be based on the eligible cart item with lowest unit price
if ( $this->is_percentage() ) {
$field = apply_filters( 'wpsc_coupon_select_item_field' , 'unit_price' );
$item = array_shift( $items );
return $item->$field * $this->value / 100;
}
// if "Apply on all products" is not checked and the coupon is a fixed value
// return the discount value
return $this->value;
}
/**
* Calculate discount amount without taking conditions into consideration
*
* @since 3.8.9
* @access private
* @return float Discount amount
*/
private function calculate_discount_without_conditions() {
global $wpsc_cart;
// if this is free shipping, return the total shipping regardless of whether "Apply on all
// products" is checked or not
if ( $this->is_free_shipping() )
return $this->calculate_free_shipping();
// if "Apply on all products" is checked, discount amount should be based on the overall
// cart
if ( $this->applies_to_all_items() ) {
if ( $this->is_percentage() ) {
$subtotal = $wpsc_cart->calculate_subtotal();
$discount = $this->value * $subtotal / 100;
} else {
$discount = $this->value * wpsc_cart_item_count();
}
return $discount;
}
// if "Apply on all products" is not checked and the coupon is percentage, the discount
// amount should be based on the cart item with lowest unit_price
if ( $this->is_percentage() ) {
$orderby = apply_filters( 'wpsc_coupon_select_item_orderby', 'unit_price' );
$order = apply_filters( 'wpsc_coupon_select_item_order' , 'ASC' );
$field = apply_filters( 'wpsc_coupon_select_item_field' , 'unit_price' );
$cart_items = $wpsc_cart->get_items( array( 'fields' => $field, 'orderby' => $orderby, 'order' => $order ) );
if ( empty( $cart_items ) )
return 0;
$item = array_shift( $cart_items );
return $item * $this->value / 100;
}
// if "Apply on all products" is not checked and the coupon is a fixed value
// return the discount value
return $this->value;
}
/**
* Check whether this coupon is a "Free shipping" coupon
*
* @since 3.8.9
* @return boolean
*/
public function is_free_shipping() {
return $this->is_percentage == '2';
}
/**
* Check whether this coupon is a "percentage" coupon
*
* @since 3.8.9
* @return boolean
*/
public function is_percentage() {
return $this->is_percentage == '1';
}
/**
* Check whether this coupon is a fixed amount coupon
*
* @since 3.8.9
* @return boolean
*/
public function is_fixed_amount() {
return ! $this->is_free_shipping() && ! $this->is_percentage();
}
/**
* Check whether this coupon can be applied to all items
*
* @since 3.8.9
* @return boolean
*/
public function applies_to_all_items() {
return $this->every_product == 1;
}
/**
* Calculate the free shipping discount amount
* @return float
*/
private function calculate_free_shipping() {
global $wpsc_cart;
return $wpsc_cart->calculate_total_shipping();
}
/**
* Calculate the discount amount
*
* @since 3.8.9
* @return float
*/
public function calculate_discount() {
global $wpdb, $wpsc_cart;
$wpsc_cart->clear_cache();
if ( $this->has_conditions() )
return $this->calculate_discount_conditions();
return $this->calculate_discount_without_conditions();
}
/**
* Comparing logic with the product information
*
* Checks if the product matchs the logic
*
* @return bool True if all conditions are matched, False otherwise.
*/
function compare_logic($c, $product_obj) {
global $wpdb;
if ( 'item_name' == $c['property'] ) {
$product_data = $wpdb->get_results( "SELECT * FROM " . $wpdb->posts . " WHERE id='{$product_obj->product_id}'" );
$product_data = $product_data[0];
switch( $c['logic'] ) {
case 'equal': //Checks if the product name is exactly the same as the condition value
if ( $product_data->post_title == $c['value'] )
return true;
break;
case 'greater'://Checks if the product name is not the same as the condition value
if ( $product_data->post_title > $c['value'] )
return true;
break;
case 'less'://Checks if the product name is not the same as the condition value
if ( $product_data->post_title < $c['value'] )
return true;
break;
case 'contains'://Checks if the product name contains the condition value
preg_match( "/(.*)" . preg_quote( $c['value'], '/' ) . "(.*)/", $product_data->post_title, $match );
if ( ! empty( $match ) )
return true;
break;
case 'category'://Checks if the product category is the condition value
if ( $product_data->post_parent ) {
$categories = wpsc_get_product_terms( $product_data->post_parent, 'wpsc_product_category' );
} else {
$categories = wpsc_get_product_terms( $product_data->ID, 'wpsc_product_category' );
}
foreach ( $categories as $cat ) {
if ( strtolower( $cat->name ) == strtolower( $c['value'] ) )
return true;
}
break;
case 'not_contain'://Checks if the product name contains the condition value
preg_match( "/(.*)" . preg_quote( $c['value'], '/' ) . "(.*)/", $product_data->post_title, $match );
if ( empty( $match ) )
return true;
break;
case 'begins'://Checks if the product name begins with condition value
preg_match( "/^" . preg_quote( $c['value'], '/' ) . "/", $product_data->post_title, $match );
if ( ! empty( $match ) )
return true;
break;
case 'ends'://Checks if the product name ends with condition value
preg_match( "/" . preg_quote( $c['value'], '/' ) . "$/", $product_data->post_title, $match );
if ( ! empty( $match ) )
return true;
break;
default:
return false;
}
} else if ( 'item_quantity' == $c['property'] ) {
switch( $c['logic'] ) {
case 'equal'://Checks if the quantity of a product in the cart equals condition value
if ( $product_obj->quantity == (int) $c['value'] )
return true;
break;
case 'greater'://Checks if the quantity of a product is greater than the condition value
if ( $product_obj->quantity > $c['value'] )
return true;
break;
case 'less'://Checks if the quantity of a product is less than the condition value
if ( $product_obj->quantity < $c['value'] )
return true;
break;
case 'contains'://Checks if the product name contains the condition value
preg_match( "/(.*)" . $c['value'] . "(.*)/", $product_obj->quantity, $match );
if ( ! empty( $match ) )
return true;
break;
case 'not_contain'://Checks if the product name contains the condition value
preg_match("/(.*)".$c['value']."(.*)/",$product_obj->quantity, $match );
if ( empty( $match ) )
return true;
break;
case 'begins'://Checks if the product name begins with condition value
preg_match("/^".$c['value']."/", $product_obj->quantity, $match );
if ( ! empty( $match ) )
return true;
break;
case 'ends'://Checks if the product name ends with condition value
preg_match( "/" . $c['value'] . "$/", $product_obj->quantity, $match );
if ( ! empty( $match ) )
return true;
break;
default:
return false;
}
} else if ($c['property'] == 'total_quantity'){
$total_quantity = wpsc_cart_item_count();
switch($c['logic']) {
case 'equal'://Checks if the quantity of products in the cart equals condition value
if ($total_quantity == $c['value'])
return true;
break;
case 'greater'://Checks if the quantity in the cart is greater than the condition value
if ($total_quantity > $c['value'])
return true;
break;
case 'less'://Checks if the quantity in the cart is less than the condition value
if ($total_quantity < $c['value'])
return true;
break;
default:
return false;
}
} else if ($c['property'] == 'subtotal_amount'){
$subtotal = wpsc_cart_total(false);
switch($c['logic']) {
case 'equal'://Checks if the subtotal of products in the cart equals condition value
if ($subtotal == $c['value'])
return true;
break;
case 'greater'://Checks if the subtotal of the cart is greater than the condition value
if ($subtotal > $c['value'])
return true;
break;
case 'less'://Checks if the subtotal of the cart is less than the condition value
if ($subtotal < $c['value']){
return true;
}else{
return false;
}
break;
default:
return false;
}
} else {
return apply_filters( 'wpsc_coupon_compare_logic', false, $c, $product_obj );
}
}
/**
* uses coupons function, no parameters
* @return boolean if true, items in the cart do use coupons
*/
function uses_coupons() {
global $wpdb;
$num_active_coupons = $wpdb->get_var("SELECT COUNT(id) as c FROM `".WPSC_TABLE_COUPON_CODES."` WHERE active='1'");
return ( $num_active_coupons > 0 );
}
}
?>