class-wc-cart.php
77.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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* 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.
*
* @class WC_Cart
* @version 2.1.0
* @package WooCommerce/Classes
* @category Class
* @author WooThemes
*/
class WC_Cart {
/** @var array Contains an array of cart items. */
public $cart_contents = array();
/** @var array Contains an array of removed cart items. */
public $removed_cart_contents = array();
/** @var array Contains an array of coupon codes applied to the cart. */
public $applied_coupons = array();
/** @var array Contains an array of coupon code discounts after they have been applied. */
public $coupon_discount_amounts = array();
/** @var array Contains an array of coupon code discount taxes. Used for tax incl pricing. */
public $coupon_discount_tax_amounts = array();
/** @var array Contains an array of coupon usage counts after they have been applied. */
public $coupon_applied_count = array();
/** @var array Array of coupons */
public $coupons = array();
/** @var float The total cost of the cart items. */
public $cart_contents_total;
/** @var float Cart grand total. */
public $total;
/** @var float Cart subtotal. */
public $subtotal;
/** @var float Cart subtotal without tax. */
public $subtotal_ex_tax;
/** @var float Total cart tax. */
public $tax_total;
/** @var array An array of taxes/tax rates for the cart. */
public $taxes;
/** @var array An array of taxes/tax rates for the shipping. */
public $shipping_taxes;
/** @var float Discount amount before tax */
public $discount_cart;
/** @var float Discounted tax amount. Used predominantly for displaying tax inclusive prices correctly */
public $discount_cart_tax;
/** @var float Total for additional fees. */
public $fee_total;
/** @var float Shipping cost. */
public $shipping_total;
/** @var float Shipping tax. */
public $shipping_tax_total;
/** @var array cart_session_data. Array of data the cart calculates and stores in the session with defaults */
public $cart_session_data = array(
'cart_contents_total' => 0,
'total' => 0,
'subtotal' => 0,
'subtotal_ex_tax' => 0,
'tax_total' => 0,
'taxes' => array(),
'shipping_taxes' => array(),
'discount_cart' => 0,
'discount_cart_tax' => 0,
'shipping_total' => 0,
'shipping_tax_total' => 0,
'coupon_discount_amounts' => array(),
'coupon_discount_tax_amounts' => array(),
'fee_total' => 0,
'fees' => array()
);
/**
* An array of fees.
*
* @var array
*/
public $fees = array();
/**
* Constructor for the cart class. Loads options and hooks in the init method.
*/
public function __construct() {
add_action( 'wp_loaded', array( $this, 'init' ) ); // Get cart after WP and plugins are loaded.
add_action( 'wp', array( $this, 'maybe_set_cart_cookies' ), 99 ); // Set cookies
add_action( 'shutdown', array( $this, 'maybe_set_cart_cookies' ), 0 ); // Set cookies before shutdown and ob flushing
add_action( 'woocommerce_add_to_cart', array( $this, 'calculate_totals' ), 20, 0 );
add_action( 'woocommerce_applied_coupon', array( $this, 'calculate_totals' ), 20, 0 );
}
/**
* Auto-load in-accessible properties on demand.
*
* @param mixed $key
* @return mixed
*/
public function __get( $key ) {
switch ( $key ) {
case 'prices_include_tax' :
return wc_prices_include_tax();
break;
case 'round_at_subtotal' :
return 'yes' === get_option( 'woocommerce_tax_round_at_subtotal' );
break;
case 'tax_display_cart' :
return get_option( 'woocommerce_tax_display_cart' );
break;
case 'dp' :
return wc_get_price_decimals();
break;
case 'display_totals_ex_tax' :
case 'display_cart_ex_tax' :
return $this->tax_display_cart === 'excl';
break;
case 'cart_contents_weight' :
return $this->get_cart_contents_weight();
break;
case 'cart_contents_count' :
return $this->get_cart_contents_count();
break;
case 'tax' :
_deprecated_argument( 'WC_Cart->tax', '2.3', 'Use WC_Tax:: directly' );
$this->tax = new WC_Tax();
return $this->tax;
case 'discount_total':
_deprecated_argument( 'WC_Cart->discount_total', '2.3', 'After tax coupons are no longer supported. For more information see: https://woocommerce.wordpress.com/2014/12/upcoming-coupon-changes-in-woocommerce-2-3/' );
return 0;
}
}
/**
* Loads the cart data from the PHP session during WordPress init and hooks in other methods.
*/
public function init() {
$this->get_cart_from_session();
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 );
}
/**
* Will set cart cookies if needed, once, during WP hook.
*/
public function maybe_set_cart_cookies() {
if ( ! headers_sent() && did_action( 'wp_loaded' ) ) {
if ( ! $this->is_empty() ) {
$this->set_cart_cookies( true );
} elseif ( isset( $_COOKIE['woocommerce_items_in_cart'] ) ) {
$this->set_cart_cookies( false );
}
}
}
/**
* Set cart hash cookie and items in cart.
*
* @access private
* @param bool $set (default: true)
*/
private function set_cart_cookies( $set = true ) {
if ( $set ) {
wc_setcookie( 'woocommerce_items_in_cart', 1 );
wc_setcookie( 'woocommerce_cart_hash', md5( json_encode( $this->get_cart_for_session() ) ) );
} elseif ( isset( $_COOKIE['woocommerce_items_in_cart'] ) ) {
wc_setcookie( 'woocommerce_items_in_cart', 0, time() - HOUR_IN_SECONDS );
wc_setcookie( 'woocommerce_cart_hash', '', time() - HOUR_IN_SECONDS );
}
do_action( 'woocommerce_set_cart_cookies', $set );
}
/*-----------------------------------------------------------------------------------*/
/* Cart Session Handling */
/*-----------------------------------------------------------------------------------*/
/**
* Get the cart data from the PHP session and store it in class variables.
*/
public function get_cart_from_session() {
// Load cart session data from session
foreach ( $this->cart_session_data as $key => $default ) {
$this->$key = WC()->session->get( $key, $default );
}
$update_cart_session = false;
$this->removed_cart_contents = array_filter( WC()->session->get( 'removed_cart_contents', array() ) );
$this->applied_coupons = array_filter( WC()->session->get( 'applied_coupons', array() ) );
/**
* Load the cart object. This defaults to the persistent cart if null.
*/
$cart = WC()->session->get( 'cart', null );
if ( is_null( $cart ) && ( $saved_cart = get_user_meta( get_current_user_id(), '_woocommerce_persistent_cart', true ) ) ) {
$cart = $saved_cart['cart'];
$update_cart_session = true;
} elseif ( is_null( $cart ) ) {
$cart = array();
}
if ( is_array( $cart ) ) {
// Prime meta cache to reduce future queries
update_meta_cache( 'post', wp_list_pluck( $cart, 'product_id' ) );
foreach ( $cart as $key => $values ) {
$_product = wc_get_product( $values['variation_id'] ? $values['variation_id'] : $values['product_id'] );
if ( ! empty( $_product ) && $_product->exists() && $values['quantity'] > 0 ) {
if ( ! $_product->is_purchasable() ) {
// Flag to indicate the stored cart should be update
$update_cart_session = true;
wc_add_notice( sprintf( __( '%s has been removed from your cart because it can no longer be purchased. Please contact us if you need assistance.', 'woocommerce' ), $_product->get_title() ), 'error' );
do_action( 'woocommerce_remove_cart_item_from_session', $key, $values );
} else {
// Put session data into array. Run through filter so other plugins can load their own session data
$session_data = array_merge( $values, array( 'data' => $_product ) );
$this->cart_contents[ $key ] = apply_filters( 'woocommerce_get_cart_item_from_session', $session_data, $values, $key );
}
}
}
}
// Trigger action
do_action( 'woocommerce_cart_loaded_from_session', $this );
if ( $update_cart_session ) {
WC()->session->cart = $this->get_cart_for_session();
}
// Queue re-calc if subtotal is not set
if ( ( ! $this->subtotal && ! $this->is_empty() ) || $update_cart_session ) {
$this->calculate_totals();
}
}
/**
* Sets the php session data for the cart and coupons.
*/
public function set_session() {
// Set cart and coupon session data
$cart_session = $this->get_cart_for_session();
WC()->session->set( 'cart', $cart_session );
WC()->session->set( 'applied_coupons', $this->applied_coupons );
WC()->session->set( 'coupon_discount_amounts', $this->coupon_discount_amounts );
WC()->session->set( 'coupon_discount_tax_amounts', $this->coupon_discount_tax_amounts );
WC()->session->set( 'removed_cart_contents', $this->removed_cart_contents );
foreach ( $this->cart_session_data as $key => $default ) {
WC()->session->set( $key, $this->$key );
}
if ( get_current_user_id() ) {
$this->persistent_cart_update();
}
do_action( 'woocommerce_cart_updated' );
}
/**
* Empties the cart and optionally the persistent cart too.
*
* @param bool $clear_persistent_cart (default: true)
*/
public function empty_cart( $clear_persistent_cart = true ) {
$this->cart_contents = array();
$this->reset( true );
unset( WC()->session->order_awaiting_payment, WC()->session->applied_coupons, WC()->session->coupon_discount_amounts, WC()->session->coupon_discount_tax_amounts, WC()->session->cart );
if ( $clear_persistent_cart && get_current_user_id() ) {
$this->persistent_cart_destroy();
}
do_action( 'woocommerce_cart_emptied' );
}
/*-----------------------------------------------------------------------------------*/
/* Persistent cart handling */
/*-----------------------------------------------------------------------------------*/
/**
* Save the persistent cart when the cart is updated.
*/
public function persistent_cart_update() {
update_user_meta( get_current_user_id(), '_woocommerce_persistent_cart', array(
'cart' => WC()->session->get( 'cart' )
) );
}
/**
* Delete the persistent cart permanently.
*/
public function persistent_cart_destroy() {
delete_user_meta( get_current_user_id(), '_woocommerce_persistent_cart' );
}
/*-----------------------------------------------------------------------------------*/
/* Cart Data Functions */
/*-----------------------------------------------------------------------------------*/
/**
* Coupons enabled function. Filterable.
*
* @deprecated 2.5.0 in favor to wc_coupons_enabled()
*
* @return bool
*/
public function coupons_enabled() {
return wc_coupons_enabled();
}
/**
* 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 += $values['data']->get_weight() * $values['quantity'];
}
return apply_filters( 'woocommerce_cart_contents_weight', $weight );
}
/**
* Checks if the cart is empty.
*
* @return bool
*/
public function is_empty() {
return 0 === sizeof( $this->get_cart() );
}
/**
* Check all cart items for errors.
*/
public function check_cart_items() {
// Result
$return = true;
// Check cart item validity
$result = $this->check_cart_item_validity();
if ( is_wp_error( $result ) ) {
wc_add_notice( $result->get_error_message(), 'error' );
$return = false;
}
// Check item stock
$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->applied_coupons as $code ) {
$coupon = new WC_Coupon( $code );
if ( ! $coupon->is_valid() ) {
// Error message
$coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_INVALID_REMOVED );
// Remove the coupon
$this->remove_coupon( $code );
// Flag totals for refresh
WC()->session->set( 'refresh_totals', true );
}
}
}
/**
* 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'];
if ( $_product->is_type( 'variation' ) && true === $_product->managing_stock() ) {
// Variation has stock levels defined so its handled individually
$quantities[ $values['variation_id'] ] = isset( $quantities[ $values['variation_id'] ] ) ? $quantities[ $values['variation_id'] ] + $values['quantity'] : $values['quantity'];
} else {
$quantities[ $values['product_id'] ] = isset( $quantities[ $values['product_id'] ] ) ? $quantities[ $values['product_id'] ] + $values['quantity'] : $values['quantity'];
}
}
return $quantities;
}
/**
* 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->post->post_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();
// First stock check loop
foreach ( $this->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
/**
* Check stock based on stock-status.
*/
if ( ! $_product->is_in_stock() ) {
$error->add( 'out-of-stock', sprintf(__( 'Sorry, "%s" is not in stock. Please edit your cart and try again. We apologise for any inconvenience caused.', 'woocommerce' ), $_product->get_title() ) );
return $error;
}
if ( ! $_product->managing_stock() ) {
continue;
}
$check_qty = $_product->is_type( 'variation' ) && true === $_product->managing_stock() ? $product_qty_in_cart[ $values['variation_id'] ] : $product_qty_in_cart[ $values['product_id'] ];
/**
* Check stock based on all items in the cart.
*/
if ( ! $_product->has_enough_stock( $check_qty ) ) {
$error->add( 'out-of-stock', sprintf(__( 'Sorry, we do not have enough "%s" in stock to fulfill your order (%s in stock). Please edit your cart and try again. We apologise for any inconvenience caused.', 'woocommerce' ), $_product->get_title(), $_product->get_stock_quantity() ) );
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;",
$_product->is_type( 'variation' ) && true === $_product->managing_stock() ? '_variation_id' : '_product_id',
$_product->is_type( 'variation' ) && true === $_product->managing_stock() ? $values['variation_id'] : $values['product_id'],
$order_id
)
);
$not_enough_stock = false;
if ( $_product->is_type( 'variation' ) && 'parent' === $_product->managing_stock() && $_product->parent->get_stock_quantity() < ( $held_stock + $check_qty ) ) {
$not_enough_stock = true;
} elseif ( $_product->get_stock_quantity() < ( $held_stock + $check_qty ) ) {
$not_enough_stock = true;
}
if ( $not_enough_stock ) {
$error->add( 'out-of-stock', sprintf(__( 'Sorry, we do not have enough "%s" in stock to fulfill your order right now. Please try again in %d minutes or edit your cart and try again. We apologise for any inconvenience caused.', 'woocommerce' ), $_product->get_title(), 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
* @param bool $flat (default: false)
* @return string
*/
public function get_item_data( $cart_item, $flat = false ) {
$item_data = array();
// Variation data
if ( ! empty( $cart_item['data']->variation_id ) && is_array( $cart_item['variation'] ) ) {
foreach ( $cart_item['variation'] as $name => $value ) {
if ( '' === $value )
continue;
$taxonomy = wc_attribute_taxonomy_name( str_replace( 'attribute_pa_', '', urldecode( $name ) ) );
// If this is a term slug, get the term's nice name
if ( taxonomy_exists( $taxonomy ) ) {
$term = get_term_by( 'slug', $value, $taxonomy );
if ( ! is_wp_error( $term ) && $term && $term->name ) {
$value = $term->name;
}
$label = wc_attribute_label( $taxonomy );
// If this is a custom option slug, get the options name
} else {
$value = apply_filters( 'woocommerce_variation_option_name', $value );
$product_attributes = $cart_item['data']->get_attributes();
if ( isset( $product_attributes[ str_replace( 'attribute_', '', $name ) ] ) ) {
$label = wc_attribute_label( $product_attributes[ str_replace( 'attribute_', '', $name ) ]['name'] );
} else {
$label = $name;
}
}
$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 ( sizeof( $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_sells(), $cross_sells );
$in_cart[] = $values['product_id'];
}
}
}
$cross_sells = array_diff( $cross_sells, $in_cart );
return $cross_sells;
}
/**
* Gets the url to the cart page.
*
* @deprecated 2.5.0 in favor to wc_get_cart_url()
*
* @return string url to page
*/
public function get_cart_url() {
return wc_get_cart_url();
}
/**
* Gets the url to the checkout page.
*
* @deprecated 2.5.0 in favor to wc_get_checkout_url()
*
* @return string url to page
*/
public function get_checkout_url() {
return wc_get_checkout_url();
}
/**
* 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
* @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 );
}
/**
* Returns the contents of the cart in an array.
*
* @return array contents of the cart
*/
public function get_cart() {
if ( ! did_action( 'wp_loaded' ) ) {
_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->get_cart_from_session();
}
return array_filter( (array) $this->cart_contents );
}
/**
* Returns the contents of the cart in an array without the 'data' element.
*
* @return array contents of the cart
*/
public function get_cart_for_session() {
$cart_session = array();
if ( $this->get_cart() ) {
foreach ( $this->get_cart() as $key => $values ) {
$cart_session[ $key ] = $values;
unset( $cart_session[ $key ]['data'] ); // Unset product object
}
}
return $cart_session;
}
/**
* 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 ) {
if ( isset( $this->cart_contents[ $item_key ] ) ) {
return $this->cart_contents[ $item_key ];
}
return array();
}
/**
* Returns the cart and shipping taxes, merged.
*
* @return array merged taxes
*/
public function get_taxes() {
$taxes = array();
// Merge
foreach ( array_keys( $this->taxes + $this->shipping_taxes ) as $key ) {
$taxes[ $key ] = ( isset( $this->shipping_taxes[ $key ] ) ? $this->shipping_taxes[ $key ] : 0 ) + ( isset( $this->taxes[ $key ] ) ? $this->taxes[ $key ] : 0 );
}
return apply_filters( 'woocommerce_cart_get_taxes', $taxes, $this );
}
/**
* 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 ) {
$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() {
if ( 'incl' === $this->tax_display_cart ) {
return wc_format_decimal( $this->subtotal );
} elseif ( 'excl' === $this->tax_display_cart ) {
return wc_format_decimal( $this->subtotal_ex_tax );
}
}
/*-----------------------------------------------------------------------------------*/
/* Add to cart handling */
/*-----------------------------------------------------------------------------------*/
/**
* 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 id of product to find in the cart
* @return string cart item key
*/
public function find_product_in_cart( $cart_id = false ) {
if ( $cart_id !== false ) {
if ( is_array( $this->cart_contents ) ) {
foreach ( $this->cart_contents as $cart_item_key => $cart_item ) {
if ( $cart_item_key == $cart_id ) {
return $cart_item_key;
}
}
}
}
return '';
}
/**
* Generate a unique ID for the cart item being added.
*
* @param int $product_id - id of the product the key is being generated for
* @param int $variation_id of the product the key is being generated for
* @param array $variation data for the cart item
* @param array $cart_item_data other cart item data passed which affects this items uniqueness in the cart
* @return string cart item key
*/
public function generate_cart_id( $product_id, $variation_id = 0, $variation = array(), $cart_item_data = array() ) {
$id_parts = array( $product_id );
if ( $variation_id && 0 != $variation_id ) {
$id_parts[] = $variation_id;
}
if ( is_array( $variation ) && ! empty( $variation ) ) {
$variation_key = '';
foreach ( $variation as $key => $value ) {
$variation_key .= trim( $key ) . trim( $value );
}
$id_parts[] = $variation_key;
}
if ( is_array( $cart_item_data ) && ! empty( $cart_item_data ) ) {
$cart_item_data_key = '';
foreach ( $cart_item_data as $key => $value ) {
if ( is_array( $value ) || is_object( $value ) ) {
$value = http_build_query( $value );
}
$cart_item_data_key .= trim( $key ) . trim( $value );
}
$id_parts[] = $cart_item_data_key;
}
return apply_filters( 'woocommerce_cart_id', md5( implode( '_', $id_parts ) ), $product_id, $variation_id, $variation, $cart_item_data );
}
/**
* Add a product to the cart.
*
* @param int $product_id contains the id of the product to add to the cart
* @param int $quantity contains the quantity of the item to add
* @param int $variation_id
* @param array $variation attribute values
* @param array $cart_item_data extra cart item data we want to pass into the item
* @return string|bool $cart_item_key
*/
public function add_to_cart( $product_id = 0, $quantity = 1, $variation_id = 0, $variation = array(), $cart_item_data = array() ) {
// Wrap in try catch so plugins can throw an exception to prevent adding to cart
try {
$product_id = absint( $product_id );
$variation_id = absint( $variation_id );
// Ensure we don't add a variation to the cart directly by variation ID
if ( 'product_variation' == get_post_type( $product_id ) ) {
$variation_id = $product_id;
$product_id = wp_get_post_parent_id( $variation_id );
}
// Get the product
$product_data = wc_get_product( $variation_id ? $variation_id : $product_id );
// Sanity check
if ( $quantity <= 0 || ! $product_data || 'trash' === $product_data->post->post_status ) {
throw new Exception();
}
// Load cart item data - may be added by other plugins
$cart_item_data = (array) apply_filters( 'woocommerce_add_cart_item_data', $cart_item_data, $product_id, $variation_id );
// Generate a ID based on product ID, variation ID, variation data, and other cart item data
$cart_id = $this->generate_cart_id( $product_id, $variation_id, $variation, $cart_item_data );
// Find the cart item key in the existing cart
$cart_item_key = $this->find_product_in_cart( $cart_id );
// Force quantity to 1 if sold individually and check for existing item in cart
if ( $product_data->is_sold_individually() ) {
$quantity = apply_filters( 'woocommerce_add_to_cart_sold_individually_quantity', 1, $quantity, $product_id, $variation_id, $cart_item_data );
$in_cart_quantity = $cart_item_key ? $this->cart_contents[ $cart_item_key ]['quantity'] : 0;
if ( $in_cart_quantity > 0 ) {
throw new Exception( sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', wc_get_cart_url(), __( 'View Cart', 'woocommerce' ), sprintf( __( 'You cannot add another "%s" to your cart.', 'woocommerce' ), $product_data->get_title() ) ) );
}
}
// Check product is_purchasable
if ( ! $product_data->is_purchasable() ) {
throw new Exception( __( 'Sorry, this product cannot be purchased.', 'woocommerce' ) );
}
// Stock check - only check if we're managing stock and backorders are not allowed
if ( ! $product_data->is_in_stock() ) {
throw new Exception( sprintf( __( 'You cannot add "%s" to the cart because the product is out of stock.', 'woocommerce' ), $product_data->get_title() ) );
}
if ( ! $product_data->has_enough_stock( $quantity ) ) {
throw new Exception( sprintf(__( 'You cannot add that amount of "%s" to the cart because there is not enough stock (%s remaining).', 'woocommerce' ), $product_data->get_title(), $product_data->get_stock_quantity() ) );
}
// Stock check - this time accounting for whats already in-cart
if ( $managing_stock = $product_data->managing_stock() ) {
$products_qty_in_cart = $this->get_cart_item_quantities();
if ( $product_data->is_type( 'variation' ) && true === $managing_stock ) {
$check_qty = isset( $products_qty_in_cart[ $variation_id ] ) ? $products_qty_in_cart[ $variation_id ] : 0;
} else {
$check_qty = isset( $products_qty_in_cart[ $product_id ] ) ? $products_qty_in_cart[ $product_id ] : 0;
}
/**
* Check stock based on all items in the cart.
*/
if ( ! $product_data->has_enough_stock( $check_qty + $quantity ) ) {
throw new Exception( sprintf(
'<a href="%s" class="button wc-forward">%s</a> %s',
wc_get_cart_url(),
__( 'View Cart', 'woocommerce' ),
sprintf( __( 'You cannot add that amount to the cart — we have %s in stock and you already have %s in your cart.', 'woocommerce' ), $product_data->get_stock_quantity(), $check_qty )
) );
}
}
// If cart_item_key is set, the item is already in the cart
if ( $cart_item_key ) {
$new_quantity = $quantity + $this->cart_contents[ $cart_item_key ]['quantity'];
$this->set_quantity( $cart_item_key, $new_quantity, false );
} else {
$cart_item_key = $cart_id;
// Add item after merging with $cart_item_data - hook to allow plugins to modify cart item
$this->cart_contents[ $cart_item_key ] = apply_filters( 'woocommerce_add_cart_item', array_merge( $cart_item_data, array(
'product_id' => $product_id,
'variation_id' => $variation_id,
'variation' => $variation,
'quantity' => $quantity,
'data' => $product_data
) ), $cart_item_key );
}
if ( did_action( 'wp' ) ) {
$this->set_cart_cookies( ! $this->is_empty() );
}
do_action( 'woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data );
return $cart_item_key;
} catch ( Exception $e ) {
if ( $e->getMessage() ) {
wc_add_notice( $e->getMessage(), 'error' );
}
return false;
}
}
/**
* Remove a cart item.
*
* @since 2.3.0
* @param string $cart_item_key
* @return bool
*/
public function remove_cart_item( $cart_item_key ) {
if ( isset( $this->cart_contents[ $cart_item_key ] ) ) {
$this->removed_cart_contents[ $cart_item_key ] = $this->cart_contents[ $cart_item_key ];
unset( $this->removed_cart_contents[ $cart_item_key ]['data'] );
do_action( 'woocommerce_remove_cart_item', $cart_item_key, $this );
unset( $this->cart_contents[ $cart_item_key ] );
do_action( 'woocommerce_cart_item_removed', $cart_item_key, $this );
$this->calculate_totals();
return true;
}
return false;
}
/**
* Restore a cart item.
*
* @param string $cart_item_key
* @return bool
*/
public function restore_cart_item( $cart_item_key ) {
if ( isset( $this->removed_cart_contents[ $cart_item_key ] ) ) {
$this->cart_contents[ $cart_item_key ] = $this->removed_cart_contents[ $cart_item_key ];
$this->cart_contents[ $cart_item_key ]['data'] = wc_get_product( $this->cart_contents[ $cart_item_key ]['variation_id'] ? $this->cart_contents[ $cart_item_key ]['variation_id'] : $this->cart_contents[ $cart_item_key ]['product_id'] );
do_action( 'woocommerce_restore_cart_item', $cart_item_key, $this );
unset( $this->removed_cart_contents[ $cart_item_key ] );
do_action( 'woocommerce_cart_item_restored', $cart_item_key, $this );
$this->calculate_totals();
return true;
}
return false;
}
/**
* Set the quantity for an item in the cart.
*
* @param string $cart_item_key contains the id of the cart item
* @param int $quantity contains the quantity of the item
* @param bool $refresh_totals whether or not to calculate totals after setting the new qty
*
* @return bool
*/
public function set_quantity( $cart_item_key, $quantity = 1, $refresh_totals = true ) {
if ( $quantity == 0 || $quantity < 0 ) {
do_action( 'woocommerce_before_cart_item_quantity_zero', $cart_item_key );
unset( $this->cart_contents[ $cart_item_key ] );
} else {
$old_quantity = $this->cart_contents[ $cart_item_key ]['quantity'];
$this->cart_contents[ $cart_item_key ]['quantity'] = $quantity;
do_action( 'woocommerce_after_cart_item_quantity_update', $cart_item_key, $quantity, $old_quantity );
}
if ( $refresh_totals ) {
$this->calculate_totals();
}
return true;
}
/*-----------------------------------------------------------------------------------*/
/* Cart Calculation Functions */
/*-----------------------------------------------------------------------------------*/
/**
* Reset cart totals to the defaults. Useful before running calculations.
*
* @param bool $unset_session If true, the session data will be forced unset.
* @access private
*/
private function reset( $unset_session = false ) {
foreach ( $this->cart_session_data as $key => $default ) {
$this->$key = $default;
if ( $unset_session ) {
unset( WC()->session->$key );
}
}
do_action( 'woocommerce_cart_reset', $this, $unset_session );
}
/**
* Sort by subtotal.
* @param array $a
* @param array $b
* @return int
*/
private function sort_by_subtotal( $a, $b ) {
$first_item_subtotal = isset( $a['line_subtotal'] ) ? $a['line_subtotal'] : 0;
$second_item_subtotal = isset( $b['line_subtotal'] ) ? $b['line_subtotal'] : 0;
if ( $first_item_subtotal === $second_item_subtotal ) {
return 0;
}
return ( $first_item_subtotal < $second_item_subtotal ) ? 1 : -1;
}
/**
* Calculate totals for the items in the cart.
*/
public function calculate_totals() {
$this->reset();
$this->coupons = $this->get_coupons();
do_action( 'woocommerce_before_calculate_totals', $this );
if ( $this->is_empty() ) {
$this->set_session();
return;
}
$tax_rates = array();
$shop_tax_rates = array();
$cart = $this->get_cart();
/**
* Calculate subtotals for items. This is done first so that discount logic can use the values.
*/
foreach ( $cart as $cart_item_key => $values ) {
$_product = $values['data'];
$line_price = $_product->get_price() * $values['quantity'];
$line_subtotal = 0;
$line_subtotal_tax = 0;
/**
* No tax to calculate.
*/
if ( ! $_product->is_taxable() ) {
// Subtotal is the undiscounted price
$this->subtotal += $line_price;
$this->subtotal_ex_tax += $line_price;
/**
* Prices include tax.
*
* To prevent rounding issues we need to work with the inclusive price where possible.
* otherwise we'll see errors such as when working with a 9.99 inc price, 20% VAT which would.
* be 8.325 leading to totals being 1p off.
*
* Pre tax coupons come off the price the customer thinks they are paying - tax is calculated.
* afterwards.
*
* e.g. $100 bike with $10 coupon = customer pays $90 and tax worked backwards from that.
*/
} elseif ( $this->prices_include_tax ) {
// Get base tax rates
if ( empty( $shop_tax_rates[ $_product->tax_class ] ) ) {
$shop_tax_rates[ $_product->tax_class ] = WC_Tax::get_base_tax_rates( $_product->tax_class );
}
// Get item tax rates
if ( empty( $tax_rates[ $_product->get_tax_class() ] ) ) {
$tax_rates[ $_product->get_tax_class() ] = WC_Tax::get_rates( $_product->get_tax_class() );
}
$base_tax_rates = $shop_tax_rates[ $_product->tax_class ];
$item_tax_rates = $tax_rates[ $_product->get_tax_class() ];
/**
* ADJUST TAX - Calculations when base tax is not equal to the item tax.
*
* The woocommerce_adjust_non_base_location_prices filter can stop base taxes being taken off when dealing with out of base locations.
* e.g. If a product costs 10 including tax, all users will pay 10 regardless of location and taxes.
* This feature is experimental @since 2.4.7 and may change in the future. Use at your risk.
*/
if ( $item_tax_rates !== $base_tax_rates && apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ) {
// Work out a new base price without the shop's base tax
$taxes = WC_Tax::calc_tax( $line_price, $base_tax_rates, true, true );
// Now we have a new item price (excluding TAX)
$line_subtotal = $line_price - array_sum( $taxes );
// Now add modified taxes
$tax_result = WC_Tax::calc_tax( $line_subtotal, $item_tax_rates );
$line_subtotal_tax = array_sum( $tax_result );
/**
* Regular tax calculation (customer inside base and the tax class is unmodified.
*/
} else {
// Calc tax normally
$taxes = WC_Tax::calc_tax( $line_price, $item_tax_rates, true );
$line_subtotal_tax = array_sum( $taxes );
$line_subtotal = $line_price - array_sum( $taxes );
}
/**
* Prices exclude tax.
*
* This calculation is simpler - work with the base, untaxed price.
*/
} else {
// Get item tax rates
if ( empty( $tax_rates[ $_product->get_tax_class() ] ) ) {
$tax_rates[ $_product->get_tax_class() ] = WC_Tax::get_rates( $_product->get_tax_class() );
}
$item_tax_rates = $tax_rates[ $_product->get_tax_class() ];
// Base tax for line before discount - we will store this in the order data
$taxes = WC_Tax::calc_tax( $line_price, $item_tax_rates );
$line_subtotal_tax = array_sum( $taxes );
$line_subtotal = $line_price;
}
// Add to main subtotal
$this->subtotal += $line_subtotal + $line_subtotal_tax;
$this->subtotal_ex_tax += $line_subtotal;
}
// Order cart items by price so coupon logic is 'fair' for customers and not based on order added to cart.
uasort( $cart, array( $this, 'sort_by_subtotal' ) );
/**
* Calculate totals for items.
*/
foreach ( $cart as $cart_item_key => $values ) {
$_product = $values['data'];
// Prices
$base_price = $_product->get_price();
$line_price = $_product->get_price() * $values['quantity'];
// Tax data
$taxes = array();
$discounted_taxes = array();
/**
* No tax to calculate.
*/
if ( ! $_product->is_taxable() ) {
// Discounted Price (price with any pre-tax discounts applied)
$discounted_price = $this->get_discounted_price( $values, $base_price, true );
$line_subtotal_tax = 0;
$line_subtotal = $line_price;
$line_tax = 0;
$line_total = round( $discounted_price * $values['quantity'], wc_get_rounding_precision() );
/**
* Prices include tax.
*/
} elseif ( $this->prices_include_tax ) {
$base_tax_rates = $shop_tax_rates[ $_product->tax_class ];
$item_tax_rates = $tax_rates[ $_product->get_tax_class() ];
/**
* ADJUST TAX - Calculations when base tax is not equal to the item tax.
*
* The woocommerce_adjust_non_base_location_prices filter can stop base taxes being taken off when dealing with out of base locations.
* e.g. If a product costs 10 including tax, all users will pay 10 regardless of location and taxes.
* This feature is experimental @since 2.4.7 and may change in the future. Use at your risk.
*/
if ( $item_tax_rates !== $base_tax_rates && apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ) {
// Work out a new base price without the shop's base tax
$taxes = WC_Tax::calc_tax( $line_price, $base_tax_rates, true, true );
// Now we have a new item price (excluding TAX)
$line_subtotal = round( $line_price - array_sum( $taxes ), wc_get_rounding_precision() );
$taxes = WC_Tax::calc_tax( $line_subtotal, $item_tax_rates );
$line_subtotal_tax = array_sum( $taxes );
// Adjusted price (this is the price including the new tax rate)
$adjusted_price = ( $line_subtotal + $line_subtotal_tax ) / $values['quantity'];
// Apply discounts and get the discounted price FOR A SINGLE ITEM
$discounted_price = $this->get_discounted_price( $values, $adjusted_price, true );
// Convert back to line price and round nicely
$discounted_line_price = round( $discounted_price * $values['quantity'], $this->dp );
// Now use rounded line price to get taxes.
$discounted_taxes = WC_Tax::calc_tax( $discounted_line_price, $item_tax_rates, true );
$line_tax = array_sum( $discounted_taxes );
$line_total = $discounted_line_price - $line_tax;
/**
* Regular tax calculation (customer inside base and the tax class is unmodified.
*/
} else {
// Work out a new base price without the item tax
$taxes = WC_Tax::calc_tax( $line_price, $item_tax_rates, true );
// Now we have a new item price (excluding TAX)
$line_subtotal = $line_price - array_sum( $taxes );
$line_subtotal_tax = array_sum( $taxes );
// Calc prices and tax (discounted)
$discounted_price = $this->get_discounted_price( $values, $base_price, true );
// Convert back to line price and round nicely
$discounted_line_price = round( $discounted_price * $values['quantity'], $this->dp );
// Now use rounded line price to get taxes.
$discounted_taxes = WC_Tax::calc_tax( $discounted_line_price, $item_tax_rates, true );
$line_tax = array_sum( $discounted_taxes );
$line_total = $discounted_line_price - $line_tax;
}
// Tax rows - merge the totals we just got
foreach ( array_keys( $this->taxes + $discounted_taxes ) as $key ) {
$this->taxes[ $key ] = ( isset( $discounted_taxes[ $key ] ) ? $discounted_taxes[ $key ] : 0 ) + ( isset( $this->taxes[ $key ] ) ? $this->taxes[ $key ] : 0 );
}
/**
* Prices exclude tax.
*/
} else {
$item_tax_rates = $tax_rates[ $_product->get_tax_class() ];
// Work out a new base price without the shop's base tax
$taxes = WC_Tax::calc_tax( $line_price, $item_tax_rates );
// Now we have the item price (excluding TAX)
$line_subtotal = $line_price;
$line_subtotal_tax = array_sum( $taxes );
// Now calc product rates
$discounted_price = $this->get_discounted_price( $values, $base_price, true );
$discounted_taxes = WC_Tax::calc_tax( $discounted_price * $values['quantity'], $item_tax_rates );
$discounted_tax_amount = array_sum( $discounted_taxes );
$line_tax = $discounted_tax_amount;
$line_total = $discounted_price * $values['quantity'];
// Tax rows - merge the totals we just got
foreach ( array_keys( $this->taxes + $discounted_taxes ) as $key ) {
$this->taxes[ $key ] = ( isset( $discounted_taxes[ $key ] ) ? $discounted_taxes[ $key ] : 0 ) + ( isset( $this->taxes[ $key ] ) ? $this->taxes[ $key ] : 0 );
}
}
// Cart contents total is based on discounted prices and is used for the final total calculation
$this->cart_contents_total += $line_total;
// Store costs + taxes for lines
$this->cart_contents[ $cart_item_key ]['line_total'] = $line_total;
$this->cart_contents[ $cart_item_key ]['line_tax'] = $line_tax;
$this->cart_contents[ $cart_item_key ]['line_subtotal'] = $line_subtotal;
$this->cart_contents[ $cart_item_key ]['line_subtotal_tax'] = $line_subtotal_tax;
// Store rates ID and costs - Since 2.2
$this->cart_contents[ $cart_item_key ]['line_tax_data'] = array( 'total' => $discounted_taxes, 'subtotal' => $taxes );
}
// Only calculate the grand total + shipping if on the cart/checkout
if ( is_checkout() || is_cart() || defined('WOOCOMMERCE_CHECKOUT') || defined('WOOCOMMERCE_CART') ) {
// Calculate the Shipping
$this->calculate_shipping();
// Trigger the fees API where developers can add fees to the cart
$this->calculate_fees();
// Total up/round taxes and shipping taxes
if ( $this->round_at_subtotal ) {
$this->tax_total = WC_Tax::get_tax_total( $this->taxes );
$this->shipping_tax_total = WC_Tax::get_tax_total( $this->shipping_taxes );
$this->taxes = array_map( array( 'WC_Tax', 'round' ), $this->taxes );
$this->shipping_taxes = array_map( array( 'WC_Tax', 'round' ), $this->shipping_taxes );
} else {
$this->tax_total = array_sum( $this->taxes );
$this->shipping_tax_total = array_sum( $this->shipping_taxes );
}
// VAT exemption done at this point - so all totals are correct before exemption
if ( WC()->customer->is_vat_exempt() ) {
$this->remove_taxes();
}
// Allow plugins to hook and alter totals before final total is calculated
do_action( 'woocommerce_calculate_totals', $this );
// Grand Total - Discounted product prices, discounted tax, shipping cost + tax
$this->total = max( 0, apply_filters( 'woocommerce_calculated_total', round( $this->cart_contents_total + $this->tax_total + $this->shipping_tax_total + $this->shipping_total + $this->fee_total, $this->dp ), $this ) );
} else {
// Set tax total to sum of all tax rows
$this->tax_total = WC_Tax::get_tax_total( $this->taxes );
// VAT exemption done at this point - so all totals are correct before exemption
if ( WC()->customer->is_vat_exempt() ) {
$this->remove_taxes();
}
}
do_action( 'woocommerce_after_calculate_totals', $this );
$this->set_session();
}
/**
* Remove taxes.
*/
public function remove_taxes() {
$this->shipping_tax_total = $this->tax_total = 0;
$this->subtotal = $this->subtotal_ex_tax;
foreach ( $this->cart_contents as $cart_item_key => $item ) {
$this->cart_contents[ $cart_item_key ]['line_subtotal_tax'] = $this->cart_contents[ $cart_item_key ]['line_tax'] = 0;
$this->cart_contents[ $cart_item_key ]['line_tax_data'] = array( 'total' => array(), 'subtotal' => array() );
}
// If true, zero rate is applied so '0' tax is displayed on the frontend rather than nothing.
if ( apply_filters( 'woocommerce_cart_remove_taxes_apply_zero_rate', true ) ) {
$this->taxes = $this->shipping_taxes = array( apply_filters( 'woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated' ) => 0 );
} else {
$this->taxes = $this->shipping_taxes = array();
}
}
/**
* Looks at the totals to see if payment is actually required.
*
* @return bool
*/
public function needs_payment() {
return apply_filters( 'woocommerce_cart_needs_payment', $this->total > 0, $this );
}
/*-----------------------------------------------------------------------------------*/
/* Shipping related functions */
/*-----------------------------------------------------------------------------------*/
/**
* Uses the shipping class to calculate shipping then gets the totals when its finished.
*/
public function calculate_shipping() {
if ( $this->needs_shipping() && $this->show_shipping() ) {
WC()->shipping->calculate_shipping( $this->get_shipping_packages() );
} else {
WC()->shipping->reset_shipping();
}
// Get totals for the chosen shipping method
$this->shipping_total = WC()->shipping->shipping_total; // Shipping Total
$this->shipping_taxes = WC()->shipping->shipping_taxes; // Shipping Taxes
}
/**
* Get packages to calculate shipping for.
*
* This lets us calculate costs for carts that are shipped to multiple locations.
*
* Shipping methods are responsible for looping through these packages.
*
* By default we pass the cart itself as a package - plugins can change this.
* through the filter and break it up.
*
* @since 1.5.4
* @return array of cart items
*/
public function get_shipping_packages() {
// Packages array for storing 'carts'
$packages = array();
$packages[0]['contents'] = $this->get_cart(); // Items in the package
$packages[0]['contents_cost'] = 0; // Cost of items in the package, set below
$packages[0]['applied_coupons'] = $this->applied_coupons;
$packages[0]['user']['ID'] = get_current_user_id();
$packages[0]['destination']['country'] = WC()->customer->get_shipping_country();
$packages[0]['destination']['state'] = WC()->customer->get_shipping_state();
$packages[0]['destination']['postcode'] = WC()->customer->get_shipping_postcode();
$packages[0]['destination']['city'] = WC()->customer->get_shipping_city();
$packages[0]['destination']['address'] = WC()->customer->get_shipping_address();
$packages[0]['destination']['address_2'] = WC()->customer->get_shipping_address_2();
foreach ( $this->get_cart() as $item ) {
if ( $item['data']->needs_shipping() ) {
if ( isset( $item['line_total'] ) ) {
$packages[0]['contents_cost'] += $item['line_total'];
}
}
}
return apply_filters( 'woocommerce_cart_shipping_packages', $packages );
}
/**
* Looks through the cart to see if shipping is actually required.
*
* @return bool whether or not the cart needs shipping
*/
public function needs_shipping() {
// If shipping is disabled or not yet configured, we can skip this.
if ( ! wc_shipping_enabled() || 0 === wc_get_shipping_method_count( true ) ) {
return false;
}
$needs_shipping = false;
if ( ! empty( $this->cart_contents ) ) {
foreach ( $this->cart_contents as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->needs_shipping() ) {
$needs_shipping = true;
}
}
}
return apply_filters( 'woocommerce_cart_needs_shipping', $needs_shipping );
}
/**
* Should the shipping address form be shown.
*
* @return bool
*/
public function needs_shipping_address() {
$needs_shipping_address = false;
if ( $this->needs_shipping() === true && ! wc_ship_to_billing_address_only() ) {
$needs_shipping_address = true;
}
return apply_filters( 'woocommerce_cart_needs_shipping_address', $needs_shipping_address );
}
/**
* Sees if the customer has entered enough data to calc the shipping yet.
*
* @return bool
*/
public function show_shipping() {
if ( ! wc_shipping_enabled() || ! is_array( $this->cart_contents ) )
return false;
if ( 'yes' === get_option( 'woocommerce_shipping_cost_requires_address' ) ) {
if ( ! WC()->customer->has_calculated_shipping() ) {
if ( ! WC()->customer->get_shipping_country() || ( ! WC()->customer->get_shipping_state() && ! WC()->customer->get_shipping_postcode() ) ) {
return false;
}
}
}
return apply_filters( 'woocommerce_cart_ready_to_calc_shipping', true );
}
/**
* Sees if we need a shipping address.
*
* @deprecated 2.5.0 in favor to wc_ship_to_billing_address_only()
*
* @return bool
*/
public function ship_to_billing_address_only() {
return wc_ship_to_billing_address_only();
}
/**
* Gets the shipping total (after calculation).
*
* @return string price or string for the shipping total
*/
public function get_cart_shipping_total() {
if ( isset( $this->shipping_total ) ) {
if ( $this->shipping_total > 0 ) {
// Display varies depending on settings
if ( $this->tax_display_cart == 'excl' ) {
$return = wc_price( $this->shipping_total );
if ( $this->shipping_tax_total > 0 && $this->prices_include_tax ) {
$return .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
return $return;
} else {
$return = wc_price( $this->shipping_total + $this->shipping_tax_total );
if ( $this->shipping_tax_total > 0 && ! $this->prices_include_tax ) {
$return .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
}
return $return;
}
} else {
return __( 'Free!', 'woocommerce' );
}
}
return '';
}
/*-----------------------------------------------------------------------------------*/
/* Coupons/Discount related functions */
/*-----------------------------------------------------------------------------------*/
/**
* Check for user coupons (now that we have billing email). If a coupon is invalid, add an error.
*
* Checks two types of coupons:
* 1. Where a list of customer emails are set (limits coupon usage to those defined).
* 2. Where a usage_limit_per_user is set (limits coupon usage to a number based on user ID and email).
*
* @param array $posted
*/
public function check_customer_coupons( $posted ) {
if ( ! empty( $this->applied_coupons ) ) {
foreach ( $this->applied_coupons as $code ) {
$coupon = new WC_Coupon( $code );
if ( $coupon->is_valid() ) {
// Limit to defined email addresses
if ( is_array( $coupon->customer_email ) && sizeof( $coupon->customer_email ) > 0 ) {
$check_emails = array();
$coupon->customer_email = array_map( 'sanitize_email', $coupon->customer_email );
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$check_emails[] = $current_user->user_email;
}
$check_emails[] = $posted['billing_email'];
$check_emails = array_map( 'sanitize_email', array_map( 'strtolower', $check_emails ) );
if ( 0 == sizeof( array_intersect( $check_emails, $coupon->customer_email ) ) ) {
$coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_NOT_YOURS_REMOVED );
// Remove the coupon
$this->remove_coupon( $code );
// Flag totals for refresh
WC()->session->set( 'refresh_totals', true );
}
}
// Usage limits per user - check against billing and user email and user ID
if ( $coupon->usage_limit_per_user > 0 ) {
$check_emails = array();
$used_by = $coupon->get_used_by();
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$check_emails[] = sanitize_email( $current_user->user_email );
$usage_count = sizeof( array_keys( $used_by, get_current_user_id() ) );
} else {
$check_emails[] = sanitize_email( $posted['billing_email'] );
$user = get_user_by( 'email', $posted['billing_email'] );
if ( $user ) {
$usage_count = sizeof( array_keys( $used_by, $user->ID ) );
} else {
$usage_count = 0;
}
}
foreach ( $check_emails as $check_email ) {
$usage_count = $usage_count + sizeof( array_keys( $used_by, $check_email ) );
}
if ( $usage_count >= $coupon->usage_limit_per_user ) {
$coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_USAGE_LIMIT_REACHED );
// Remove the coupon
$this->remove_coupon( $code );
// Flag totals for refresh
WC()->session->set( 'refresh_totals', true );
}
}
}
}
}
}
/**
* Returns whether or not a discount has been applied.
* @param string $coupon_code
* @return bool
*/
public function has_discount( $coupon_code = '' ) {
return $coupon_code ? in_array( apply_filters( 'woocommerce_coupon_code', $coupon_code ), $this->applied_coupons ) : sizeof( $this->applied_coupons ) > 0;
}
/**
* Applies a coupon code passed to the method.
*
* @param string $coupon_code - The code to apply
* @return bool True if the coupon is applied, false if it does not exist or cannot be applied
*/
public function add_discount( $coupon_code ) {
// Coupons are globally disabled
if ( ! wc_coupons_enabled() ) {
return false;
}
// Sanitize coupon code
$coupon_code = apply_filters( 'woocommerce_coupon_code', $coupon_code );
// Get the coupon
$the_coupon = new WC_Coupon( $coupon_code );
// Check it can be used with cart
if ( ! $the_coupon->is_valid() ) {
wc_add_notice( $the_coupon->get_error_message(), 'error' );
return false;
}
// Check if applied
if ( $this->has_discount( $coupon_code ) ) {
$the_coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_ALREADY_APPLIED );
return false;
}
// If its individual use then remove other coupons
if ( $the_coupon->individual_use == 'yes' ) {
$this->applied_coupons = apply_filters( 'woocommerce_apply_individual_use_coupon', array(), $the_coupon, $this->applied_coupons );
}
if ( $this->applied_coupons ) {
foreach ( $this->applied_coupons as $code ) {
$coupon = new WC_Coupon( $code );
if ( $coupon->individual_use == 'yes' && false === apply_filters( 'woocommerce_apply_with_individual_use_coupon', false, $the_coupon, $coupon, $this->applied_coupons ) ) {
// Reject new coupon
$coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY );
return false;
}
}
}
$this->applied_coupons[] = $coupon_code;
// Choose free shipping
if ( $the_coupon->enable_free_shipping() ) {
$packages = WC()->shipping->get_packages();
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
foreach ( $packages as $i => $package ) {
$chosen_shipping_methods[ $i ] = 'free_shipping';
}
WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods );
}
$the_coupon->add_coupon_message( WC_Coupon::WC_COUPON_SUCCESS );
do_action( 'woocommerce_applied_coupon', $coupon_code );
return true;
}
/**
* Get array of applied coupon objects and codes.
* @return array of applied coupons
*/
public function get_coupons( $deprecated = null ) {
$coupons = array();
if ( 'order' === $deprecated ) {
return $coupons;
}
foreach ( $this->get_applied_coupons() as $code ) {
$coupon = new WC_Coupon( $code );
$coupons[ $code ] = $coupon;
}
return $coupons;
}
/**
* Gets the array of applied coupon codes.
*
* @return array of applied coupons
*/
public function get_applied_coupons() {
return $this->applied_coupons;
}
/**
* Get the discount amount for a used coupon.
* @param string $code coupon code
* @param bool $ex_tax inc or ex tax
* @return float discount amount
*/
public function get_coupon_discount_amount( $code, $ex_tax = true ) {
$discount_amount = isset( $this->coupon_discount_amounts[ $code ] ) ? $this->coupon_discount_amounts[ $code ] : 0;
if ( ! $ex_tax ) {
$discount_amount += $this->get_coupon_discount_tax_amount( $code );
}
return wc_cart_round_discount( $discount_amount, $this->dp );
}
/**
* Get the discount tax amount for a used coupon (for tax inclusive prices).
* @param string $code coupon code
* @param bool inc or ex tax
* @return float discount amount
*/
public function get_coupon_discount_tax_amount( $code ) {
return wc_cart_round_discount( isset( $this->coupon_discount_tax_amounts[ $code ] ) ? $this->coupon_discount_tax_amounts[ $code ] : 0, $this->dp );
}
/**
* Remove coupons from the cart of a defined type. Type 1 is before tax, type 2 is after tax.
*/
public function remove_coupons( $deprecated = null ) {
$this->applied_coupons = $this->coupon_discount_amounts = $this->coupon_discount_tax_amounts = $this->coupon_applied_count = array();
WC()->session->set( 'applied_coupons', array() );
WC()->session->set( 'coupon_discount_amounts', array() );
WC()->session->set( 'coupon_discount_tax_amounts', array() );
}
/**
* Remove a single coupon by code.
* @param string $coupon_code Code of the coupon to remove
* @return bool
*/
public function remove_coupon( $coupon_code ) {
// Coupons are globally disabled
if ( ! wc_coupons_enabled() ) {
return false;
}
// Get the coupon
$coupon_code = apply_filters( 'woocommerce_coupon_code', $coupon_code );
$position = array_search( $coupon_code, $this->applied_coupons );
if ( $position !== false ) {
unset( $this->applied_coupons[ $position ] );
}
WC()->session->set( 'applied_coupons', $this->applied_coupons );
do_action( 'woocommerce_removed_coupon', $coupon_code );
return true;
}
/**
* Function to apply discounts to a product and get the discounted price (before tax is applied).
*
* @param mixed $values
* @param mixed $price
* @param bool $add_totals (default: false)
* @return float price
*/
public function get_discounted_price( $values, $price, $add_totals = false ) {
if ( ! $price ) {
return $price;
}
$undiscounted_price = $price;
if ( ! empty( $this->coupons ) ) {
$product = $values['data'];
foreach ( $this->coupons as $code => $coupon ) {
if ( $coupon->is_valid() && ( $coupon->is_valid_for_product( $product, $values ) || $coupon->is_valid_for_cart() ) ) {
$discount_amount = $coupon->get_discount_amount( 'yes' === get_option( 'woocommerce_calc_discounts_sequentially', 'no' ) ? $price : $undiscounted_price, $values, true );
$discount_amount = min( $price, $discount_amount );
$price = max( $price - $discount_amount, 0 );
// Store the totals for DISPLAY in the cart
if ( $add_totals ) {
$total_discount = $discount_amount * $values['quantity'];
$total_discount_tax = 0;
if ( wc_tax_enabled() ) {
$tax_rates = WC_Tax::get_rates( $product->get_tax_class() );
$taxes = WC_Tax::calc_tax( $discount_amount, $tax_rates, $this->prices_include_tax );
$total_discount_tax = WC_Tax::get_tax_total( $taxes ) * $values['quantity'];
$total_discount = $this->prices_include_tax ? $total_discount - $total_discount_tax : $total_discount;
$this->discount_cart_tax += $total_discount_tax;
}
$this->discount_cart += $total_discount;
$this->increase_coupon_discount_amount( $code, $total_discount, $total_discount_tax );
$this->increase_coupon_applied_count( $code, $values['quantity'] );
}
}
// If the price is 0, we can stop going through coupons because there is nothing more to discount for this product.
if ( 0 >= $price ) {
break;
}
}
}
return apply_filters( 'woocommerce_get_discounted_price', $price, $values, $this );
}
/**
* Store how much discount each coupon grants.
*
* @access private
* @param string $code
* @param double $amount
* @param double $tax
*/
private function increase_coupon_discount_amount( $code, $amount, $tax ) {
$this->coupon_discount_amounts[ $code ] = isset( $this->coupon_discount_amounts[ $code ] ) ? $this->coupon_discount_amounts[ $code ] + $amount : $amount;
$this->coupon_discount_tax_amounts[ $code ] = isset( $this->coupon_discount_tax_amounts[ $code ] ) ? $this->coupon_discount_tax_amounts[ $code ] + $tax : $tax;
}
/**
* Store how many times each coupon is applied to cart/items.
*
* @access private
* @param string $code
* @param int $count
*/
private function increase_coupon_applied_count( $code, $count = 1 ) {
if ( empty( $this->coupon_applied_count[ $code ] ) ) {
$this->coupon_applied_count[ $code ] = 0;
}
$this->coupon_applied_count[ $code ] += $count;
}
/*-----------------------------------------------------------------------------------*/
/* Fees API to add additional costs to orders */
/*-----------------------------------------------------------------------------------*/
/**
* Add additional fee to the cart.
*
* @param string $name Unique name for the fee. Multiple fees of the same name cannot be added.
* @param float $amount Fee amount.
* @param bool $taxable (default: false) Is the fee taxable?
* @param string $tax_class (default: '') The tax class for the fee if taxable. A blank string is standard tax class.
*/
public function add_fee( $name, $amount, $taxable = false, $tax_class = '' ) {
$new_fee_id = sanitize_title( $name );
// Only add each fee once
foreach ( $this->fees as $fee ) {
if ( $fee->id == $new_fee_id ) {
return;
}
}
$new_fee = new stdClass();
$new_fee->id = $new_fee_id;
$new_fee->name = esc_attr( $name );
$new_fee->amount = (float) esc_attr( $amount );
$new_fee->tax_class = $tax_class;
$new_fee->taxable = $taxable ? true : false;
$new_fee->tax = 0;
$new_fee->tax_data = array();
$this->fees[] = $new_fee;
}
/**
* Get fees.
*
* @return array
*/
public function get_fees() {
return array_filter( (array) $this->fees );
}
/**
* Calculate fees.
*/
public function calculate_fees() {
// Reset fees before calculation
$this->fee_total = 0;
$this->fees = array();
// Fire an action where developers can add their fees
do_action( 'woocommerce_cart_calculate_fees', $this );
// If fees were added, total them and calculate tax
if ( ! empty( $this->fees ) ) {
foreach ( $this->fees as $fee_key => $fee ) {
$this->fee_total += $fee->amount;
if ( $fee->taxable ) {
// Get tax rates
$tax_rates = WC_Tax::get_rates( $fee->tax_class );
$fee_taxes = WC_Tax::calc_tax( $fee->amount, $tax_rates, false );
if ( ! empty( $fee_taxes ) ) {
// Set the tax total for this fee
$this->fees[ $fee_key ]->tax = array_sum( $fee_taxes );
// Set tax data - Since 2.2
$this->fees[ $fee_key ]->tax_data = $fee_taxes;
// Tax rows - merge the totals we just got
foreach ( array_keys( $this->taxes + $fee_taxes ) as $key ) {
$this->taxes[ $key ] = ( isset( $fee_taxes[ $key ] ) ? $fee_taxes[ $key ] : 0 ) + ( isset( $this->taxes[ $key ] ) ? $this->taxes[ $key ] : 0 );
}
}
}
}
}
}
/*-----------------------------------------------------------------------------------*/
/* Get Formatted Totals */
/*-----------------------------------------------------------------------------------*/
/**
* Gets the order total (after calculation).
*
* @return string formatted price
*/
public function get_total() {
return apply_filters( 'woocommerce_cart_total', wc_price( $this->total ) );
}
/**
* Gets the total excluding taxes.
*
* @return string formatted price
*/
public function get_total_ex_tax() {
$total = $this->total - $this->tax_total - $this->shipping_tax_total;
if ( $total < 0 ) {
$total = 0;
}
return apply_filters( 'woocommerce_cart_total_ex_tax', wc_price( $total ) );
}
/**
* Gets the cart contents total (after calculation).
*
* @return string formatted price
*/
public function get_cart_total() {
if ( ! $this->prices_include_tax ) {
$cart_contents_total = wc_price( $this->cart_contents_total );
} else {
$cart_contents_total = wc_price( $this->cart_contents_total + $this->tax_total );
}
return apply_filters( 'woocommerce_cart_contents_total', $cart_contents_total );
}
/**
* Gets the sub total (after calculation).
*
* @param bool $compound whether to include compound taxes
* @return string formatted price
*/
public function get_cart_subtotal( $compound = false ) {
// If the cart has compound tax, we want to show the subtotal as
// cart + shipping + non-compound taxes (after discount)
if ( $compound ) {
$cart_subtotal = wc_price( $this->cart_contents_total + $this->shipping_total + $this->get_taxes_total( false, false ) );
// Otherwise we show cart items totals only (before discount)
} else {
// Display varies depending on settings
if ( $this->tax_display_cart == 'excl' ) {
$cart_subtotal = wc_price( $this->subtotal_ex_tax );
if ( $this->tax_total > 0 && $this->prices_include_tax ) {
$cart_subtotal .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
} else {
$cart_subtotal = wc_price( $this->subtotal );
if ( $this->tax_total > 0 && !$this->prices_include_tax ) {
$cart_subtotal .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
}
}
}
return apply_filters( 'woocommerce_cart_subtotal', $cart_subtotal, $compound, $this );
}
/**
* Get the product row price per item.
*
* @param WC_Product $_product
* @return string formatted price
*/
public function get_product_price( $_product ) {
if ( $this->tax_display_cart == 'excl' ) {
$product_price = $_product->get_price_excluding_tax();
} else {
$product_price = $_product->get_price_including_tax();
}
return apply_filters( 'woocommerce_cart_product_price', wc_price( $product_price ), $_product );
}
/**
* Get the product row subtotal.
*
* Gets the tax etc to avoid rounding issues.
*
* When on the checkout (review order), this will get the subtotal based on the customer's tax rate rather than the base rate.
*
* @param WC_Product $_product
* @param int $quantity
* @return string formatted price
*/
public function get_product_subtotal( $_product, $quantity ) {
$price = $_product->get_price();
$taxable = $_product->is_taxable();
// Taxable
if ( $taxable ) {
if ( $this->tax_display_cart == 'excl' ) {
$row_price = $_product->get_price_excluding_tax( $quantity );
$product_subtotal = wc_price( $row_price );
if ( $this->prices_include_tax && $this->tax_total > 0 ) {
$product_subtotal .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
} else {
$row_price = $_product->get_price_including_tax( $quantity );
$product_subtotal = wc_price( $row_price );
if ( ! $this->prices_include_tax && $this->tax_total > 0 ) {
$product_subtotal .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
}
}
// Non-taxable
} else {
$row_price = $price * $quantity;
$product_subtotal = wc_price( $row_price );
}
return apply_filters( 'woocommerce_cart_product_subtotal', $product_subtotal, $_product, $quantity, $this );
}
/**
* Gets the cart tax (after calculation).
*
* @return string formatted price
*/
public function get_cart_tax() {
$cart_total_tax = wc_round_tax_total( $this->tax_total + $this->shipping_tax_total );
return apply_filters( 'woocommerce_get_cart_tax', $cart_total_tax ? wc_price( $cart_total_tax ) : '' );
}
/**
* Get a tax amount.
* @param string $tax_rate_id
* @return float amount
*/
public function get_tax_amount( $tax_rate_id ) {
return isset( $this->taxes[ $tax_rate_id ] ) ? $this->taxes[ $tax_rate_id ] : 0;
}
/**
* Get a tax amount.
* @param string $tax_rate_id
* @return float amount
*/
public function get_shipping_tax_amount( $tax_rate_id ) {
return isset( $this->shipping_taxes[ $tax_rate_id ] ) ? $this->shipping_taxes[ $tax_rate_id ] : 0;
}
/**
* Get tax row amounts with or without compound taxes includes.
*
* @param bool $compound True if getting compound taxes
* @param bool $display True if getting total to display
* @return float price
*/
public function get_taxes_total( $compound = true, $display = true ) {
$total = 0;
foreach ( $this->taxes as $key => $tax ) {
if ( ! $compound && WC_Tax::is_compound( $key ) ) continue;
$total += $tax;
}
foreach ( $this->shipping_taxes as $key => $tax ) {
if ( ! $compound && WC_Tax::is_compound( $key ) ) continue;
$total += $tax;
}
if ( $display ) {
$total = wc_round_tax_total( $total );
}
return apply_filters( 'woocommerce_cart_taxes_total', $total, $compound, $display, $this );
}
/**
* Get the total of all cart discounts.
*
* @return float
*/
public function get_cart_discount_total() {
return wc_cart_round_discount( $this->discount_cart, $this->dp );
}
/**
* Get the total of all cart tax discounts (used for discounts on tax inclusive prices).
*
* @return float
*/
public function get_cart_discount_tax_total() {
return wc_cart_round_discount( $this->discount_cart_tax, $this->dp );
}
/**
* Gets the total discount amount - both kinds.
*
* @return mixed formatted price or false if there are none
*/
public function get_total_discount() {
if ( $this->get_cart_discount_total() ) {
$total_discount = wc_price( $this->get_cart_discount_total() );
} else {
$total_discount = false;
}
return apply_filters( 'woocommerce_cart_total_discount', $total_discount, $this );
}
/**
* Gets the total (product) discount amount - these are applied before tax.
*
* @deprecated Order discounts (after tax) removed in 2.3 so multiple methods for discounts are no longer required.
* @return mixed formatted price or false if there are none
*/
public function get_discounts_before_tax() {
_deprecated_function( 'get_discounts_before_tax', '2.3', 'get_total_discount' );
if ( $this->get_cart_discount_total() ) {
$discounts_before_tax = wc_price( $this->get_cart_discount_total() );
} else {
$discounts_before_tax = false;
}
return apply_filters( 'woocommerce_cart_discounts_before_tax', $discounts_before_tax, $this );
}
/**
* Get the total of all order discounts (after tax discounts).
*
* @deprecated Order discounts (after tax) removed in 2.3
* @return int
*/
public function get_order_discount_total() {
_deprecated_function( 'get_order_discount_total', '2.3' );
return 0;
}
/**
* Function to apply cart discounts after tax.
* @deprecated Coupons can not be applied after tax
*/
public function apply_cart_discounts_after_tax( $values, $price ) {
_deprecated_function( 'apply_cart_discounts_after_tax', '2.3' );
}
/**
* Function to apply product discounts after tax.
* @deprecated Coupons can not be applied after tax
*/
public function apply_product_discounts_after_tax( $values, $price ) {
_deprecated_function( 'apply_product_discounts_after_tax', '2.3' );
}
/**
* Gets the order discount amount - these are applied after tax.
* @deprecated Coupons can not be applied after tax
*/
public function get_discounts_after_tax() {
_deprecated_function( 'get_discounts_after_tax', '2.3' );
}
}