forked from woocommerce/woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-wc-cart.php
1936 lines (1674 loc) · 57.2 KB
/
class-wc-cart.php
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* WooCommerce cart
*
* The WooCommerce cart class stores cart data and active coupons as well as handling customer sessions and some cart related urls.
* The cart class also has a price calculation function which calls upon other classes to calculate totals.
*
* @version 2.1.0
* @package WooCommerce/Classes
* @category Class
* @author WooThemes
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
include_once( WC_ABSPATH . 'includes/legacy/class-wc-legacy-cart.php' );
include_once( WC_ABSPATH . 'includes/class-wc-cart-session.php' );
/**
* WC_Cart class.
*/
class WC_Cart extends WC_Legacy_Cart {
/**
* Contains an array of cart items.
*
* @var array
*/
public $cart_contents = array();
/**
* Contains an array of removed cart items so we can restore them if needed.
*
* @var array
*/
public $removed_cart_contents = array();
/**
* Contains an array of coupon codes applied to the cart.
*
* @var array
*/
public $applied_coupons = array();
/**
* Are prices in the cart displayed inc or excl tax.
*
* @var string
*/
public $tax_display_cart;
/**
* This stores the chosen shipping methods for the cart item packages.
*
* @var array
*/
protected $shipping_methods;
/**
* Total defaults used to reset.
*
* @var array
*/
protected $default_totals = array(
'subtotal' => 0,
'subtotal_tax' => 0,
'shipping_total' => 0,
'shipping_tax' => 0,
'shipping_taxes' => array(),
'discount_total' => 0,
'discount_tax' => 0,
'cart_contents_total' => 0,
'cart_contents_tax' => 0,
'cart_contents_taxes' => array(),
'fee_total' => 0,
'fee_tax' => 0,
'fee_taxes' => array(),
'total' => 0,
'total_tax' => 0,
);
/**
* Store calculated totals.
*
* @var array
*/
protected $totals = array();
/**
* Reference to the cart session handling class.
*
* @var WC_Cart_Session
*/
protected $session;
/**
* Reference to the cart fees API class.
*
* @var WC_Cart_Fees
*/
protected $fees_api;
/**
* Constructor for the cart class. Loads options and hooks in the init method.
*/
public function __construct() {
$this->session = new WC_Cart_Session( $this );
$this->fees_api = new WC_Cart_Fees( $this );
$this->tax_display_cart = get_option( 'woocommerce_tax_display_cart' );
add_action( 'woocommerce_add_to_cart', array( $this, 'calculate_totals' ), 20, 0 );
add_action( 'woocommerce_applied_coupon', array( $this, 'calculate_totals' ), 20, 0 );
add_action( 'woocommerce_cart_item_removed', array( $this, 'calculate_totals' ), 20, 0 );
add_action( 'woocommerce_cart_item_restored', array( $this, 'calculate_totals' ), 20, 0 );
add_action( 'woocommerce_check_cart_items', array( $this, 'check_cart_items' ), 1 );
add_action( 'woocommerce_check_cart_items', array( $this, 'check_cart_coupons' ), 1 );
add_action( 'woocommerce_after_checkout_validation', array( $this, 'check_customer_coupons' ), 1 );
}
/*
|--------------------------------------------------------------------------
| Getters.
|--------------------------------------------------------------------------
|
| Methods to retrieve class properties and avoid direct access.
*/
/**
* Gets cart contents.
*
* @since 3.2.0
* @return array of applied coupons
*/
public function get_cart_contents() {
return (array) $this->cart_contents;
}
/**
* Return items removed from the cart.
*
* @since 3.2.0
* @return array
*/
public function get_removed_cart_contents() {
return (array) $this->removed_cart_contents;
}
/**
* Gets the array of applied coupon codes.
*
* @return array of applied coupons
*/
public function get_applied_coupons() {
return (array) $this->applied_coupons;
}
/**
* Return all calculated coupon totals.
*
* @since 3.2.0
* @return array
*/
public function get_coupon_discount_totals() {
return (array) $this->coupon_discount_totals;
}
/**
* Return all calculated coupon tax totals.
*
* @since 3.2.0
* @return array
*/
public function get_coupon_discount_tax_totals() {
return (array) $this->coupon_discount_tax_totals;
}
/**
* Return all calculated totals.
*
* @since 3.2.0
* @return array
*/
public function get_totals() {
return empty( $this->totals ) ? $this->default_totals : $this->totals;
}
/**
* Get a total.
*
* @since 3.2.0
* @param string $key Key of element in $totals array.
* @return mixed
*/
protected function get_totals_var( $key ) {
return isset( $this->totals[ $key ] ) ? $this->totals[ $key ] : $this->default_totals[ $key ];
}
/**
* Get subtotal.
*
* @since 3.2.0
* @return float
*/
public function get_subtotal() {
return apply_filters( 'woocommerce_cart_' . __METHOD__, $this->get_totals_var( 'subtotal' ) );
}
/**
* Get subtotal.
*
* @since 3.2.0
* @return float
*/
public function get_subtotal_tax() {
return apply_filters( 'woocommerce_cart_' . __METHOD__, $this->get_totals_var( 'subtotal_tax' ) );
}
/**
* Get discount_total.
*
* @since 3.2.0
* @return float
*/
public function get_discount_total() {
return apply_filters( 'woocommerce_cart_' . __METHOD__, $this->get_totals_var( 'discount_total' ) );
}
/**
* Get discount_tax.
*
* @since 3.2.0
* @return float
*/
public function get_discount_tax() {
return apply_filters( 'woocommerce_cart_' . __METHOD__, $this->get_totals_var( 'discount_tax' ) );
}
/**
* Get shipping_total.
*
* @since 3.2.0
* @return float
*/
public function get_shipping_total() {
return apply_filters( 'woocommerce_cart_' . __METHOD__, $this->get_totals_var( 'shipping_total' ) );
}
/**
* Get shipping_tax.
*
* @since 3.2.0
* @return float
*/
public function get_shipping_tax() {
return apply_filters( 'woocommerce_cart_' . __METHOD__, $this->get_totals_var( 'shipping_tax' ) );
}
/**
* Gets cart total. This is the total of items in the cart, but after discounts. Subtotal is before discounts.
*
* @since 3.2.0
* @return float
*/
public function get_cart_contents_total() {
return apply_filters( 'woocommerce_cart_' . __METHOD__, $this->get_totals_var( 'cart_contents_total' ) );
}
/**
* Gets cart tax amount.
*
* @since 3.2.0
* @return float
*/
public function get_cart_contents_tax() {
return apply_filters( 'woocommerce_cart_' . __METHOD__, $this->get_totals_var( 'cart_contents_tax' ) );
}
/**
* Gets cart total after calculation.
*
* @since 3.2.0
* @param string $context If the context is view, the value will be formatted for display. This keeps it compatible with pre-3.2 versions.
* @return float
*/
public function get_total( $context = 'view' ) {
$total = apply_filters( 'woocommerce_cart_' . __METHOD__, $this->get_totals_var( 'total' ) );
return 'view' === $context ? apply_filters( 'woocommerce_cart_total', wc_price( $total ) ) : $total;
}
/**
* Get total tax amount.
*
* @since 3.2.0
* @return float
*/
public function get_total_tax() {
return apply_filters( 'woocommerce_cart_' . __METHOD__, $this->get_totals_var( 'total_tax' ) );
}
/**
* Get total fee amount.
*
* @since 3.2.0
* @return float
*/
public function get_fee_total() {
return apply_filters( 'woocommerce_cart_' . __METHOD__, $this->get_totals_var( 'fee_total' ) );
}
/**
* Get total fee tax amount.
*
* @since 3.2.0
* @return float
*/
public function get_fee_tax() {
return apply_filters( 'woocommerce_cart_' . __METHOD__, $this->get_totals_var( 'fee_tax' ) );
}
/**
* Get taxes.
*
* @since 3.2.0
*/
public function get_shipping_taxes() {
return apply_filters( 'woocommerce_cart_' . __METHOD__, $this->get_totals_var( 'shipping_taxes' ) );
}
/**
* Get taxes.
*
* @since 3.2.0
*/
public function get_cart_contents_taxes() {
return apply_filters( 'woocommerce_cart_' . __METHOD__, $this->get_totals_var( 'cart_contents_taxes' ) );
}
/**
* Get taxes.
*
* @since 3.2.0
*/
public function get_fee_taxes() {
return apply_filters( 'woocommerce_cart_' . __METHOD__, $this->get_totals_var( 'fee_taxes' ) );
}
/*
|--------------------------------------------------------------------------
| Setters.
|--------------------------------------------------------------------------
|
| Methods to set class properties and avoid direct access.
*/
/**
* Sets the contents of the cart.
*
* @param array $value Cart array.
*/
public function set_cart_contents( $value ) {
$this->cart_contents = (array) $value;
}
/**
* Set items removed from the cart.
*
* @since 3.2.0
* @param array $value Item array.
*/
public function set_removed_cart_contents( $value = array() ) {
$this->removed_cart_contents = (array) $value;
}
/**
* Sets the array of applied coupon codes.
*
* @param array $value List of applied coupon codes.
*/
public function set_applied_coupons( $value = array() ) {
$this->applied_coupons = (array) $value;
}
/**
* Return all calculated coupon totals.
*
* @since 3.2.0
* @param array $value Value to set.
*/
public function set_coupon_discount_totals( $value = array() ) {
$this->coupon_discount_totals = (array) $value;
}
/**
* Return all calculated coupon tax totals.
*
* @since 3.2.0
* @param array $value Value to set.
*/
public function set_coupon_discount_tax_totals( $value = array() ) {
$this->coupon_discount_tax_totals = (array) $value;
}
/**
* Set all calculated totals.
*
* @since 3.2.0
* @param array $value Value to set.
*/
public function set_totals( $value = array() ) {
$this->totals = wp_parse_args( $value, $this->default_totals );
}
/**
* Set subtotal.
*
* @since 3.2.0
* @param string $value Value to set.
*/
public function set_subtotal( $value ) {
$this->totals['subtotal'] = wc_format_decimal( $value );
}
/**
* Set subtotal.
*
* @since 3.2.0
* @param string $value Value to set.
*/
public function set_subtotal_tax( $value ) {
$this->totals['subtotal_tax'] = wc_round_tax_total( $value );
}
/**
* Set discount_total.
*
* @since 3.2.0
* @param string $value Value to set.
*/
public function set_discount_total( $value ) {
$this->totals['discount_total'] = wc_cart_round_discount( $value, wc_get_price_decimals() );
}
/**
* Set discount_tax.
*
* @since 3.2.0
* @param string $value Value to set.
*/
public function set_discount_tax( $value ) {
$this->totals['discount_tax'] = wc_format_decimal( $value );
}
/**
* Set shipping_total.
*
* @since 3.2.0
* @param string $value Value to set.
*/
public function set_shipping_total( $value ) {
$this->totals['shipping_total'] = wc_format_decimal( $value );
}
/**
* Set shipping_tax.
*
* @since 3.2.0
* @param string $value Value to set.
*/
public function set_shipping_tax( $value ) {
$this->totals['shipping_tax'] = wc_round_tax_total( $value );
}
/**
* Set cart_contents_total.
*
* @since 3.2.0
* @param string $value Value to set.
*/
public function set_cart_contents_total( $value ) {
$this->totals['cart_contents_total'] = wc_format_decimal( $value );
}
/**
* Set cart tax amount.
*
* @since 3.2.0
* @param string $value Value to set.
*/
public function set_cart_contents_tax( $value ) {
$this->totals['cart_contents_tax'] = wc_round_tax_total( $value );
}
/**
* Set cart total.
*
* @since 3.2.0
* @param string $value Value to set.
*/
public function set_total( $value ) {
$this->totals['total'] = wc_format_decimal( $value );
}
/**
* Set total tax amount.
*
* @since 3.2.0
* @param string $value Value to set.
*/
public function set_total_tax( $value ) {
$this->totals['total_tax'] = wc_round_tax_total( $value );
}
/**
* Set fee amount.
*
* @since 3.2.0
* @param string $value Value to set.
*/
public function set_fee_total( $value ) {
$this->totals['fee_total'] = wc_format_decimal( $value );
}
/**
* Set fee tax.
*
* @since 3.2.0
* @param string $value Value to set.
*/
public function set_fee_tax( $value ) {
$this->totals['fee_tax'] = wc_round_tax_total( $value );
}
/**
* Set taxes.
*
* @since 3.2.0
* @param array $value Tax values.
*/
public function set_shipping_taxes( $value ) {
$this->totals['shipping_taxes'] = (array) $value;
}
/**
* Set taxes.
*
* @since 3.2.0
* @param array $value Tax values.
*/
public function set_cart_contents_taxes( $value ) {
$this->totals['cart_contents_taxes'] = (array) $value;
}
/**
* Set taxes.
*
* @since 3.2.0
* @param array $value Tax values.
*/
public function set_fee_taxes( $value ) {
$this->totals['fee_taxes'] = (array) $value;
}
/*
|--------------------------------------------------------------------------
| Helper methods.
|--------------------------------------------------------------------------
*/
/**
* Returns the cart and shipping taxes, merged.
*
* @return array merged taxes
*/
public function get_taxes() {
return apply_filters( 'woocommerce_cart_get_taxes', wc_array_merge_recursive_numeric( $this->get_shipping_taxes(), $this->get_cart_contents_taxes(), $this->get_fee_taxes() ), $this );
}
/**
* Returns the contents of the cart in an array.
*
* @return array contents of the cart
*/
public function get_cart() {
if ( ! did_action( 'wp_loaded' ) ) {
wc_doing_it_wrong( __FUNCTION__, __( 'Get cart should not be called before the wp_loaded action.', 'woocommerce' ), '2.3' );
}
if ( ! did_action( 'woocommerce_cart_loaded_from_session' ) ) {
$this->session->get_cart_from_session();
}
return array_filter( $this->get_cart_contents() );
}
/**
* Returns a specific item in the cart.
*
* @param string $item_key Cart item key.
* @return array Item data
*/
public function get_cart_item( $item_key ) {
return isset( $this->cart_contents[ $item_key ] ) ? $this->cart_contents[ $item_key ] : array();
}
/**
* Checks if the cart is empty.
*
* @return bool
*/
public function is_empty() {
return 0 === count( $this->get_cart() );
}
/**
* Empties the cart and optionally the persistent cart too.
*
* @param bool $clear_persistent_cart Should the persistant cart be cleared too. Defaults to true.
*/
public function empty_cart( $clear_persistent_cart = true ) {
$this->cart_contents = array();
$this->removed_cart_contents = array();
$this->shipping_methods = array();
$this->coupon_discount_totals = array();
$this->coupon_discount_tax_totals = array();
$this->applied_coupons = array();
$this->totals = $this->default_totals;
if ( $clear_persistent_cart ) {
$this->session->persistent_cart_destroy();
}
do_action( 'woocommerce_cart_emptied' );
}
/**
* Get number of items in the cart.
*
* @return int
*/
public function get_cart_contents_count() {
return apply_filters( 'woocommerce_cart_contents_count', array_sum( wp_list_pluck( $this->get_cart(), 'quantity' ) ) );
}
/**
* Get weight of items in the cart.
*
* @since 2.5.0
* @return int
*/
public function get_cart_contents_weight() {
$weight = 0;
foreach ( $this->get_cart() as $cart_item_key => $values ) {
$weight += (float) $values['data']->get_weight() * $values['quantity'];
}
return apply_filters( 'woocommerce_cart_contents_weight', $weight );
}
/**
* Get cart items quantities - merged so we can do accurate stock checks on items across multiple lines.
*
* @return array
*/
public function get_cart_item_quantities() {
$quantities = array();
foreach ( $this->get_cart() as $cart_item_key => $values ) {
$product = $values['data'];
$quantities[ $product->get_stock_managed_by_id() ] = isset( $quantities[ $product->get_stock_managed_by_id() ] ) ? $quantities[ $product->get_stock_managed_by_id() ] + $values['quantity'] : $values['quantity'];
}
return $quantities;
}
/**
* Check all cart items for errors.
*/
public function check_cart_items() {
$return = true;
$result = $this->check_cart_item_validity();
if ( is_wp_error( $result ) ) {
wc_add_notice( $result->get_error_message(), 'error' );
$return = false;
}
$result = $this->check_cart_item_stock();
if ( is_wp_error( $result ) ) {
wc_add_notice( $result->get_error_message(), 'error' );
$return = false;
}
return $return;
}
/**
* Check cart coupons for errors.
*/
public function check_cart_coupons() {
foreach ( $this->get_applied_coupons() as $code ) {
$coupon = new WC_Coupon( $code );
if ( ! $coupon->is_valid() ) {
$coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_INVALID_REMOVED );
$this->remove_coupon( $code );
}
}
}
/**
* Looks through cart items and checks the posts are not trashed or deleted.
*
* @return bool|WP_Error
*/
public function check_cart_item_validity() {
$return = true;
foreach ( $this->get_cart() as $cart_item_key => $values ) {
$product = $values['data'];
if ( ! $product || ! $product->exists() || 'trash' === $product->get_status() ) {
$this->set_quantity( $cart_item_key, 0 );
$return = new WP_Error( 'invalid', __( 'An item which is no longer available was removed from your cart.', 'woocommerce' ) );
}
}
return $return;
}
/**
* Looks through the cart to check each item is in stock. If not, add an error.
*
* @return bool|WP_Error
*/
public function check_cart_item_stock() {
global $wpdb;
$error = new WP_Error();
$product_qty_in_cart = $this->get_cart_item_quantities();
foreach ( $this->get_cart() as $cart_item_key => $values ) {
$product = $values['data'];
/**
* Check stock based on stock-status.
*/
if ( ! $product->is_in_stock() ) {
/* translators: %s: product name */
$error->add( 'out-of-stock', sprintf( __( 'Sorry, "%s" is not in stock. Please edit your cart and try again. We apologize for any inconvenience caused.', 'woocommerce' ), $product->get_name() ) );
return $error;
}
if ( ! $product->managing_stock() ) {
continue;
}
/**
* Check stock based on all items in the cart.
*/
if ( ! $product->has_enough_stock( $product_qty_in_cart[ $product->get_stock_managed_by_id() ] ) ) {
/* translators: 1: product name 2: quantity in stock */
$error->add( 'out-of-stock', sprintf( __( 'Sorry, we do not have enough "%1$s" in stock to fulfill your order (%2$s in stock). Please edit your cart and try again. We apologize for any inconvenience caused.', 'woocommerce' ), $product->get_name(), wc_format_stock_quantity_for_display( $product->get_stock_quantity(), $product ) ) );
return $error;
}
/**
* Finally consider any held stock, from pending orders.
*/
if ( get_option( 'woocommerce_hold_stock_minutes' ) > 0 && ! $product->backorders_allowed() ) {
$order_id = isset( WC()->session->order_awaiting_payment ) ? absint( WC()->session->order_awaiting_payment ) : 0;
$held_stock = $wpdb->get_var(
$wpdb->prepare( "
SELECT SUM( order_item_meta.meta_value ) AS held_qty
FROM {$wpdb->posts} AS posts
LEFT JOIN {$wpdb->prefix}woocommerce_order_items as order_items ON posts.ID = order_items.order_id
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta2 ON order_items.order_item_id = order_item_meta2.order_item_id
WHERE order_item_meta.meta_key = '_qty'
AND order_item_meta2.meta_key = %s AND order_item_meta2.meta_value = %d
AND posts.post_type IN ( '" . implode( "','", wc_get_order_types() ) . "' )
AND posts.post_status = 'wc-pending'
AND posts.ID != %d;",
'variation' === get_post_type( $product->get_stock_managed_by_id() ) ? '_variation_id' : '_product_id',
$product->get_stock_managed_by_id(),
$order_id
)
);
if ( $product->get_stock_quantity() < ( $held_stock + $product_qty_in_cart[ $product->get_stock_managed_by_id() ] ) ) {
/* translators: 1: product name 2: minutes */
$error->add( 'out-of-stock', sprintf( __( 'Sorry, we do not have enough "%1$s" in stock to fulfill your order right now. Please try again in %2$d minutes or edit your cart and try again. We apologize for any inconvenience caused.', 'woocommerce' ), $product->get_name(), get_option( 'woocommerce_hold_stock_minutes' ) ) );
return $error;
}
}
}
return true;
}
/**
* Gets and formats a list of cart item data + variations for display on the frontend.
*
* @param array $cart_item Cart item object.
* @param bool $flat Should the data be returned flat or in a list.
* @return string
*/
public function get_item_data( $cart_item, $flat = false ) {
$item_data = array();
// Variation values are shown only if they are not found in the title as of 3.0.
// This is because variation titles display the attributes.
if ( $cart_item['data']->is_type( 'variation' ) && is_array( $cart_item['variation'] ) ) {
foreach ( $cart_item['variation'] as $name => $value ) {
$taxonomy = wc_attribute_taxonomy_name( str_replace( 'attribute_pa_', '', urldecode( $name ) ) );
if ( taxonomy_exists( $taxonomy ) ) {
// If this is a term slug, get the term's nice name.
$term = get_term_by( 'slug', $value, $taxonomy );
if ( ! is_wp_error( $term ) && $term && $term->name ) {
$value = $term->name;
}
$label = wc_attribute_label( $taxonomy );
} else {
// If this is a custom option slug, get the options name.
$value = apply_filters( 'woocommerce_variation_option_name', $value );
$label = wc_attribute_label( str_replace( 'attribute_', '', $name ), $cart_item['data'] );
}
// Check the nicename against the title.
if ( '' === $value || wc_is_attribute_in_product_name( $value, $cart_item['data']->get_name() ) ) {
continue;
}
$item_data[] = array(
'key' => $label,
'value' => $value,
);
}
}
// Filter item data to allow 3rd parties to add more to the array.
$item_data = apply_filters( 'woocommerce_get_item_data', $item_data, $cart_item );
// Format item data ready to display.
foreach ( $item_data as $key => $data ) {
// Set hidden to true to not display meta on cart.
if ( ! empty( $data['hidden'] ) ) {
unset( $item_data[ $key ] );
continue;
}
$item_data[ $key ]['key'] = ! empty( $data['key'] ) ? $data['key'] : $data['name'];
$item_data[ $key ]['display'] = ! empty( $data['display'] ) ? $data['display'] : $data['value'];
}
// Output flat or in list format.
if ( count( $item_data ) > 0 ) {
ob_start();
if ( $flat ) {
foreach ( $item_data as $data ) {
echo esc_html( $data['key'] ) . ': ' . wp_kses_post( $data['display'] ) . "\n";
}
} else {
wc_get_template( 'cart/cart-item-data.php', array( 'item_data' => $item_data ) );
}
return ob_get_clean();
}
return '';
}
/**
* Gets cross sells based on the items in the cart.
*
* @return array cross_sells (item ids)
*/
public function get_cross_sells() {
$cross_sells = array();
$in_cart = array();
if ( ! $this->is_empty() ) {
foreach ( $this->get_cart() as $cart_item_key => $values ) {
if ( $values['quantity'] > 0 ) {
$cross_sells = array_merge( $values['data']->get_cross_sell_ids(), $cross_sells );
$in_cart[] = $values['product_id'];
}
}
}
$cross_sells = array_diff( $cross_sells, $in_cart );
return apply_filters( 'woocommerce_cart_crosssell_ids', wp_parse_id_list( $cross_sells ), $this );
}
/**
* Gets the url to remove an item from the cart.
*
* @param string $cart_item_key contains the id of the cart item.
* @return string url to page
*/
public function get_remove_url( $cart_item_key ) {
$cart_page_url = wc_get_page_permalink( 'cart' );
return apply_filters( 'woocommerce_get_remove_url', $cart_page_url ? wp_nonce_url( add_query_arg( 'remove_item', $cart_item_key, $cart_page_url ), 'woocommerce-cart' ) : '' );
}
/**
* Gets the url to re-add an item into the cart.
*
* @param string $cart_item_key Cart item key to undo.
* @return string url to page
*/
public function get_undo_url( $cart_item_key ) {
$cart_page_url = wc_get_page_permalink( 'cart' );
$query_args = array(
'undo_item' => $cart_item_key,
);
return apply_filters( 'woocommerce_get_undo_url', $cart_page_url ? wp_nonce_url( add_query_arg( $query_args, $cart_page_url ), 'woocommerce-cart' ) : '', $cart_item_key );
}
/**
* Get taxes, merged by code, formatted ready for output.
*
* @return array
*/
public function get_tax_totals() {
$taxes = $this->get_taxes();
$tax_totals = array();
foreach ( $taxes as $key => $tax ) {
$code = WC_Tax::get_rate_code( $key );
if ( $code || $key === apply_filters( 'woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated' ) ) {
if ( ! isset( $tax_totals[ $code ] ) ) {
$tax_totals[ $code ] = new stdClass();
$tax_totals[ $code ]->amount = 0;
}
$tax_totals[ $code ]->tax_rate_id = $key;
$tax_totals[ $code ]->is_compound = WC_Tax::is_compound( $key );
$tax_totals[ $code ]->label = WC_Tax::get_rate_label( $key );
$tax_totals[ $code ]->amount += wc_round_tax_total( $tax );
$tax_totals[ $code ]->formatted_amount = wc_price( wc_round_tax_total( $tax_totals[ $code ]->amount ) );
}
}
if ( apply_filters( 'woocommerce_cart_hide_zero_taxes', true ) ) {
$amounts = array_filter( wp_list_pluck( $tax_totals, 'amount' ) );
$tax_totals = array_intersect_key( $tax_totals, $amounts );
}
return apply_filters( 'woocommerce_cart_tax_totals', $tax_totals, $this );
}
/**
* Get all tax classes for items in the cart.
*
* @return array
*/
public function get_cart_item_tax_classes() {
$found_tax_classes = array();
foreach ( WC()->cart->get_cart() as $item ) {
if ( $item['data'] && ( $item['data']->is_taxable() || $item['data']->is_shipping_taxable() ) ) {
$found_tax_classes[] = $item['data']->get_tax_class();
}
}
return array_unique( $found_tax_classes );
}
/**
* Determines the value that the customer spent and the subtotal
* displayed, used for things like coupon validation.
*
* Since the coupon lines are displayed based on the TAX DISPLAY value
* of cart, this is used to determine the spend.
*
* If cart totals are shown including tax, use the subtotal.
* If cart totals are shown excluding tax, use the subtotal ex tax
* (tax is shown after coupons).
*
* @since 2.6.0
* @return string
*/
public function get_displayed_subtotal() {
return 'incl' === $this->tax_display_cart ? $this->get_subtotal() + $this->get_subtotal_tax() : $this->get_subtotal();
}
/**
* Check if product is in the cart and return cart item key.
*
* Cart item key will be unique based on the item and its properties, such as variations.
*
* @param mixed $cart_id id of product to find in the cart.
* @return string cart item key
*/
public function find_product_in_cart( $cart_id = false ) {
if ( false !== $cart_id ) {
if ( is_array( $this->cart_contents ) && isset( $this->cart_contents[ $cart_id ] ) ) {
return $cart_id;