-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
abstract-wc-order.php
2352 lines (2043 loc) · 71.6 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
/**
* 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\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;
/**
* 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 ) {
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 ) {
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->update_if_cached( $this );
}
/**
* 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.
$status = apply_filters( 'woocommerce_default_order_status', '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 = $this->get_discount_total();
} else {
$total_discount = $this->get_discount_total() + $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
);
}
/*
|--------------------------------------------------------------------------
| 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 = 'wc-' === substr( $new_status, 0, 3 ) ? substr( $new_status, 3 ) : $new_status;
$status_exceptions = array( 'auto-draft', '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 = '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(), true ) && ! in_array( $old_status, $status_exceptions, true ) ) {
$old_status = '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 ) {
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();
}
}
/**
* 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',
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 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