/
abstract-wc-order.php
1587 lines (1394 loc) · 44.9 KB
/
abstract-wc-order.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
include_once( WC_ABSPATH . 'includes/legacy/abstract-wc-legacy-order.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
* @category Class
* @author WooThemes
*/
abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
/**
* 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,
);
/**
* 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 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 ) {
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 );
}
}
/**
* 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 ) {
// Trigger action before saving to the DB. Allows you to adjust object props before save.
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();
return $this->get_id();
}
/**
* Save all order items which are part of this order.
*/
protected function save_items() {
foreach ( $this->items_to_delete as $item ) {
$item->delete();
}
$this->items_to_delete = array();
// Add/save items.
foreach ( $this->items as $item_group => $items ) {
if ( is_array( $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 ] );
}
}
}
}
}
/*
|--------------------------------------------------------------------------
| Getters
|--------------------------------------------------------------------------
*/
/**
* Get parent order ID.
*
* @since 3.0.0
* @param string $context
* @return integer
*/
public function get_parent_id( $context = 'view' ) {
return $this->get_prop( 'parent_id', $context );
}
/**
* Gets order currency.
*
* @param string $context
* @return string
*/
public function get_currency( $context = 'view' ) {
return $this->get_prop( 'currency', $context );
}
/**
* Get order_version.
*
* @param string $context
* @return string
*/
public function get_version( $context = 'view' ) {
return $this->get_prop( 'version', $context );
}
/**
* Get prices_include_tax.
*
* @param string $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
* @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
* @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 );
}
/**
* Return the order statuses without wc- internal prefix.
*
* @param string $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.
$status = apply_filters( 'woocommerce_default_order_status', 'pending' );
}
return $status;
}
/**
* Get discount_total.
*
* @param string $context
* @return string
*/
public function get_discount_total( $context = 'view' ) {
return $this->get_prop( 'discount_total', $context );
}
/**
* Get discount_tax.
*
* @param string $context
* @return string
*/
public function get_discount_tax( $context = 'view' ) {
return $this->get_prop( 'discount_tax', $context );
}
/**
* Get shipping_total.
*
* @param string $context
* @return string
*/
public function get_shipping_total( $context = 'view' ) {
return $this->get_prop( 'shipping_total', $context );
}
/**
* Get shipping_tax.
*
* @param string $context
* @return string
*/
public function get_shipping_tax( $context = 'view' ) {
return $this->get_prop( 'shipping_tax', $context );
}
/**
* Gets cart tax amount.
*
* @param string $context
* @return float
*/
public function get_cart_tax( $context = 'view' ) {
return $this->get_prop( 'cart_tax', $context );
}
/**
* Gets order grand total. incl. taxes. Used in gateways.
*
* @param string $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
* @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 = $this->get_discount_total();
} else {
$total_discount = $this->get_discount_total() + $this->get_discount_tax();
}
return apply_filters( 'woocommerce_order_get_total_discount', round( $total_discount, WC_ROUNDING_PRECISION ), $this );
}
/**
* Gets order subtotal.
* @return float
*/
public function get_subtotal() {
$subtotal = 0;
foreach ( $this->get_items() as $item ) {
$subtotal += $item->get_subtotal();
}
return apply_filters( 'woocommerce_order_get_subtotal', (double) $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( wc_round_tax_total( $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() );
}
/*
|--------------------------------------------------------------------------
| 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
* @throws WC_Data_Exception
*/
public function set_parent_id( $value ) {
if ( $value && ! 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 = 'wc-' === substr( $new_status, 0, 3 ) ? substr( $new_status, 3 ) : $new_status;
// 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() ) && 'trash' !== $new_status ) {
$new_status = 'pending';
}
// If the old status is set but unknown (e.g. draft) assume its pending for action usage.
if ( $old_status && ! in_array( 'wc-' . $old_status, $this->get_valid_statuses() ) && 'trash' !== $old_status ) {
$old_status = 'pending';
}
}
$this->set_prop( 'status', $new_status );
return array(
'from' => $old_status,
'to' => $new_status,
);
}
/**
* Set order_version.
*
* @param string $value
* @throws WC_Data_Exception
*/
public function set_version( $value ) {
$this->set_prop( 'version', $value );
}
/**
* Set order_currency.
*
* @param string $value
* @throws WC_Data_Exception
*/
public function set_currency( $value ) {
if ( $value && ! in_array( $value, array_keys( get_woocommerce_currencies() ) ) ) {
$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
* @throws WC_Data_Exception
*/
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 their is no date.
* @throws WC_Data_Exception
*/
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 their is no date.
* @throws WC_Data_Exception
*/
public function set_date_modified( $date = null ) {
$this->set_date_prop( 'date_modified', $date );
}
/**
* Set discount_total.
*
* @param string $value
* @throws WC_Data_Exception
*/
public function set_discount_total( $value ) {
$this->set_prop( 'discount_total', wc_format_decimal( $value ) );
}
/**
* Set discount_tax.
*
* @param string $value
* @throws WC_Data_Exception
*/
public function set_discount_tax( $value ) {
$this->set_prop( 'discount_tax', wc_format_decimal( $value ) );
}
/**
* Set shipping_total.
*
* @param string $value
* @throws WC_Data_Exception
*/
public function set_shipping_total( $value ) {
$this->set_prop( 'shipping_total', wc_format_decimal( $value ) );
}
/**
* Set shipping_tax.
*
* @param string $value
* @throws WC_Data_Exception
*/
public function set_shipping_tax( $value ) {
$this->set_prop( 'shipping_tax', wc_format_decimal( $value ) );
$this->set_total_tax( (float) $this->get_cart_tax() + (float) $this->get_shipping_tax() );
}
/**
* Set cart tax.
*
* @param string $value
* @throws WC_Data_Exception
*/
public function set_cart_tax( $value ) {
$this->set_prop( 'cart_tax', wc_format_decimal( $value ) );
$this->set_total_tax( (float) $this->get_cart_tax() + (float) $this->get_shipping_tax() );
}
/**
* Sets order tax (sum of cart and shipping tax). Used internaly only.
*
* @param string $value
* @throws WC_Data_Exception
*/
protected function set_total_tax( $value ) {
$this->set_prop( 'total_tax', wc_format_decimal( $value ) );
}
/**
* Set total.
*
* @param string $value
* @param string $deprecated Function used to set different totals based on this.
* @throws WC_Data_Exception
*/
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() ) );
}
/*
|--------------------------------------------------------------------------
| 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 ) {
if ( ! empty( $type ) ) {
$this->data_store->delete_items( $this, $type );
if ( $group = $this->type_to_group( $type ) ) {
unset( $this->items[ $group ] );
}
} else {
$this->data_store->delete_items( $this );
$this->items = array();
}
}
/**
* Convert a type to a types group.
*
* @param string $type
* @return string group
*/
protected function type_to_group( $type ) {
$type_to_group = apply_filters( 'woocommerce_order_type_to_group', array(
'line_item' => 'line_items',
'tax' => 'tax_lines',
'shipping' => 'shipping_lines',
'fee' => 'fee_lines',
'coupon' => 'coupon_lines',
) );
return isset( $type_to_group[ $type ] ) ? $type_to_group[ $type ] : '';
}
/**
* Return an array of items/products within this order.
*
* @param string|array $types Types of line items to get (array or string).
* @return Array of WC_Order_item
*/
public function get_items( $types = 'line_item' ) {
$items = array();
$types = array_filter( (array) $types );
foreach ( $types as $type ) {
if ( $group = $this->type_to_group( $type ) ) {
if ( ! isset( $this->items[ $group ] ) ) {
$this->items[ $group ] = $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 );
}
/**
* Return an array of fees within this order.
*
* @return array
*/
public function get_fees() {
return $this->get_items( 'fee' );
}
/**
* Return an array of taxes within this order.
*
* @return array
*/
public function get_taxes() {
return $this->get_items( 'tax' );
}
/**
* Return an array of shipping costs within this order.
*
* @return array
*/
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 coupon codes only.
*
* @return array
*/
public function get_used_coupons() {
$coupon_codes = array();
if ( $coupons = $this->get_items( 'coupon' ) ) {
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
* @return 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 it's type.
*
* @since 3.0.0
* @param int $item_id
* @return WC_Order_Item
*/
public function get_item( $item_id ) {
return WC_Order_Factory::get_order_item( $item_id );
}
/**
* Get key for where a certain item type is stored in _items.
*
* @since 3.0.0
* @param $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';
} else {
return '';
}
}
/**
* Remove item from the order.
*
* @param int $item_id
*/
public function remove_item( $item_id ) {
$item = $this->get_item( $item_id );
if ( ! $item || ! ( $items_key = $this->get_items_key( $item ) ) ) {
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 Order item object (product, shipping, fee, coupon, tax)
*/
public function add_item( $item ) {
if ( ! $items_key = $this->get_items_key( $item ) ) {
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
if ( $item_id = $item->get_id() ) {
$this->items[ $items_key ][ $item_id ] = $item;
} else {
$this->items[ $items_key ][ 'new:' . sizeof( $this->items[ $items_key ] ) ] = $item;
}
}
/**
* Add a product line item to the order. This is the only line item type with
* it's own method because it saves looking up order amounts (costs are added up for you).
*
* @param \WC_Product $product
* @param int $qty
* @param array $args
* @return int order item ID
* @throws WC_Data_Exception
*/
public function add_product( $product, $qty = 1, $args = array() ) {
if ( $product ) {
$default_args = array(
'name' => $product->get_name(),
'tax_class' => $product->get_tax_class(),
'product_id' => $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id(),
'variation_id' => $product->is_type( 'variation' ) ? $product->get_id() : 0,
'variation' => $product->is_type( 'variation' ) ? $product->get_attributes() : array(),
'subtotal' => wc_get_price_excluding_tax( $product, array( 'qty' => $qty ) ),
'total' => wc_get_price_excluding_tax( $product, array( 'qty' => $qty ) ),
'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 = new WC_Order_Item_Product();
$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' );
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 += $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 ( $_product = $item->get_product() ) {
$found_tax_classes[] = $_product->get_tax_class();
}
}
return array_unique( $found_tax_classes );
}
/**
* Calculate taxes for all line items and shipping, and store the totals and tax rows.
*
* Will use the base country unless customer addresses are set.
* @param $args array Added in 3.0.0 to pass things like location.
*/
public function calculate_taxes( $args = array() ) {
$tax_based_on = get_option( 'woocommerce_tax_based_on' );
$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(),
) );
// Default to base
if ( 'base' === $tax_based_on || empty( $args['country'] ) ) {
$default = wc_get_base_location();
$args['country'] = $default['country'];
$args['state'] = $default['state'];
$args['postcode'] = '';
$args['city'] = '';