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
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
|
<?php /** * Abstract Order * * Handles generic order data and database interaction which is extended by both * WC_Order (regular orders) and WC_Order_Refund (refunds are negative orders). * * @class WC_Abstract_Order * @version 3.0.0 * @package WooCommerce\Classes */
use Automattic\WooCommerce\Caches\OrderCache; use Automattic\WooCommerce\Enums\OrderStatus; use Automattic\WooCommerce\Enums\ProductType; use Automattic\WooCommerce\Internal\CostOfGoodsSold\CogsAwareTrait; use Automattic\WooCommerce\Internal\Orders\PaymentInfo; use Automattic\WooCommerce\Proxies\LegacyProxy; use Automattic\WooCommerce\Utilities\ArrayUtil; use Automattic\WooCommerce\Utilities\NumberUtil; use Automattic\WooCommerce\Utilities\OrderUtil;
defined( 'ABSPATH' ) || exit;
require_once WC_ABSPATH . 'includes/legacy/abstract-wc-legacy-order.php';
/** * WC_Abstract_Order class. */ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { use WC_Item_Totals; use CogsAwareTrait;
/** * Order Data array. This is the core order data exposed in APIs since 3.0.0. * * Notes: cart_tax = cart_tax is the new name for the legacy 'order_tax' * which is the tax for items only, not shipping. * * @since 3.0.0 * @var array */ protected $data = array( 'parent_id' => 0, 'status' => '', 'currency' => '', 'version' => '', 'prices_include_tax' => false, 'date_created' => null, 'date_modified' => null, 'discount_total' => 0, 'discount_tax' => 0, 'shipping_total' => 0, 'shipping_tax' => 0, 'cart_tax' => 0, 'total' => 0, 'total_tax' => 0, );
/** * List of properties that were earlier managed by data store. However, since DataStore is a not a stored entity in itself, they used to store data in metadata of the data object. * With custom tables, some of these are moved from metadata to their own columns, but existing code will still try to add them to metadata. This array is used to keep track of such properties. * * Only reason to add a property here is that you are moving properties from DataStore instance to data object. If you are adding a new property, consider adding it to to $data array instead. * * @var array */ protected $legacy_datastore_props = array( '_recorded_coupon_usage_counts', );
/** * Order items will be stored here, sometimes before they persist in the DB. * * @since 3.0.0 * @var array */ protected $items = array();
/** * Order items that need deleting are stored here. * * @since 3.0.0 * @var array */ protected $items_to_delete = array();
/** * Stores meta in cache for future reads. * * A group must be set to to enable caching. * * @var string */ protected $cache_group = 'orders';
/** * Which data store to load. * * @var string */ protected $data_store_name = 'order';
/** * This is the name of this object type. * * @var string */ protected $object_type = 'order';
/** * Get the order if ID is passed, otherwise the order is new and empty. * This class should NOT be instantiated, but the wc_get_order function or new WC_Order_Factory * should be used. It is possible, but the aforementioned are preferred and are the only * methods that will be maintained going forward. * * @param int|object|WC_Order $order Order to read. */ public function __construct( $order = 0 ) { if ( $this->has_cogs() && $this->cogs_is_enabled() ) { $this->data['cogs_total_value'] = 0; }
parent::__construct( $order );
if ( is_numeric( $order ) && $order > 0 ) { $this->set_id( $order ); } elseif ( $order instanceof self ) { $this->set_id( $order->get_id() ); } elseif ( ! empty( $order->ID ) ) { $this->set_id( $order->ID ); } else { $this->set_object_read( true ); }
$this->data_store = WC_Data_Store::load( $this->data_store_name );
if ( $this->get_id() > 0 ) { $this->data_store->read( $this ); } }
/** * This method overwrites the base class's clone method to make it a no-op. In base class WC_Data, we are unsetting the meta_id to clone. * It seems like this was done to avoid conflicting the metadata when duplicating products. However, doing that does not seems necessary for orders. * In-fact, when we do that for orders, we lose the capability to clone orders with custom meta data by caching plugins. This is because, when we clone an order object for caching, it will clone the metadata without the ID. Unfortunately, when this cached object with nulled meta ID is retrieved, WC_Data will consider it as a new meta and will insert it as a new meta-data causing duplicates. * * Eventually, we should move away from overwriting the __clone method in base class itself, since it's easily possible to still duplicate the product without having to hook into the __clone method. * * @since 7.6.0 */ public function __clone() {}
/** * Get internal type. * * @return string */ public function get_type() { return 'shop_order'; }
/** * Get all class data in array format. * * @since 3.0.0 * @return array */ public function get_data() { return array_merge( array( 'id' => $this->get_id(), ), $this->data, array( 'meta_data' => $this->get_meta_data(), 'line_items' => $this->get_items( 'line_item' ), 'tax_lines' => $this->get_items( 'tax' ), 'shipping_lines' => $this->get_items( 'shipping' ), 'fee_lines' => $this->get_items( 'fee' ), 'coupon_lines' => $this->get_items( 'coupon' ), ) ); }
/* |-------------------------------------------------------------------------- | CRUD methods |-------------------------------------------------------------------------- | | Methods which create, read, update and delete orders from the database. | Written in abstract fashion so that the way orders are stored can be | changed more easily in the future. | | A save method is included for convenience (chooses update or create based | on if the order exists yet). | */
/** * Save data to the database. * * @since 3.0.0 * @return int order ID */ public function save() { if ( ! $this->data_store ) { return $this->get_id(); }
try { /** * Trigger action before saving to the DB. Allows you to adjust object props before save. * * @param WC_Data $this The object being saved. * @param WC_Data_Store_WP $data_store THe data store persisting the data. */ do_action( 'woocommerce_before_' . $this->object_type . '_object_save', $this, $this->data_store );
if ( $this->get_id() ) { $this->data_store->update( $this ); } else { $this->data_store->create( $this ); }
$this->save_items();
if ( OrderUtil::orders_cache_usage_is_enabled() ) { $order_cache = wc_get_container()->get( OrderCache::class ); $order_cache->remove( $this->get_id() ); }
/** * Trigger action after saving to the DB. * * @param WC_Data $this The object being saved. * @param WC_Data_Store_WP $data_store THe data store persisting the data. */ do_action( 'woocommerce_after_' . $this->object_type . '_object_save', $this, $this->data_store );
} catch ( Exception $e ) { $message_id = $this->get_id() ? $this->get_id() : __( '(no ID)', 'woocommerce' ); $this->handle_exception( $e, wp_kses_post( sprintf( /* translators: 1: Order ID or "(no ID)" if not known. */ __( 'Error saving order ID %1$s.', 'woocommerce' ), $message_id ) ) ); }
return $this->get_id(); }
/** * Log an error about this order is exception is encountered. * * @param Exception $e Exception object. * @param string $message Message regarding exception thrown. * @since 3.7.0 */ protected function handle_exception( $e, $message = 'Error' ) { wc_get_logger()->error( $message, array( 'order' => $this, 'error' => $e, ) ); }
/** * Save all order items which are part of this order. */ protected function save_items() { $items_changed = false;
foreach ( $this->items_to_delete as $item ) { $item->delete(); $items_changed = true; } $this->items_to_delete = array();
// Add/save items. foreach ( $this->items as $item_group => $items ) { if ( is_array( $items ) ) { $items = array_filter( $items ); foreach ( $items as $item_key => $item ) { $item->set_order_id( $this->get_id() );
$item_id = $item->save();
// If ID changed (new item saved to DB)... if ( $item_id !== $item_key ) { $this->items[ $item_group ][ $item_id ] = $item;
unset( $this->items[ $item_group ][ $item_key ] );
$items_changed = true; } } } }
if ( $items_changed ) { delete_transient( 'wc_order_' . $this->get_id() . '_needs_processing' ); } }
/* |-------------------------------------------------------------------------- | Getters |-------------------------------------------------------------------------- */
/** * Get parent order ID. * * @since 3.0.0 * @param string $context View or edit context. * @return integer */ public function get_parent_id( $context = 'view' ) { return $this->get_prop( 'parent_id', $context ); }
/** * Gets order currency. * * @param string $context View or edit context. * @return string */ public function get_currency( $context = 'view' ) { return $this->get_prop( 'currency', $context ); }
/** * Get order_version. * * @param string $context View or edit context. * @return string */ public function get_version( $context = 'view' ) { return $this->get_prop( 'version', $context ); }
/** * Get prices_include_tax. * * @param string $context View or edit context. * @return bool */ public function get_prices_include_tax( $context = 'view' ) { return $this->get_prop( 'prices_include_tax', $context ); }
/** * Get date_created. * * @param string $context View or edit context. * @return WC_DateTime|NULL object if the date is set or null if there is no date. */ public function get_date_created( $context = 'view' ) { return $this->get_prop( 'date_created', $context ); }
/** * Get date_modified. * * @param string $context View or edit context. * @return WC_DateTime|NULL object if the date is set or null if there is no date. */ public function get_date_modified( $context = 'view' ) { return $this->get_prop( 'date_modified', $context ); }
/** * Get date_modified. * * @param string $context View or edit context. * @return WC_DateTime|NULL object if the date is set or null if there is no date. */ public function get_date_paid( $context = 'view' ) { return $this->get_prop( 'date_paid', $context ); }
/** * Get date_modified. * * @param string $context View or edit context. * @return WC_DateTime|NULL object if the date is set or null if there is no date. */ public function get_date_completed( $context = 'view' ) { return $this->get_prop( 'date_completed', $context ); }
/** * Return the order statuses without wc- internal prefix. * * @param string $context View or edit context. * @return string */ public function get_status( $context = 'view' ) { $status = $this->get_prop( 'status', $context );
if ( empty( $status ) && 'view' === $context ) { /** * In view context, return the default status if no status has been set. * * @since 3.0.0 * * @param string $status Default status. */ $status = apply_filters( 'woocommerce_default_order_status', OrderStatus::PENDING ); } return $status; }
/** * Get discount_total. * * @param string $context View or edit context. * @return string */ public function get_discount_total( $context = 'view' ) { return $this->get_prop( 'discount_total', $context ); }
/** * Get discount_tax. * * @param string $context View or edit context. * @return string */ public function get_discount_tax( $context = 'view' ) { return $this->get_prop( 'discount_tax', $context ); }
/** * Get shipping_total. * * @param string $context View or edit context. * @return string */ public function get_shipping_total( $context = 'view' ) { return $this->get_prop( 'shipping_total', $context ); }
/** * Get shipping_tax. * * @param string $context View or edit context. * @return string */ public function get_shipping_tax( $context = 'view' ) { return $this->get_prop( 'shipping_tax', $context ); }
/** * Gets cart tax amount. * * @param string $context View or edit context. * @return float */ public function get_cart_tax( $context = 'view' ) { return $this->get_prop( 'cart_tax', $context ); }
/** * Gets order grand total including taxes, shipping cost, fees, and coupon discounts. Used in gateways. * * @param string $context View or edit context. * @return float */ public function get_total( $context = 'view' ) { return $this->get_prop( 'total', $context ); }
/** * Get total tax amount. Alias for get_order_tax(). * * @param string $context View or edit context. * @return float */ public function get_total_tax( $context = 'view' ) { return $this->get_prop( 'total_tax', $context ); }
/* |-------------------------------------------------------------------------- | Non-CRUD Getters |-------------------------------------------------------------------------- */
/** * Gets the total discount amount. * * @param bool $ex_tax Show discount excl any tax. * @return float */ public function get_total_discount( $ex_tax = true ) { if ( $ex_tax ) { $total_discount = (float) $this->get_discount_total(); } else { $total_discount = (float) $this->get_discount_total() + (float) $this->get_discount_tax(); } return apply_filters( 'woocommerce_order_get_total_discount', NumberUtil::round( $total_discount, WC_ROUNDING_PRECISION ), $this ); }
/** * Gets order subtotal. Order subtotal is the price of all items excluding taxes, fees, shipping cost, and coupon discounts. * If sale price is set on an item, the subtotal will include this sale discount. E.g. a product with a regular * price of $100 bought at a 50% discount will represent $50 of the subtotal for the order. * * @return float */ public function get_subtotal() { $subtotal = NumberUtil::round( $this->get_cart_subtotal_for_order(), wc_get_price_decimals() ); return apply_filters( 'woocommerce_order_get_subtotal', (float) $subtotal, $this ); }
/** * Get taxes, merged by code, formatted ready for output. * * @return array */ public function get_tax_totals() { $tax_totals = array();
foreach ( $this->get_items( 'tax' ) as $key => $tax ) { $code = $tax->get_rate_code();
if ( ! isset( $tax_totals[ $code ] ) ) { $tax_totals[ $code ] = new stdClass(); $tax_totals[ $code ]->amount = 0; }
$tax_totals[ $code ]->id = $key; $tax_totals[ $code ]->rate_id = $tax->get_rate_id(); $tax_totals[ $code ]->is_compound = $tax->is_compound(); $tax_totals[ $code ]->label = $tax->get_label(); $tax_totals[ $code ]->amount += (float) $tax->get_tax_total() + (float) $tax->get_shipping_tax_total(); $tax_totals[ $code ]->formatted_amount = wc_price( $tax_totals[ $code ]->amount, array( 'currency' => $this->get_currency() ) ); }
if ( apply_filters( 'woocommerce_order_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_order_get_tax_totals', $tax_totals, $this ); }
/** * Get all valid statuses for this order * * @since 3.0.0 * @return array Internal status keys e.g. 'wc-processing' */ protected function get_valid_statuses() { return array_keys( wc_get_order_statuses() ); }
/** * Get user ID. Used by orders, not other order types like refunds. * * @param string $context View or edit context. * @return int */ public function get_user_id( $context = 'view' ) { return 0; }
/** * Get user. Used by orders, not other order types like refunds. * * @return WP_User|false */ public function get_user() { return false; }
/** * Gets information about whether coupon counts were updated. * * @param string $context What the value is for. Valid values are view and edit. * * @return bool True if coupon counts were updated, false otherwise. */ public function get_recorded_coupon_usage_counts( $context = 'view' ) { return wc_string_to_bool( $this->get_prop( 'recorded_coupon_usage_counts', $context ) ); }
/** * Get basic order data in array format. * * @return array */ public function get_base_data() { return array_merge( array( 'id' => $this->get_id() ), $this->data ); }
/** * Get info about the card used for payment in the order. * * @return array */ public function get_payment_card_info() { return PaymentInfo::get_card_info( $this ); }
/* |-------------------------------------------------------------------------- | Setters |-------------------------------------------------------------------------- | | Functions for setting order data. These should not update anything in the | database itself and should only change what is stored in the class | object. However, for backwards compatibility pre 3.0.0 some of these | setters may handle both. */
/** * Set parent order ID. * * @since 3.0.0 * @param int $value Value to set. * @throws WC_Data_Exception Exception thrown if parent ID does not exist or is invalid. */ public function set_parent_id( $value ) { if ( $value && ( $value === $this->get_id() || ! wc_get_order( $value ) ) ) { $this->error( 'order_invalid_parent_id', __( 'Invalid parent ID', 'woocommerce' ) ); } $this->set_prop( 'parent_id', absint( $value ) ); }
/** * Set order status. * * @since 3.0.0 * @param string $new_status Status to change the order to. No internal wc- prefix is required. * @return array details of change */ public function set_status( $new_status ) { $old_status = $this->get_status(); $new_status = OrderUtil::remove_status_prefix( $new_status );
$status_exceptions = array( OrderStatus::AUTO_DRAFT, OrderStatus::TRASH );
// If setting the status, ensure it's set to a valid status. if ( true === $this->object_read ) { // Only allow valid new status. if ( ! in_array( 'wc-' . $new_status, $this->get_valid_statuses(), true ) && ! in_array( $new_status, $status_exceptions, true ) ) { $new_status = OrderStatus::PENDING; }
// If the old status is set but unknown (e.g. draft) assume its pending for action usage. if ( $old_status && ( OrderStatus::AUTO_DRAFT === $old_status || ( ! in_array( 'wc-' . $old_status, $this->get_valid_statuses(), true ) && ! in_array( $old_status, $status_exceptions, true ) ) ) ) { $old_status = OrderStatus::PENDING; } }
$this->set_prop( 'status', $new_status );
return array( 'from' => $old_status, 'to' => $new_status, ); }
/** * Set order_version. * * @param string $value Value to set. * @throws WC_Data_Exception Exception may be thrown if value is invalid. */ public function set_version( $value ) { $this->set_prop( 'version', $value ); }
/** * Set order_currency. * * @param string $value Value to set. * @throws WC_Data_Exception Exception may be thrown if value is invalid. */ public function set_currency( $value ) { if ( $value && ! in_array( $value, array_keys( get_woocommerce_currencies() ), true ) ) { $this->error( 'order_invalid_currency', __( 'Invalid currency code', 'woocommerce' ) ); } $this->set_prop( 'currency', $value ? $value : get_woocommerce_currency() ); }
/** * Set prices_include_tax. * * @param bool $value Value to set. * @throws WC_Data_Exception Exception may be thrown if value is invalid. */ public function set_prices_include_tax( $value ) { $this->set_prop( 'prices_include_tax', (bool) $value ); }
/** * Set date_created. * * @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if there is no date. * @throws WC_Data_Exception Exception may be thrown if value is invalid. */ public function set_date_created( $date = null ) { $this->set_date_prop( 'date_created', $date ); }
/** * Set date_modified. * * @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if there is no date. * @throws WC_Data_Exception Exception may be thrown if value is invalid. */ public function set_date_modified( $date = null ) { $this->set_date_prop( 'date_modified', $date ); }
/** * Set discount_total. * * @param string $value Value to set. * @throws WC_Data_Exception Exception may be thrown if value is invalid. */ public function set_discount_total( $value ) { $this->set_prop( 'discount_total', wc_format_decimal( $value, false, true ) ); }
/** * Set discount_tax. * * @param string $value Value to set. * @throws WC_Data_Exception Exception may be thrown if value is invalid. */ public function set_discount_tax( $value ) { $this->set_prop( 'discount_tax', wc_format_decimal( $value, false, true ) ); }
/** * Set shipping_total. * * @param string $value Value to set. * @throws WC_Data_Exception Exception may be thrown if value is invalid. */ public function set_shipping_total( $value ) { $this->set_prop( 'shipping_total', wc_format_decimal( $value, false, true ) ); }
/** * Set shipping_tax. * * @param string $value Value to set. * @throws WC_Data_Exception Exception may be thrown if value is invalid. */ public function set_shipping_tax( $value ) { $this->set_prop( 'shipping_tax', wc_format_decimal( $value, false, true ) ); $this->set_total_tax( (float) $this->get_cart_tax() + (float) $this->get_shipping_tax() ); }
/** * Set cart tax. * * @param string $value Value to set. * @throws WC_Data_Exception Exception may be thrown if value is invalid. */ public function set_cart_tax( $value ) { $this->set_prop( 'cart_tax', wc_format_decimal( $value, false, true ) ); $this->set_total_tax( (float) $this->get_cart_tax() + (float) $this->get_shipping_tax() ); }
/** * Sets order tax (sum of cart and shipping tax). Used internally only. * * @param string $value Value to set. * @throws WC_Data_Exception Exception may be thrown if value is invalid. */ protected function set_total_tax( $value ) { // We round here because this is a total entry, as opposed to line items in other setters. $this->set_prop( 'total_tax', wc_format_decimal( NumberUtil::round( $value, wc_get_price_decimals() ) ) ); }
/** * Set total. * * @param string $value Value to set. * @param string $deprecated Function used to set different totals based on this. * * @return bool|void * @throws WC_Data_Exception Exception may be thrown if value is invalid. */ public function set_total( $value, $deprecated = '' ) { if ( $deprecated ) { wc_deprecated_argument( 'total_type', '3.0', 'Use dedicated total setter methods instead.' ); return $this->legacy_set_total( $value, $deprecated ); } $this->set_prop( 'total', wc_format_decimal( $value, wc_get_price_decimals() ) ); }
/** * Stores information about whether the coupon usage were counted. * * @param bool|string $value True if counted, false if not. * * @return void */ public function set_recorded_coupon_usage_counts( $value ) { $this->set_prop( 'recorded_coupon_usage_counts', wc_string_to_bool( $value ) ); }
/* |-------------------------------------------------------------------------- | Order Item Handling |-------------------------------------------------------------------------- | | Order items are used for products, taxes, shipping, and fees within | each order. */
/** * Remove all line items (products, coupons, shipping, taxes) from the order. * * @param string $type Order item type. Default null. */ public function remove_order_items( $type = null ) {
/** * Trigger action before removing all order line items. Allows you to track order items. * * @param WC_Order $this The current order object. * @param string $type Order item type. Default null. * * @since 7.8.0 */ do_action( 'woocommerce_remove_order_items', $this, $type ); if ( ! empty( $type ) ) { $this->data_store->delete_items( $this, $type );
$group = $this->type_to_group( $type );
if ( $group ) { unset( $this->items[ $group ] ); } } else { $this->data_store->delete_items( $this ); $this->items = array(); } /** * Trigger action after removing all order line items. * * @param WC_Order $this The current order object. * @param string $type Order item type. Default null. * * @since 7.8.0 */ do_action( 'woocommerce_removed_order_items', $this, $type ); }
/** * Convert a type to a types group. * * @param string $type type to lookup. * @return string */ protected function type_to_group( $type ) { $type_to_group = apply_filters( 'woocommerce_order_type_to_group', $this->item_types_to_group ); return $type_to_group[ $type ] ?? ''; }
/** * Mappings of order item types to groups. * * @var array */ protected array $item_types_to_group = array( 'line_item' => 'line_items', 'tax' => 'tax_lines', 'shipping' => 'shipping_lines', 'fee' => 'fee_lines', 'coupon' => 'coupon_lines', );
/** * Return an array of items/products within this order. * * @param string|array $types Types of line items to get (array or string). * @return WC_Order_Item[] */ public function get_items( $types = 'line_item' ) { $items = array(); $types = array_filter( (array) $types );
foreach ( $types as $type ) { $group = $this->type_to_group( $type );
if ( $group ) { if ( ! isset( $this->items[ $group ] ) ) { $this->items[ $group ] = array_filter( $this->data_store->read_items( $this, $type ) ); } // Don't use array_merge here because keys are numeric. $items = $items + $this->items[ $group ]; } }
return apply_filters( 'woocommerce_order_get_items', $items, $this, $types ); }
/** * Return array of values for calculations. * * @param string $field Field name to return. * * @return array Array of values. */ protected function get_values_for_total( $field ) { $items = array_map( function ( $item ) use ( $field ) { return wc_add_number_precision( $item[ $field ], false ); }, array_values( $this->get_items() ) ); return $items; }
/** * Return an array of coupons within this order. * * @since 3.7.0 * @return WC_Order_Item_Coupon[] */ public function get_coupons() { return $this->get_items( 'coupon' ); }
/** * Return an array of fees within this order. * * @return WC_Order_item_Fee[] */ public function get_fees() { return $this->get_items( 'fee' ); }
/** * Return an array of taxes within this order. * * @return WC_Order_Item_Tax[] */ public function get_taxes() { return $this->get_items( 'tax' ); }
/** * Return an array of shipping costs within this order. * * @return WC_Order_Item_Shipping[] */ public function get_shipping_methods() { return $this->get_items( 'shipping' ); }
/** * Gets formatted shipping method title. * * @return string */ public function get_shipping_method() { $names = array(); foreach ( $this->get_shipping_methods() as $shipping_method ) { $names[] = $shipping_method->get_name(); } return apply_filters( 'woocommerce_order_shipping_method', implode( ', ', $names ), $this ); }
/** * Get used coupon codes only. * * @since 3.7.0 * @return array */ public function get_coupon_codes() { $coupon_codes = array(); $coupons = $this->get_items( 'coupon' );
if ( $coupons ) { foreach ( $coupons as $coupon ) { $coupon_codes[] = $coupon->get_code(); } } return $coupon_codes; }
/** * Gets the count of order items of a certain type. * * @param string $item_type Item type to lookup. * @return int|string */ public function get_item_count( $item_type = '' ) { $items = $this->get_items( empty( $item_type ) ? 'line_item' : $item_type ); $count = 0;
foreach ( $items as $item ) { $count += $item->get_quantity(); }
return apply_filters( 'woocommerce_get_item_count', $count, $item_type, $this ); }
/** * Get an order item object, based on its type. * * @since 3.0.0 * @param int $item_id ID of item to get. * @param bool $load_from_db Prior to 3.2 this item was loaded direct from WC_Order_Factory, not this object. This param is here for backwards compatibility with that. If false, uses the local items variable instead. * @return WC_Order_Item|false */ public function get_item( $item_id, $load_from_db = true ) { if ( $load_from_db ) { return WC_Order_Factory::get_order_item( $item_id ); }
// Search for item id. if ( $this->items ) { foreach ( $this->items as $group => $items ) { if ( isset( $items[ $item_id ] ) ) { return $items[ $item_id ]; } } }
// Load all items of type and cache. $type = $this->data_store->get_order_item_type( $this, $item_id );
if ( ! $type ) { return false; }
$items = $this->get_items( $type );
return ! empty( $items[ $item_id ] ) ? $items[ $item_id ] : false; }
/** * Get key for where a certain item type is stored in _items. * * @since 3.0.0 * @param string $item object Order item (product, shipping, fee, coupon, tax). * @return string */ protected function get_items_key( $item ) { if ( is_a( $item, 'WC_Order_Item_Product' ) ) { return 'line_items'; } elseif ( is_a( $item, 'WC_Order_Item_Fee' ) ) { return 'fee_lines'; } elseif ( is_a( $item, 'WC_Order_Item_Shipping' ) ) { return 'shipping_lines'; } elseif ( is_a( $item, 'WC_Order_Item_Tax' ) ) { return 'tax_lines'; } elseif ( is_a( $item, 'WC_Order_Item_Coupon' ) ) { return 'coupon_lines'; } return apply_filters( 'woocommerce_get_items_key', '', $item ); }
/** * Remove item from the order. * * @param int $item_id Item ID to delete. * @return false|void */ public function remove_item( $item_id ) { $item = $this->get_item( $item_id, false ); $items_key = $item ? $this->get_items_key( $item ) : false;
if ( ! $items_key ) { return false; }
// Unset and remove later. $this->items_to_delete[] = $item; unset( $this->items[ $items_key ][ $item->get_id() ] ); }
/** * Adds an order item to this order. The order item will not persist until save. * * @since 3.0.0 * @param WC_Order_Item $item Order item object (product, shipping, fee, coupon, tax). * @return false|void */ public function add_item( $item ) { $items_key = $this->get_items_key( $item );
if ( ! $items_key ) { return false; }
// Make sure existing items are loaded so we can append this new one. if ( ! isset( $this->items[ $items_key ] ) ) { $this->items[ $items_key ] = $this->get_items( $item->get_type() ); }
// Set parent. $item->set_order_id( $this->get_id() );
// Append new row with generated temporary ID. $item_id = $item->get_id();
if ( $item_id ) { $this->items[ $items_key ][ $item_id ] = $item; } else { $this->items[ $items_key ][ 'new:' . $items_key . count( $this->items[ $items_key ] ) ] = $item; } }
/** * Check and records coupon usage tentatively so that counts validation is correct. Display an error if coupon usage limit has been reached. * * If you are using this method, make sure to `release_held_coupons` in case an Exception is thrown. * * @throws Exception When not able to apply coupon. * * @param string $billing_email Billing email of order. */ public function hold_applied_coupons( $billing_email ) { $held_keys = array(); $held_keys_for_user = array(); $error = null;
try { foreach ( WC()->cart->get_applied_coupons() as $code ) { $coupon = new WC_Coupon( $code ); if ( ! $coupon->get_data_store() ) { continue; }
// Hold coupon for when global coupon usage limit is present. if ( 0 < $coupon->get_usage_limit() ) { $held_key = $this->hold_coupon( $coupon ); if ( $held_key ) { $held_keys[ $coupon->get_id() ] = $held_key; } }
// Hold coupon for when usage limit per customer is enabled. if ( 0 < $coupon->get_usage_limit_per_user() ) {
if ( ! isset( $user_ids_and_emails ) ) { $user_alias = get_current_user_id() ? wp_get_current_user()->ID : sanitize_email( $billing_email ); $user_ids_and_emails = $this->get_billing_and_current_user_aliases( $billing_email ); }
$held_key_for_user = $this->hold_coupon_for_users( $coupon, $user_ids_and_emails, $user_alias );
if ( $held_key_for_user ) { $held_keys_for_user[ $coupon->get_id() ] = $held_key_for_user; } } } } catch ( Exception $e ) { $error = $e; } finally { // Even in case of error, we will save keys for whatever coupons that were held so our data remains accurate. // We save them in bulk instead of one by one for performance reasons. if ( 0 < count( $held_keys_for_user ) || 0 < count( $held_keys ) ) { $this->get_data_store()->set_coupon_held_keys( $this, $held_keys, $held_keys_for_user ); } if ( $error instanceof Exception ) { throw $error; } } }
/** * Hold coupon if a global usage limit is defined. * * @param WC_Coupon $coupon Coupon object. * * @return string Meta key which indicates held coupon. * @throws Exception When can't be held. */ private function hold_coupon( $coupon ) { $result = $coupon->get_data_store()->check_and_hold_coupon( $coupon ); if ( false === $result ) { // translators: Actual coupon code. throw new Exception( sprintf( __( 'An unexpected error happened while applying the Coupon %s.', 'woocommerce' ), esc_html( $coupon->get_code() ) ) ); } elseif ( 0 === $result ) { // translators: Actual coupon code. throw new Exception( sprintf( __( 'Coupon %s was used in another transaction during this checkout, and coupon usage limit is reached. Please remove the coupon and try again.', 'woocommerce' ), esc_html( $coupon->get_code() ) ) ); } return $result; }
/** * Hold coupon if usage limit per customer is defined. * * @param WC_Coupon $coupon Coupon object. * @param array $user_ids_and_emails Array of user Id and emails to check for usage limit. * @param string $user_alias User ID or email to use to record current usage. * * @return string Meta key which indicates held coupon. * @throws Exception When coupon can't be held. */ private function hold_coupon_for_users( $coupon, $user_ids_and_emails, $user_alias ) { $result = $coupon->get_data_store()->check_and_hold_coupon_for_user( $coupon, $user_ids_and_emails, $user_alias ); if ( false === $result ) { // translators: Actual coupon code. throw new Exception( sprintf( __( 'An unexpected error happened while applying the Coupon %s.', 'woocommerce' ), esc_html( $coupon->get_code() ) ) ); } elseif ( 0 === $result ) { // translators: Actual coupon code. throw new Exception( sprintf( __( 'You have used this coupon %s in another transaction during this checkout, and coupon usage limit is reached. Please remove the coupon and try again.', 'woocommerce' ), esc_html( $coupon->get_code() ) ) ); } return $result; }
/** * Helper method to get all aliases for current user and provide billing email. * * @param string $billing_email Billing email provided in form. * * @return array Array of all aliases. * @throws Exception When validation fails. */ private function get_billing_and_current_user_aliases( $billing_email ) { $emails = array( $billing_email ); if ( get_current_user_id() ) { $emails[] = wp_get_current_user()->user_email; } $emails = array_unique( array_map( 'strtolower', array_map( 'sanitize_email', $emails ) ) ); $customer_data_store = WC_Data_Store::load( 'customer' ); $user_ids = $customer_data_store->get_user_ids_for_billing_email( $emails ); return array_merge( $user_ids, $emails ); }
/** * Apply a coupon to the order and recalculate totals. * * @since 3.2.0 * @param string|WC_Coupon $raw_coupon Coupon code or object. * @return true|WP_Error True if applied, error if not. */ public function apply_coupon( $raw_coupon ) { if ( is_a( $raw_coupon, 'WC_Coupon' ) ) { $coupon = $raw_coupon; } elseif ( is_string( $raw_coupon ) ) { $code = wc_format_coupon_code( $raw_coupon ); $coupon = new WC_Coupon( $code );
if ( $coupon->get_code() !== $code ) { return new WP_Error( 'invalid_coupon', __( 'Invalid coupon code', 'woocommerce' ) ); } } else { return new WP_Error( 'invalid_coupon', __( 'Invalid coupon', 'woocommerce' ) ); }
// Check to make sure coupon is not already applied. $applied_coupons = $this->get_items( 'coupon' ); foreach ( $applied_coupons as $applied_coupon ) { if ( $applied_coupon->get_code() === $coupon->get_code() ) { return new WP_Error( 'invalid_coupon', __( 'Coupon code already applied!', 'woocommerce' ) ); } }
$discounts = new WC_Discounts( $this ); $applied = $discounts->apply_coupon( $coupon );
if ( is_wp_error( $applied ) ) { return $applied; }
$data_store = $coupon->get_data_store();
// Check specific for guest checkouts here as well since WC_Cart handles that separately in check_customer_coupons. if ( $data_store && 0 === $this->get_customer_id() ) { $usage_count = $data_store->get_usage_by_email( $coupon, $this->get_billing_email() ); if ( 0 < $coupon->get_usage_limit_per_user() && $usage_count >= $coupon->get_usage_limit_per_user() ) { return new WP_Error( 'invalid_coupon', $coupon->get_coupon_error( 106 ), array( 'status' => 400, ) ); } }
/** * Action to signal that a coupon has been applied to an order. * * @param WC_Coupon $coupon The applied coupon object. * @param WC_Order $order The current order object. * * @since 7.3 */ do_action( 'woocommerce_order_applied_coupon', $coupon, $this );
$this->set_coupon_discount_amounts( $discounts ); $this->save();
// Recalculate totals and taxes. $this->recalculate_coupons();
// Record usage so counts and validation is correct. $used_by = $this->get_user_id();
if ( ! $used_by ) { $used_by = $this->get_billing_email(); }
$order_data_store = $this->get_data_store(); if ( $order_data_store->get_recorded_coupon_usage_counts( $this ) ) { $coupon->increase_usage_count( $used_by ); }
wc_update_coupon_usage_counts( $this->get_id() );
return true; }
/** * Remove a coupon from the order and recalculate totals. * * Coupons affect line item totals, but there is no relationship between * coupon and line total, so to remove a coupon we need to work from the * line subtotal (price before discount) and re-apply all coupons in this * order. * * Manual discounts are not affected; those are separate and do not affect * stored line totals. * * @since 3.2.0 * @since 7.6.0 Returns a boolean indicating success. * * @param string $code Coupon code. * @return bool TRUE if coupon was removed, FALSE otherwise. */ public function remove_coupon( $code ) { $coupons = $this->get_items( 'coupon' );
// Remove the coupon line. foreach ( $coupons as $item_id => $coupon ) { if ( $coupon->get_code() === $code ) { $this->remove_item( $item_id ); $coupon_object = new WC_Coupon( $code ); $coupon_object->decrease_usage_count( $this->get_user_id() ); $this->recalculate_coupons();
return true; } }
return false; }
/** * Apply all coupons in this order again to all line items. * This method is public since WooCommerce 3.8.0. * * @since 3.2.0 */ public function recalculate_coupons() { // Reset line item totals. foreach ( $this->get_items() as $item ) { $item->set_total( $item->get_subtotal() ); $item->set_total_tax( $item->get_subtotal_tax() ); }
$discounts = new WC_Discounts( $this );
foreach ( $this->get_items( 'coupon' ) as $coupon_item ) { $coupon_code = $coupon_item->get_code(); $coupon_id = wc_get_coupon_id_by_code( $coupon_code );
// If we have a coupon ID (loaded via wc_get_coupon_id_by_code) we can simply load the new coupon object using the ID. if ( $coupon_id ) { $coupon_object = new WC_Coupon( $coupon_id );
} else {
// If we do not have a coupon ID (was it virtual? has it been deleted?) we must create a temporary coupon using what data we have stored during checkout. $coupon_object = $this->get_temporary_coupon( $coupon_item ); $coupon_object->set_code( $coupon_code ); $coupon_object->set_virtual( true );
// If there is no coupon amount (maybe dynamic?), set it to the given **discount** amount so the coupon's same value is applied. if ( ! $coupon_object->get_amount() ) {
// If the order originally had prices including tax, remove the discount + discount tax. if ( $this->get_prices_include_tax() ) { $coupon_object->set_amount( (float) $coupon_item->get_discount() + (float) $coupon_item->get_discount_tax() ); } else { $coupon_object->set_amount( $coupon_item->get_discount() ); } $coupon_object->set_discount_type( 'fixed_cart' ); } }
/** * Allow developers to filter this coupon before it gets re-applied to the order. * * @since 3.2.0 */ $coupon_object = apply_filters( 'woocommerce_order_recalculate_coupons_coupon_object', $coupon_object, $coupon_code, $coupon_item, $this );
if ( $coupon_object ) { $discounts->apply_coupon( $coupon_object, false ); } }
$this->set_coupon_discount_amounts( $discounts ); $this->set_item_discount_amounts( $discounts );
// Recalculate totals and taxes. $this->calculate_totals( true ); }
/** * Get a coupon object populated from order line item metadata, to be used when reapplying coupons * if the original coupon no longer exists. * * @since 8.7.0 * * @param WC_Order_Item_Coupon $coupon_item The order item corresponding to the coupon to reapply. * @returns WC_Coupon Coupon object populated from order line item metadata, or empty if no such metadata exists (should never happen). */ private function get_temporary_coupon( WC_Order_Item_Coupon $coupon_item ): WC_Coupon { $coupon_object = new WC_Coupon();
// Since WooCommerce 8.7 a succinct 'coupon_info' line item meta entry is created // whenever a coupon is applied to an order. Previously a more verbose 'coupon_data' was created.
$coupon_info = $coupon_item->get_meta( 'coupon_info', true ); if ( $coupon_info ) { $coupon_object->set_short_info( $coupon_info ); return $coupon_object; }
$coupon_data = $coupon_item->get_meta( 'coupon_data', true ); if ( $coupon_data ) { $coupon_object->set_props( (array) $coupon_data ); }
return $coupon_object; }
/** * After applying coupons via the WC_Discounts class, update line items. * * @since 3.2.0 * @param WC_Discounts $discounts Discounts class. */ protected function set_item_discount_amounts( $discounts ) { $item_discounts = $discounts->get_discounts_by_item(); $tax_location = $this->get_tax_location(); $tax_location = array( $tax_location['country'], $tax_location['state'], $tax_location['postcode'], $tax_location['city'] );
if ( $item_discounts ) { foreach ( $item_discounts as $item_id => $amount ) { $item = $this->get_item( $item_id, false );
// If the prices include tax, discounts should be taken off the tax inclusive prices like in the cart. if ( $this->get_prices_include_tax() && wc_tax_enabled() && 'taxable' === $item->get_tax_status() ) { $taxes = WC_Tax::calc_tax( $amount, $this->get_tax_rates( $item->get_tax_class(), $tax_location ), true );
// Use unrounded taxes so totals will be re-calculated accurately, like in cart. $amount = $amount - array_sum( $taxes ); }
$item->set_total( max( 0, $item->get_total() - $amount ) ); } } }
/** * After applying coupons via the WC_Discounts class, update or create coupon items. * * @since 3.2.0 * @param WC_Discounts $discounts Discounts class. */ protected function set_coupon_discount_amounts( $discounts ) { $coupons = $this->get_items( 'coupon' ); $coupon_code_to_id = wc_list_pluck( $coupons, 'get_id', 'get_code' ); $all_discounts = $discounts->get_discounts(); $coupon_discounts = $discounts->get_discounts_by_coupon(); $tax_location = $this->get_tax_location(); $tax_location = array( $tax_location['country'], $tax_location['state'], $tax_location['postcode'], $tax_location['city'], );
if ( $coupon_discounts ) { foreach ( $coupon_discounts as $coupon_code => $amount ) { $item_id = isset( $coupon_code_to_id[ $coupon_code ] ) ? $coupon_code_to_id[ $coupon_code ] : 0;
if ( ! $item_id ) { $coupon_item = new WC_Order_Item_Coupon(); $coupon_item->set_code( $coupon_code );
// Add coupon data. $coupon_id = wc_get_coupon_id_by_code( $coupon_code ); $coupon = new WC_Coupon( $coupon_id );
$coupon_info = $coupon->get_short_info(); $coupon_item->add_meta_data( 'coupon_info', $coupon_info ); } else { $coupon_item = $this->get_item( $item_id, false ); }
$discount_tax = 0;
// Work out how much tax has been removed as a result of the discount from this coupon. foreach ( $all_discounts[ $coupon_code ] as $item_id => $item_discount_amount ) { $item = $this->get_item( $item_id, false );
if ( 'taxable' !== $item->get_tax_status() || ! wc_tax_enabled() ) { continue; }
$taxes = array_sum( WC_Tax::calc_tax( $item_discount_amount, $this->get_tax_rates( $item->get_tax_class(), $tax_location ), $this->get_prices_include_tax() ) ); if ( 'yes' !== get_option( 'woocommerce_tax_round_at_subtotal' ) ) { $taxes = wc_round_tax_total( $taxes ); }
$discount_tax += $taxes;
if ( $this->get_prices_include_tax() ) { $amount = $amount - $taxes; } }
$coupon_item->set_discount( $amount ); $coupon_item->set_discount_tax( $discount_tax );
$this->add_item( $coupon_item ); } } }
/** * Add a product line item to the order. This is the only line item type with * its own method because it saves looking up order amounts (costs are added up for you). * * @param WC_Product $product Product object. * @param int $qty Quantity to add. * @param array $args Args for the added product. * @return int */ public function add_product( $product, $qty = 1, $args = array() ) { if ( $product ) { $order = ArrayUtil::get_value_or_default( $args, 'order' ); $total = wc_get_price_excluding_tax( $product, array( 'qty' => $qty, 'order' => $order, ) );
$default_args = array( 'name' => $product->get_name(), 'tax_class' => $product->get_tax_class(), 'product_id' => $product->is_type( ProductType::VARIATION ) ? $product->get_parent_id() : $product->get_id(), 'variation_id' => $product->is_type( ProductType::VARIATION ) ? $product->get_id() : 0, 'variation' => $product->is_type( ProductType::VARIATION ) ? $product->get_attributes() : array(), 'subtotal' => $total, 'total' => $total, 'quantity' => $qty, ); } else { $default_args = array( 'quantity' => $qty, ); }
$args = wp_parse_args( $args, $default_args );
// BW compatibility with old args. if ( isset( $args['totals'] ) ) { foreach ( $args['totals'] as $key => $value ) { if ( 'tax' === $key ) { $args['total_tax'] = $value; } elseif ( 'tax_data' === $key ) { $args['taxes'] = $value; } else { $args[ $key ] = $value; } } }
$item = wc_get_container()->get( LegacyProxy::class )->get_instance_of( WC_Order_Item_Product::class ); $item->set_props( $args ); $item->set_backorder_meta(); $item->set_order_id( $this->get_id() ); $item->save(); $this->add_item( $item ); wc_do_deprecated_action( 'woocommerce_order_add_product', array( $this->get_id(), $item->get_id(), $product, $qty, $args ), '3.0', 'woocommerce_new_order_item action instead' ); delete_transient( 'wc_order_' . $this->get_id() . '_needs_processing' ); return $item->get_id(); }
/* |-------------------------------------------------------------------------- | Payment Token Handling |-------------------------------------------------------------------------- | | Payment tokens are hashes used to take payments by certain gateways. | */
/** * Add a payment token to an order * * @since 2.6 * @param WC_Payment_Token $token Payment token object. * @return boolean|int The new token ID or false if it failed. */ public function add_payment_token( $token ) { if ( empty( $token ) || ! ( $token instanceof WC_Payment_Token ) ) { return false; }
$token_ids = $this->data_store->get_payment_token_ids( $this ); $token_ids[] = $token->get_id(); $this->data_store->update_payment_token_ids( $this, $token_ids );
do_action( 'woocommerce_payment_token_added_to_order', $this->get_id(), $token->get_id(), $token, $token_ids ); return $token->get_id(); }
/** * Returns a list of all payment tokens associated with the current order * * @since 2.6 * @return array An array of payment token objects */ public function get_payment_tokens() { return $this->data_store->get_payment_token_ids( $this ); }
/* |-------------------------------------------------------------------------- | Calculations. |-------------------------------------------------------------------------- | | These methods calculate order totals and taxes based on the current data. | */
/** * Calculate shipping total. * * @since 2.2 * @return float */ public function calculate_shipping() { $shipping_total = 0;
foreach ( $this->get_shipping_methods() as $shipping ) { $shipping_total += (float) $shipping->get_total(); }
$this->set_shipping_total( $shipping_total ); $this->save();
return $this->get_shipping_total(); }
/** * Get all tax classes for items in the order. * * @since 2.6.3 * @return array */ public function get_items_tax_classes() { $found_tax_classes = array();
foreach ( $this->get_items() as $item ) { if ( is_callable( array( $item, 'get_tax_status' ) ) && in_array( $item->get_tax_status(), array( 'taxable', 'shipping' ), true ) ) { $found_tax_classes[] = $item->get_tax_class(); } }
return array_unique( $found_tax_classes ); }
/** * Get tax location for this order. * * @since 3.2.0 * @param array $args array Override the location. * @return array */ protected function get_tax_location( $args = array() ) { $tax_based_on = get_option( 'woocommerce_tax_based_on' );
if ( 'shipping' === $tax_based_on && ! $this->get_shipping_country() ) { $tax_based_on = 'billing'; }
$args = wp_parse_args( $args, array( 'country' => 'billing' === $tax_based_on ? $this->get_billing_country() : $this->get_shipping_country(), 'state' => 'billing' === $tax_based_on ? $this->get_billing_state() : $this->get_shipping_state(), 'postcode' => 'billing' === $tax_based_on ? $this->get_billing_postcode() : $this->get_shipping_postcode(), 'city' => 'billing' === $tax_based_on ? $this->get_billing_city() : $this->get_shipping_city(), ) );
/** * Filters whether apply base tax for local pickup shipping method or not. * * @since 6.8.0 * @param boolean apply_base_tax Whether apply base tax for local pickup. Default true. */ $apply_base_tax = true === apply_filters( 'woocommerce_apply_base_tax_for_local_pickup', true );
/** * Filters local pickup shipping methods. * * @since 6.8.0 * @param string[] $local_pickup_methods Local pickup shipping method IDs. */ $local_pickup_methods = apply_filters( 'woocommerce_local_pickup_methods', array( 'legacy_local_pickup', 'local_pickup' ) );
$shipping_method_ids = ArrayUtil::select( $this->get_shipping_methods(), 'get_method_id', ArrayUtil::SELECT_BY_OBJECT_METHOD );
// Set shop base address as a tax location if order has local pickup shipping method. if ( $apply_base_tax && count( array_intersect( $shipping_method_ids, $local_pickup_methods ) ) > 0 ) { $tax_based_on = 'base'; }
// Default to base. if ( 'base' === $tax_based_on || empty( $args['country'] ) ) { $args['country'] = WC()->countries->get_base_country(); $args['state'] = WC()->countries->get_base_state(); $args['postcode'] = WC()->countries->get_base_postcode(); $args['city'] = WC()->countries->get_base_city(); }
return apply_filters( 'woocommerce_order_get_tax_location', $args, $this ); }
/** * Public wrapper for exposing get_tax_location() method, enabling 3rd parties to get the tax location for an order. * * @since 7.6.0 * @param array $args array Override the location. * @return array */ public function get_taxable_location( $args = array() ) { return $this->get_tax_location( $args ); }
/** * Get tax rates for an order. Use order's shipping or billing address, defaults to base location. * * @param string $tax_class Tax class to get rates for. * @param array $location_args Location to compute rates for. Should be in form: array( country, state, postcode, city). * @param object $customer Only used to maintain backward compatibility for filter `woocommerce-matched_rates`. * * @return mixed|void Tax rates. */ protected function get_tax_rates( $tax_class, $location_args = array(), $customer = null ) { $tax_location = $this->get_tax_location( $location_args ); $tax_location = array( $tax_location['country'], $tax_location['state'], $tax_location['postcode'], $tax_location['city'] ); return WC_Tax::get_rates_from_location( $tax_class, $tax_location, $customer ); }
/** * Calculate taxes for all line items and shipping, and store the totals and tax rows. * * If by default the taxes are based on the shipping address and the current order doesn't * have any, it would use the billing address rather than using the Shopping base location. * * Will use the base country unless customer addresses are set. * * @param array $args Added in 3.0.0 to pass things like location. */ public function calculate_taxes( $args = array() ) { do_action( 'woocommerce_order_before_calculate_taxes', $args, $this );
$calculate_tax_for = $this->get_tax_location( $args ); $shipping_tax_class = get_option( 'woocommerce_shipping_tax_class' );
if ( 'inherit' === $shipping_tax_class ) { $found_classes = array_intersect( array_merge( array( '' ), WC_Tax::get_tax_class_slugs() ), $this->get_items_tax_classes() ); $shipping_tax_class = count( $found_classes ) ? current( $found_classes ) : false; }
$is_vat_exempt = apply_filters( 'woocommerce_order_is_vat_exempt', 'yes' === $this->get_meta( 'is_vat_exempt' ), $this );
// Trigger tax recalculation for all items. foreach ( $this->get_items( array( 'line_item', 'fee' ) ) as $item_id => $item ) { if ( ! $is_vat_exempt ) { $item->calculate_taxes( $calculate_tax_for ); } else { $item->set_taxes( false ); } }
foreach ( $this->get_shipping_methods() as $item_id => $item ) { if ( false !== $shipping_tax_class && ! $is_vat_exempt ) { $item->calculate_taxes( array_merge( $calculate_tax_for, array( 'tax_class' => $shipping_tax_class ) ) ); } else { $item->set_taxes( false ); } }
$this->update_taxes(); }
/** * Calculate fees for all line items. * * @return float Fee total. */ public function get_total_fees() { return array_reduce( $this->get_fees(), function ( $carry, $item ) { return $carry + (float) $item->get_total(); }, 0.0 ); }
/** * Update tax lines for the order based on the line item taxes themselves. */ public function update_taxes() { $cart_taxes = array(); $shipping_taxes = array(); $existing_taxes = $this->get_taxes(); $saved_rate_ids = array();
foreach ( $this->get_items( array( 'line_item', 'fee' ) ) as $item_id => $item ) { $taxes = $item->get_taxes(); foreach ( $taxes['total'] as $tax_rate_id => $tax ) { $tax_amount = (float) $this->round_line_tax( $tax, false );
$cart_taxes[ $tax_rate_id ] = isset( $cart_taxes[ $tax_rate_id ] ) ? (float) $cart_taxes[ $tax_rate_id ] + $tax_amount : $tax_amount; } }
foreach ( $this->get_shipping_methods() as $item_id => $item ) { $taxes = $item->get_taxes(); foreach ( $taxes['total'] as $tax_rate_id => $tax ) { $tax_amount = (float) $tax;
if ( 'yes' !== get_option( 'woocommerce_tax_round_at_subtotal' ) ) { $tax_amount = wc_round_tax_total( $tax_amount ); }
$shipping_taxes[ $tax_rate_id ] = isset( $shipping_taxes[ $tax_rate_id ] ) ? (float) $shipping_taxes[ $tax_rate_id ] + $tax_amount : $tax_amount; } }
foreach ( $existing_taxes as $tax ) { // Remove taxes which no longer exist for cart/shipping. if ( ( ! array_key_exists( $tax->get_rate_id(), $cart_taxes ) && ! array_key_exists( $tax->get_rate_id(), $shipping_taxes ) ) || in_array( $tax->get_rate_id(), $saved_rate_ids, true ) ) { $this->remove_item( $tax->get_id() ); continue; } $saved_rate_ids[] = $tax->get_rate_id(); $tax->set_rate( $tax->get_rate_id() ); $tax->set_tax_total( isset( $cart_taxes[ $tax->get_rate_id() ] ) ? $cart_taxes[ $tax->get_rate_id() ] : 0 ); $tax->set_label( WC_Tax::get_rate_label( $tax->get_rate_id() ) ); $tax->set_shipping_tax_total( ! empty( $shipping_taxes[ $tax->get_rate_id() ] ) ? $shipping_taxes[ $tax->get_rate_id() ] : 0 ); $tax->save(); }
$new_rate_ids = wp_parse_id_list( array_diff( array_keys( $cart_taxes + $shipping_taxes ), $saved_rate_ids ) );
// New taxes. foreach ( $new_rate_ids as $tax_rate_id ) { $item = new WC_Order_Item_Tax(); $item->set_rate( $tax_rate_id ); $item->set_tax_total( isset( $cart_taxes[ $tax_rate_id ] ) ? $cart_taxes[ $tax_rate_id ] : 0 ); $item->set_shipping_tax_total( ! empty( $shipping_taxes[ $tax_rate_id ] ) ? $shipping_taxes[ $tax_rate_id ] : 0 ); $this->add_item( $item ); }
$this->set_shipping_tax( array_sum( $shipping_taxes ) ); $this->set_cart_tax( array_sum( $cart_taxes ) ); $this->save(); }
/** * Helper function. * If you add all items in this order in cart again, this would be the cart subtotal (assuming all other settings are same). * * @return float Cart subtotal. */ protected function get_cart_subtotal_for_order() { return wc_remove_number_precision( $this->get_rounded_items_total( $this->get_values_for_total( 'subtotal' ) ) ); }
/** * Helper function. * If you add all items in this order in cart again, this would be the cart total (assuming all other settings are same). * * @return float Cart total. */ protected function get_cart_total_for_order() { return wc_remove_number_precision( $this->get_rounded_items_total( $this->get_values_for_total( 'total' ) ) ); }
/** * Calculate totals by looking at the contents of the order. Stores the totals and returns the orders final total. * * @since 2.2 * @param bool $and_taxes Calc taxes if true. * @return float calculated grand total. */ public function calculate_totals( $and_taxes = true ) { do_action( 'woocommerce_order_before_calculate_totals', $and_taxes, $this );
$fees_total = 0; $shipping_total = 0; $cart_subtotal_tax = 0; $cart_total_tax = 0;
$cart_subtotal = $this->get_cart_subtotal_for_order(); $cart_total = (float) $this->get_cart_total_for_order();
// Sum shipping costs. foreach ( $this->get_shipping_methods() as $shipping ) { $shipping_total += NumberUtil::round( $shipping->get_total(), wc_get_price_decimals() ); }
$this->set_shipping_total( $shipping_total );
// Sum fee costs. foreach ( $this->get_fees() as $item ) { $fee_total = (float) $item->get_total();
if ( 0 > $fee_total ) { $max_discount = NumberUtil::round( $cart_total + $fees_total + $shipping_total, wc_get_price_decimals() ) * -1;
if ( $fee_total < $max_discount && 0 > $max_discount ) { $item->set_total( $max_discount ); } } $fees_total += (float) $item->get_total(); }
// Calculate taxes for items, shipping, discounts. Note; this also triggers save(). if ( $and_taxes ) { $this->calculate_taxes(); }
// Sum taxes again so we can work out how much tax was discounted. This uses original values, not those possibly rounded to 2dp. foreach ( $this->get_items() as $item ) { $taxes = $item->get_taxes();
foreach ( $taxes['total'] as $tax_rate_id => $tax ) { $cart_total_tax += (float) $tax; }
foreach ( $taxes['subtotal'] as $tax_rate_id => $tax ) { $cart_subtotal_tax += (float) $tax; } }
$this->set_discount_total( NumberUtil::round( $cart_subtotal - $cart_total, wc_get_price_decimals() ) ); $this->set_discount_tax( wc_round_tax_total( $cart_subtotal_tax - $cart_total_tax ) ); $this->set_total( NumberUtil::round( $cart_total + $fees_total + (float) $this->get_shipping_total() + (float) $this->get_cart_tax() + (float) $this->get_shipping_tax(), wc_get_price_decimals() ) );
if ( $this->has_cogs() && $this->cogs_is_enabled() ) { $this->calculate_cogs_total_value(); }
do_action( 'woocommerce_order_after_calculate_totals', $and_taxes, $this );
$this->save();
return $this->get_total(); }
/** * Get item subtotal - this is the cost before discount. * * @param object $item Item to get total from. * @param bool $inc_tax (default: false). * @param bool $round (default: true). * @return float */ public function get_item_subtotal( $item, $inc_tax = false, $round = true ) { $subtotal = 0;
if ( is_callable( array( $item, 'get_subtotal' ) ) && $item->get_quantity() ) { if ( $inc_tax ) { $subtotal = ( (float) $item->get_subtotal() + (float) $item->get_subtotal_tax() ) / $item->get_quantity(); } else { $subtotal = ( (float) $item->get_subtotal() ) / $item->get_quantity(); }
$subtotal = $round ? NumberUtil::round( $subtotal, wc_get_price_decimals() ) : $subtotal; }
return apply_filters( 'woocommerce_order_amount_item_subtotal', $subtotal, $this, $item, $inc_tax, $round ); }
/** * Get line subtotal - this is the cost before discount. * * @param object $item Item to get total from. * @param bool $inc_tax (default: false). * @param bool $round (default: true). * @return float */ public function get_line_subtotal( $item, $inc_tax = false, $round = true ) { $subtotal = 0;
if ( is_callable( array( $item, 'get_subtotal' ) ) ) { if ( $inc_tax ) { $subtotal = (float) $item->get_subtotal() + (float) $item->get_subtotal_tax(); } else { $subtotal = (float) $item->get_subtotal(); }
$subtotal = $round ? NumberUtil::round( $subtotal, wc_get_price_decimals() ) : $subtotal; }
return apply_filters( 'woocommerce_order_amount_line_subtotal', $subtotal, $this, $item, $inc_tax, $round ); }
/** * Calculate item cost - useful for gateways. * * @param object $item Item to get total from. * @param bool $inc_tax (default: false). * @param bool $round (default: true). * @return float */ public function get_item_total( $item, $inc_tax = false, $round = true ) { $total = 0;
if ( is_callable( array( $item, 'get_total' ) ) && $item->get_quantity() ) { if ( $inc_tax ) { $total = ( (float) $item->get_total() + (float) $item->get_total_tax() ) / $item->get_quantity(); } else { $total = ( (float) $item->get_total() ) / $item->get_quantity(); }
$total = $round ? NumberUtil::round( $total, wc_get_price_decimals() ) : $total; }
return apply_filters( 'woocommerce_order_amount_item_total', $total, $this, $item, $inc_tax, $round ); }
/** * Calculate line total - useful for gateways. * * @param object $item Item to get total from. * @param bool $inc_tax (default: false). * @param bool $round (default: true). * @return float */ public function get_line_total( $item, $inc_tax = false, $round = true ) { $total = 0;
if ( is_callable( array( $item, 'get_total' ) ) ) { // Check if we need to add line tax to the line total. $total = $inc_tax ? (float) $item->get_total() + (float) $item->get_total_tax() : (float) $item->get_total();
// Check if we need to round. $total = $round ? NumberUtil::round( $total, wc_get_price_decimals() ) : $total; }
return apply_filters( 'woocommerce_order_amount_line_total', $total, $this, $item, $inc_tax, $round ); }
/** * Get item tax - useful for gateways. * * @param mixed $item Item to get total from. * @param bool $round (default: true). * @return float */ public function get_item_tax( $item, $round = true ) { $tax = 0;
if ( is_callable( array( $item, 'get_total_tax' ) ) && $item->get_quantity() ) { $tax = $item->get_total_tax() / $item->get_quantity(); $tax = $round ? wc_round_tax_total( $tax ) : $tax; }
return apply_filters( 'woocommerce_order_amount_item_tax', $tax, $item, $round, $this ); }
/** * Get line tax - useful for gateways. * * @param mixed $item Item to get total from. * @return float */ public function get_line_tax( $item ) { return apply_filters( 'woocommerce_order_amount_line_tax', is_callable( array( $item, 'get_total_tax' ) ) ? wc_round_tax_total( $item->get_total_tax() ) : 0, $item, $this ); }
/** * Gets line subtotal - formatted for display. * * @param object $item Item to get total from. * @param string $tax_display Incl or excl tax display mode. * @return string */ public function get_formatted_line_subtotal( $item, $tax_display = '' ) { $tax_display = $tax_display ? $tax_display : get_option( 'woocommerce_tax_display_cart' );
if ( 'excl' === $tax_display ) { $ex_tax_label = $this->get_prices_include_tax() ? 1 : 0;
$subtotal = wc_price( $this->get_line_subtotal( $item ), array( 'ex_tax_label' => $ex_tax_label, 'currency' => $this->get_currency(), ) ); } else { $subtotal = wc_price( $this->get_line_subtotal( $item, true ), array( 'currency' => $this->get_currency() ) ); }
return apply_filters( 'woocommerce_order_formatted_line_subtotal', $subtotal, $item, $this ); }
/** * Gets order total - formatted for display. * * @return string */ public function get_formatted_order_total() { $formatted_total = wc_price( $this->get_total(), array( 'currency' => $this->get_currency() ) ); return apply_filters( 'woocommerce_get_formatted_order_total', $formatted_total, $this ); }
/** * Gets subtotal - subtotal is shown before discounts, but with localised taxes. * * @param bool $compound (default: false). * @param string $tax_display (default: the tax_display_cart value). * @return string */ public function get_subtotal_to_display( $compound = false, $tax_display = '' ) { $tax_display = $tax_display ? $tax_display : get_option( 'woocommerce_tax_display_cart' ); $subtotal = (float) $this->get_cart_subtotal_for_order();
if ( ! $compound ) {
if ( 'incl' === $tax_display ) { $subtotal_taxes = 0; foreach ( $this->get_items() as $item ) { $subtotal_taxes += self::round_line_tax( (float) $item->get_subtotal_tax(), false ); } $subtotal += wc_round_tax_total( $subtotal_taxes ); }
$subtotal = wc_price( $subtotal, array( 'currency' => $this->get_currency() ) );
if ( 'excl' === $tax_display && $this->get_prices_include_tax() && wc_tax_enabled() ) { $subtotal .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>'; } } else { if ( 'incl' === $tax_display ) { return ''; }
// Add Shipping Costs. $subtotal += (float) $this->get_shipping_total();
// Remove non-compound taxes. foreach ( $this->get_taxes() as $tax ) { if ( $tax->is_compound() ) { continue; } $subtotal = $subtotal + (float) $tax->get_tax_total() + (float) $tax->get_shipping_tax_total(); }
// Remove discounts. $subtotal = $subtotal - (float) $this->get_total_discount(); $subtotal = wc_price( $subtotal, array( 'currency' => $this->get_currency() ) ); }
return apply_filters( 'woocommerce_order_subtotal_to_display', $subtotal, $compound, $this ); }
/** * Gets shipping (formatted). * * @param string $tax_display Excl or incl tax display mode. * @return string */ public function get_shipping_to_display( $tax_display = '' ) { $tax_display = $tax_display ? $tax_display : get_option( 'woocommerce_tax_display_cart' );
if ( 0 < abs( (float) $this->get_shipping_total() ) ) {
if ( 'excl' === $tax_display ) {
// Show shipping excluding tax. $shipping = wc_price( $this->get_shipping_total(), array( 'currency' => $this->get_currency() ) );
if ( (float) $this->get_shipping_tax() > 0 && $this->get_prices_include_tax() ) { $shipping .= apply_filters( 'woocommerce_order_shipping_to_display_tax_label', ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>', $this, $tax_display ); } } else {
// Show shipping including tax. $shipping = wc_price( (float) $this->get_shipping_total() + (float) $this->get_shipping_tax(), array( 'currency' => $this->get_currency() ) );
if ( (float) $this->get_shipping_tax() > 0 && ! $this->get_prices_include_tax() ) { $shipping .= apply_filters( 'woocommerce_order_shipping_to_display_tax_label', ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>', $this, $tax_display ); } }
/* translators: %s: method */ $shipping .= apply_filters( 'woocommerce_order_shipping_to_display_shipped_via', ' <small class="shipped_via">' . sprintf( __( 'via %s', 'woocommerce' ), $this->get_shipping_method() ) . '</small>', $this );
} elseif ( $this->get_shipping_method() ) { $shipping = $this->get_shipping_method(); } else { $shipping = __( 'Free!', 'woocommerce' ); }
return apply_filters( 'woocommerce_order_shipping_to_display', $shipping, $this, $tax_display ); }
/** * Get the discount amount (formatted). * * @since 2.3.0 * @param string $tax_display Excl or incl tax display mode. * @return string */ public function get_discount_to_display( $tax_display = '' ) { $tax_display = $tax_display ? $tax_display : get_option( 'woocommerce_tax_display_cart' );
/** * Filter the discount amount to display. * * @since 2.7.0. */ return apply_filters( 'woocommerce_order_discount_to_display', wc_price( $this->get_total_discount( 'excl' === $tax_display ), array( 'currency' => $this->get_currency() ) ), $this ); }
/** * Add total row for subtotal. * * @param array $total_rows Reference to total rows array. * @param string $tax_display Excl or incl tax display mode. */ protected function add_order_item_totals_subtotal_row( &$total_rows, $tax_display ) { $subtotal = $this->get_subtotal_to_display( false, $tax_display );
if ( $subtotal ) { $total_rows['cart_subtotal'] = array( 'type' => 'subtotal', 'label' => __( 'Subtotal:', 'woocommerce' ), 'value' => $subtotal, ); } }
/** * Add total row for discounts. * * @param array $total_rows Reference to total rows array. * @param string $tax_display Excl or incl tax display mode. */ protected function add_order_item_totals_discount_row( &$total_rows, $tax_display ) { if ( $this->get_total_discount() > 0 ) { $total_rows['discount'] = array( 'type' => 'discount', 'label' => __( 'Discount:', 'woocommerce' ), 'value' => '-' . $this->get_discount_to_display( $tax_display ), ); } }
/** * Add total row for shipping. * * @param array $total_rows Reference to total rows array. * @param string $tax_display Excl or incl tax display mode. */ protected function add_order_item_totals_shipping_row( &$total_rows, $tax_display ) { if ( $this->get_shipping_method() ) { $total_rows['shipping'] = array( 'type' => 'shipping', 'label' => __( 'Shipping:', 'woocommerce' ), 'value' => $this->get_shipping_to_display( $tax_display ), 'meta' => $this->get_shipping_method(), ); } }
/** * Add total row for fees. * * @param array $total_rows Reference to total rows array. * @param string $tax_display Excl or incl tax display mode. */ protected function add_order_item_totals_fee_rows( &$total_rows, $tax_display ) { $fees = $this->get_fees();
if ( $fees ) { foreach ( $fees as $id => $fee ) { if ( apply_filters( 'woocommerce_get_order_item_totals_excl_free_fees', empty( $fee['line_total'] ) && empty( $fee['line_tax'] ), $id ) ) { continue; } $total_rows[ 'fee_' . $fee->get_id() ] = array( 'type' => 'fee', 'label' => $fee->get_name() . ':', 'value' => wc_price( 'excl' === $tax_display ? (float) $fee->get_total() : (float) $fee->get_total() + (float) $fee->get_total_tax(), array( 'currency' => $this->get_currency() ) ), ); } } }
/** * Add total row for taxes. * * @param array $total_rows Reference to total rows array. * @param string $tax_display Excl or incl tax display mode. */ protected function add_order_item_totals_tax_rows( &$total_rows, $tax_display ) { // Tax for tax exclusive prices. if ( 'excl' === $tax_display && wc_tax_enabled() ) { if ( 'itemized' === get_option( 'woocommerce_tax_total_display' ) ) { foreach ( $this->get_tax_totals() as $code => $tax ) { $total_rows[ sanitize_title( $code ) ] = array( 'type' => 'tax', 'label' => $tax->label . ':', 'value' => $tax->formatted_amount, ); } } else { $total_rows['tax'] = array( 'type' => 'tax', 'label' => WC()->countries->tax_or_vat() . ':', 'value' => wc_price( $this->get_total_tax(), array( 'currency' => $this->get_currency() ) ), ); } } }
/** * Add total row for grand total. * * @param array $total_rows Reference to total rows array. * @param string $tax_display Excl or incl tax display mode. */ protected function add_order_item_totals_total_row( &$total_rows, $tax_display ) { $total_rows['order_total'] = array( 'type' => 'total', 'label' => __( 'Total:', 'woocommerce' ), 'value' => $this->get_formatted_order_total( $tax_display ), ); }
/** * Get totals for display on pages and in emails. * * @param mixed $tax_display Excl or incl tax display mode. * @return array */ public function get_order_item_totals( $tax_display = '' ) { $tax_display = $tax_display ? $tax_display : get_option( 'woocommerce_tax_display_cart' ); $total_rows = array();
$this->add_order_item_totals_subtotal_row( $total_rows, $tax_display ); $this->add_order_item_totals_discount_row( $total_rows, $tax_display ); $this->add_order_item_totals_shipping_row( $total_rows, $tax_display ); $this->add_order_item_totals_fee_rows( $total_rows, $tax_display ); $this->add_order_item_totals_tax_rows( $total_rows, $tax_display ); $this->add_order_item_totals_total_row( $total_rows, $tax_display );
return apply_filters( 'woocommerce_get_order_item_totals', $total_rows, $this, $tax_display ); }
/* |-------------------------------------------------------------------------- | Conditionals |-------------------------------------------------------------------------- | | Checks if a condition is true or false. | */
/** * Checks the order status against a passed in status. * * @param array|string $status Status to check. * @return bool */ public function has_status( $status ) { return apply_filters( 'woocommerce_order_has_status', ( is_array( $status ) && in_array( $this->get_status(), $status, true ) ) || $this->get_status() === $status, $this, $status ); }
/** * Check whether this order has a specific shipping method or not. * * @param string $method_id Method ID to check. * @return bool */ public function has_shipping_method( $method_id ) { foreach ( $this->get_shipping_methods() as $shipping_method ) { if ( strpos( $shipping_method->get_method_id(), $method_id ) === 0 ) { return true; } } return false; }
/** * Returns true if the order contains a free product. * * @since 2.5.0 * @return bool */ public function has_free_item() { foreach ( $this->get_items() as $item ) { if ( ! $item->get_total() ) { return true; } } return false; }
/** * Get order title. * * @return string Order title. */ public function get_title(): string { if ( method_exists( $this->data_store, 'get_title' ) ) { return $this->data_store->get_title( $this ); } else { return __( 'Order', 'woocommerce' ); } }
/** * Indicates if the current order has an associated Cost of Goods Sold value. * * Derived classes representing orders that have a COGS value should override this method to return "true". * * @since 9.5.0 * * @return bool True if this order has an associated Cost of Goods Sold value. */ public function has_cogs() { return false; }
/** * Calculate the Cost of Goods Sold value and set it as the actual value for this order. * * @since 9.5.0 * * @return float The calculated value. */ public function calculate_cogs_total_value(): float { if ( ! $this->has_cogs() || ! $this->cogs_is_enabled( __METHOD__ ) ) { return 0; }
$cogs_value = $this->calculate_cogs_total_value_core();
/** * Filter to modify the Cost of Goods Sold value that gets calculated for a given order. * * @since 9.5.0 * * @param float $value The value originally calculated. * @param WC_Abstract_Order $order The order for which the value is calculated. */ $cogs_value = apply_filters( 'woocommerce_calculated_order_cogs_value', $cogs_value, $this );
$this->set_cogs_total_value( $cogs_value );
return $cogs_value; }
/** * Core method to calculate the Cost of Goods Sold value for this order: * it doesn't check if COGS is enabled at class or system level, doesn't fire hooks, and doesn't set the value as the current one for the order. * * @return float The calculated value. */ protected function calculate_cogs_total_value_core(): float { if ( ! $this->has_cogs() || ! $this->cogs_is_enabled( __METHOD__ ) ) { return 0; }
$value = 0; foreach ( array_keys( $this->item_types_to_group ) as $item_type ) { $order_items = $this->get_items( $item_type ); foreach ( $order_items as $item ) { if ( $item->has_cogs() ) { $item->calculate_cogs_value(); $value += $item->get_cogs_value(); } } }
return $value; }
/** * Get the value of the Cost of Goods Sold for this order. * * WARNING! If the Cost of Goods Sold feature is disabled this method will always return zero. * * @return float The current value for this order. */ public function get_cogs_total_value(): float { return (float) ( $this->has_cogs() && $this->cogs_is_enabled( __METHOD__ ) ? $this->get_prop( 'cogs_total_value' ) : 0 ); }
/** * Set the value of the Cost of Goods Sold for this order. * * WARNING! If the Cost of Goods Sold feature is disabled this method will have no effect. * * @param float $value The value to set for this order. * * @internal This method is intended for data store usage only, the value set here will be overridden by calculate_cogs_total_value. */ public function set_cogs_total_value( float $value ) { if ( $this->has_cogs() && $this->cogs_is_enabled( __METHOD__ ) ) { $this->set_prop( 'cogs_total_value', $value ); } } }
|