Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like WC_Abstract_Order often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WC_Abstract_Order, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { |
||
21 | |||
22 | /** |
||
23 | * Order Data array, with defaults. This is the core order data exposed |
||
24 | * in APIs since 2.7.0. |
||
25 | * |
||
26 | * Notes: |
||
27 | * order_tax = Sum of all taxes. |
||
28 | * cart_tax = cart_tax is the new name for the legacy 'order_tax' which is the tax for items only, not shipping. |
||
29 | * |
||
30 | * @since 2.7.0 |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $_data = array( |
||
34 | 'id' => 0, |
||
35 | 'parent_id' => 0, |
||
36 | 'status' => '', |
||
37 | 'order_key' => '', |
||
38 | 'currency' => '', |
||
39 | 'version' => '', |
||
40 | 'prices_include_tax' => false, |
||
41 | 'date_created' => '', |
||
42 | 'date_modified' => '', |
||
43 | 'customer_id' => 0, |
||
44 | 'discount_total' => 0, |
||
45 | 'discount_tax' => 0, |
||
46 | 'shipping_total' => 0, |
||
47 | 'shipping_tax' => 0, |
||
48 | 'cart_tax' => 0, |
||
49 | 'total' => 0, |
||
50 | 'total_tax' => 0, |
||
51 | ); |
||
52 | |||
53 | /** |
||
54 | * Data stored in meta keys, but not considered "meta" for an order. |
||
55 | * @since 2.7.0 |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $_internal_meta_keys = array( |
||
59 | '_customer_user', '_order_key', '_order_currency', '_cart_discount', |
||
60 | '_cart_discount_tax', '_order_shipping', '_order_shipping_tax', |
||
61 | '_order_tax', '_order_total', '_order_version', '_prices_include_tax', |
||
62 | '_payment_tokens', |
||
63 | ); |
||
64 | |||
65 | /** |
||
66 | * Order items will be stored here, sometimes before they persist in the DB. |
||
67 | * @since 2.7.0 |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $_items = array( |
||
71 | 'line_items' => null, |
||
72 | 'coupon_lines' => null, |
||
73 | 'shipping_lines' => null, |
||
74 | 'fee_lines' => null, |
||
75 | 'tax_lines' => null, |
||
76 | ); |
||
77 | |||
78 | /** |
||
79 | * Order items that need deleting are stored here. |
||
80 | * @since 2.7.0 |
||
81 | * @var array |
||
82 | */ |
||
83 | protected $_items_to_delete = array(); |
||
84 | |||
85 | /** |
||
86 | * Internal meta type used to store order data. |
||
87 | * @var string |
||
88 | */ |
||
89 | protected $_meta_type = 'post'; |
||
90 | |||
91 | /** |
||
92 | * Stores meta in cache for future reads. |
||
93 | * A group must be set to to enable caching. |
||
94 | * @var string |
||
95 | */ |
||
96 | protected $_cache_group = 'order'; |
||
97 | |||
98 | /** |
||
99 | * Get the order if ID is passed, otherwise the order is new and empty. |
||
100 | * This class should NOT be instantiated, but the get_order function or new WC_Order_Factory. |
||
101 | * should be used. It is possible, but the aforementioned are preferred and are the only. |
||
102 | * methods that will be maintained going forward. |
||
103 | * |
||
104 | * @param int|object|WC_Order $order Order to init. |
||
105 | */ |
||
106 | public function __construct( $order = 0 ) { |
||
119 | |||
120 | /* |
||
121 | |-------------------------------------------------------------------------- |
||
122 | | CRUD methods |
||
123 | |-------------------------------------------------------------------------- |
||
124 | | |
||
125 | | Methods which create, read, update and delete orders from the database. |
||
126 | | Written in abstract fashion so that the way orders are stored can be |
||
127 | | changed more easily in the future. |
||
128 | | |
||
129 | | A save method is included for convenience (chooses update or create based |
||
130 | | on if the order exists yet). |
||
131 | | |
||
132 | */ |
||
133 | |||
134 | /** |
||
135 | * Get internal type (post type.) |
||
136 | * @return string |
||
137 | */ |
||
138 | public function get_type() { |
||
141 | |||
142 | /** |
||
143 | * Get a title for the new post type. |
||
144 | */ |
||
145 | protected function get_post_title() { |
||
148 | |||
149 | /** |
||
150 | * Insert data into the database. |
||
151 | * @since 2.7.0 |
||
152 | */ |
||
153 | public function create() { |
||
187 | |||
188 | /** |
||
189 | * Read from the database. |
||
190 | * @since 2.7.0 |
||
191 | * @param int $id ID of object to read. |
||
192 | */ |
||
193 | public function read( $id ) { |
||
220 | |||
221 | /** |
||
222 | * Post meta update wrapper. Sets or deletes based on value. |
||
223 | * @since 2.7.0 |
||
224 | * @return bool Was it changed? |
||
225 | */ |
||
226 | protected function update_post_meta( $key, $value ) { |
||
233 | |||
234 | /** |
||
235 | * Update data in the database. |
||
236 | * @since 2.7.0 |
||
237 | */ |
||
238 | public function update() { |
||
269 | |||
270 | /** |
||
271 | * Delete data from the database. |
||
272 | * @since 2.7.0 |
||
273 | */ |
||
274 | public function delete() { |
||
277 | |||
278 | /** |
||
279 | * Save data to the database. |
||
280 | * @since 2.7.0 |
||
281 | * @return int order ID |
||
282 | */ |
||
283 | public function save() { |
||
298 | |||
299 | /** |
||
300 | * Save all order items which are part of this order. |
||
301 | */ |
||
302 | protected function save_items() { |
||
345 | |||
346 | /* |
||
347 | |-------------------------------------------------------------------------- |
||
348 | | Getters |
||
349 | |-------------------------------------------------------------------------- |
||
350 | | |
||
351 | | Methods for getting data from the order object. |
||
352 | | |
||
353 | */ |
||
354 | |||
355 | /** |
||
356 | * Get all class data in array format. |
||
357 | * @since 2.7.0 |
||
358 | * @return array |
||
359 | */ |
||
360 | public function get_data() { |
||
374 | |||
375 | /** |
||
376 | * Get order ID. |
||
377 | * @since 2.7.0 |
||
378 | * @return integer |
||
379 | */ |
||
380 | public function get_id() { |
||
383 | |||
384 | /** |
||
385 | * Get parent order ID. |
||
386 | * @since 2.7.0 |
||
387 | * @return integer |
||
388 | */ |
||
389 | public function get_parent_id() { |
||
392 | |||
393 | /** |
||
394 | * get_order_number function. |
||
395 | * |
||
396 | * Gets the order number for display (by default, order ID). |
||
397 | * |
||
398 | * @return string |
||
399 | */ |
||
400 | public function get_order_number() { |
||
403 | |||
404 | /** |
||
405 | * Get order key. |
||
406 | * @since 2.7.0 |
||
407 | * @return string |
||
408 | */ |
||
409 | public function get_order_key() { |
||
412 | |||
413 | /** |
||
414 | * Gets order currency. |
||
415 | * @return string |
||
416 | */ |
||
417 | public function get_currency() { |
||
420 | |||
421 | /** |
||
422 | * Get order_version |
||
423 | * @return string |
||
424 | */ |
||
425 | public function get_version() { |
||
428 | |||
429 | /** |
||
430 | * Get prices_include_tax |
||
431 | * @return bool |
||
432 | */ |
||
433 | public function get_prices_include_tax() { |
||
436 | |||
437 | /** |
||
438 | * Get date_created |
||
439 | * @return int |
||
440 | */ |
||
441 | public function get_date_created() { |
||
444 | |||
445 | /** |
||
446 | * Get date_modified |
||
447 | * @return int |
||
448 | */ |
||
449 | public function get_date_modified() { |
||
452 | |||
453 | /** |
||
454 | * Get customer_id |
||
455 | * @return int |
||
456 | */ |
||
457 | public function get_customer_id() { |
||
460 | |||
461 | /** |
||
462 | * Return the order statuses without wc- internal prefix. |
||
463 | * @return string |
||
464 | */ |
||
465 | public function get_status() { |
||
468 | |||
469 | /** |
||
470 | * Alias for get_customer_id(). |
||
471 | * @since 2.2 |
||
472 | * @return int |
||
473 | */ |
||
474 | public function get_user_id() { |
||
477 | |||
478 | /** |
||
479 | * Get the user associated with the order. False for guests. |
||
480 | * |
||
481 | * @since 2.2 |
||
482 | * @return WP_User|false |
||
483 | */ |
||
484 | public function get_user() { |
||
487 | |||
488 | /** |
||
489 | * Get discount_total |
||
490 | * @param bool $raw Gets raw unfiltered value. |
||
491 | * @return string |
||
492 | */ |
||
493 | public function get_discount_total( $raw = false ) { |
||
497 | |||
498 | /** |
||
499 | * Get discount_tax |
||
500 | * @param bool $raw Gets raw unfiltered value. |
||
501 | * @return string |
||
502 | */ |
||
503 | public function get_discount_tax( $raw = false ) { |
||
507 | |||
508 | /** |
||
509 | * Get shipping_total |
||
510 | * @param bool $raw Gets raw unfiltered value. |
||
511 | * @return string |
||
512 | */ |
||
513 | public function get_shipping_total( $raw = false ) { |
||
517 | |||
518 | /** |
||
519 | * Get shipping_tax. |
||
520 | * @param bool $raw Gets raw unfiltered value. |
||
521 | * @return string |
||
522 | */ |
||
523 | public function get_shipping_tax( $raw = false ) { |
||
527 | |||
528 | /** |
||
529 | * Gets cart tax amount. |
||
530 | * @param bool $raw Gets raw unfiltered value. |
||
531 | * @return float |
||
532 | */ |
||
533 | public function get_cart_tax( $raw = false ) { |
||
537 | |||
538 | /** |
||
539 | * Gets order grand total. incl. taxes. Used in gateways. Filtered. |
||
540 | * @param bool $raw Gets raw unfiltered value. |
||
541 | * @return float |
||
542 | */ |
||
543 | public function get_total( $raw = false ) { |
||
547 | |||
548 | /** |
||
549 | * Get total tax amount. Alias for get_order_tax(). |
||
550 | * |
||
551 | * @since 2.7.0 woocommerce_order_amount_total_tax filter has been removed to avoid |
||
552 | * these values being modified and then saved back to the DB. There are |
||
553 | * other, later hooks available to change totals on display. e.g. |
||
554 | * woocommerce_get_order_item_totals. |
||
555 | * @param bool $raw Gets raw unfiltered value. |
||
556 | * @return float |
||
557 | */ |
||
558 | public function get_total_tax( $raw = false ) { |
||
562 | |||
563 | /** |
||
564 | * Gets the total discount amount. |
||
565 | * @param bool $ex_tax Show discount excl any tax. |
||
566 | * @return float |
||
567 | */ |
||
568 | public function get_total_discount( $ex_tax = true ) { |
||
576 | |||
577 | /** |
||
578 | * Gets order subtotal. |
||
579 | * @return float |
||
580 | */ |
||
581 | public function get_subtotal() { |
||
590 | |||
591 | /** |
||
592 | * Get taxes, merged by code, formatted ready for output. |
||
593 | * |
||
594 | * @return array |
||
595 | */ |
||
596 | public function get_tax_totals() { |
||
622 | |||
623 | /* |
||
624 | |-------------------------------------------------------------------------- |
||
625 | | Setters |
||
626 | |-------------------------------------------------------------------------- |
||
627 | | |
||
628 | | Functions for setting order data. These should not update anything in the |
||
629 | | database itself and should only change what is stored in the class |
||
630 | | object. However, for backwards compatibility pre 2.7.0 some of these |
||
631 | | setters may handle both. |
||
632 | | |
||
633 | */ |
||
634 | |||
635 | /** |
||
636 | * Set order ID. |
||
637 | * @since 2.7.0 |
||
638 | * @param int $value |
||
639 | */ |
||
640 | public function set_id( $value ) { |
||
643 | |||
644 | /** |
||
645 | * Set parent order ID. |
||
646 | * @since 2.7.0 |
||
647 | * @param int $value |
||
648 | */ |
||
649 | public function set_parent_id( $value ) { |
||
652 | |||
653 | /** |
||
654 | * Set order status. |
||
655 | * @since 2.7.0 |
||
656 | * @param string $new_status Status to change the order to. No internal wc- prefix is required. |
||
657 | * @param array details of change |
||
658 | */ |
||
659 | public function set_status( $new_status ) { |
||
680 | |||
681 | /** |
||
682 | * Set order_key. |
||
683 | * @param string $value Max length 20 chars. |
||
684 | */ |
||
685 | public function set_order_key( $value ) { |
||
688 | |||
689 | /** |
||
690 | * Set order_version |
||
691 | * @param string $value |
||
692 | */ |
||
693 | public function set_version( $value ) { |
||
696 | |||
697 | /** |
||
698 | * Set order_currency |
||
699 | * @param string $value |
||
700 | */ |
||
701 | public function set_currency( $value ) { |
||
707 | |||
708 | /** |
||
709 | * Set prices_include_tax |
||
710 | * @param bool $value |
||
711 | */ |
||
712 | public function set_prices_include_tax( $value ) { |
||
715 | |||
716 | /** |
||
717 | * Set date_created |
||
718 | * @param string $timestamp Timestamp |
||
719 | */ |
||
720 | public function set_date_created( $timestamp ) { |
||
723 | |||
724 | /** |
||
725 | * Set date_modified |
||
726 | * @param string $timestamp |
||
727 | */ |
||
728 | public function set_date_modified( $timestamp ) { |
||
731 | |||
732 | /** |
||
733 | * Set customer_id |
||
734 | * @param int $value |
||
735 | */ |
||
736 | public function set_customer_id( $value ) { |
||
739 | |||
740 | /** |
||
741 | * Set discount_total |
||
742 | * @param string $value |
||
743 | */ |
||
744 | public function set_discount_total( $value ) { |
||
747 | |||
748 | /** |
||
749 | * Set discount_tax |
||
750 | * @param string $value |
||
751 | */ |
||
752 | public function set_discount_tax( $value ) { |
||
755 | |||
756 | /** |
||
757 | * Set shipping_total |
||
758 | * @param string $value |
||
759 | */ |
||
760 | public function set_shipping_total( $value ) { |
||
763 | |||
764 | /** |
||
765 | * Set shipping_tax |
||
766 | * @param string $value |
||
767 | */ |
||
768 | public function set_shipping_tax( $value ) { |
||
772 | |||
773 | /** |
||
774 | * Set cart tax |
||
775 | * @param string $value |
||
776 | */ |
||
777 | public function set_cart_tax( $value ) { |
||
781 | |||
782 | /** |
||
783 | * Sets order tax (sum of cart and shipping tax). Used internaly only. |
||
784 | * @param string $value |
||
785 | */ |
||
786 | protected function set_total_tax( $value ) { |
||
789 | |||
790 | /** |
||
791 | * Set total |
||
792 | * @param string $value |
||
793 | * @param string $deprecated Function used to set different totals based on this. |
||
794 | */ |
||
795 | public function set_total( $value, $deprecated = '' ) { |
||
802 | |||
803 | /* |
||
804 | |-------------------------------------------------------------------------- |
||
805 | | Order Item Handling |
||
806 | |-------------------------------------------------------------------------- |
||
807 | | |
||
808 | | Order items are used for products, taxes, shipping, and fees within |
||
809 | | each order. |
||
810 | | |
||
811 | */ |
||
812 | |||
813 | /** |
||
814 | * Prefixes an item key with a string so arrays are associative rather than numeric. |
||
815 | * @param int $id |
||
816 | * @return string |
||
817 | */ |
||
818 | protected function prefix_item_id( $id ) { |
||
821 | |||
822 | /** |
||
823 | * Remove all line items (products, coupons, shipping, taxes) from the order. |
||
824 | * @param string $type Order item type. Default null. |
||
825 | */ |
||
826 | public function remove_order_items( $type = null ) { |
||
846 | |||
847 | /** |
||
848 | * Convert a type to a types group. |
||
849 | * @param string $type |
||
850 | * @return string group |
||
851 | */ |
||
852 | protected function type_to_group( $type ) { |
||
862 | |||
863 | /** |
||
864 | * Return an array of items/products within this order. |
||
865 | * @param string|array $types Types of line items to get (array or string). |
||
866 | * @return Array of WC_Order_item |
||
867 | */ |
||
868 | public function get_items( $types = 'line_item' ) { |
||
883 | |||
884 | /** |
||
885 | * Gets items from the database by type. |
||
886 | * @param string $type |
||
887 | * @return array |
||
888 | */ |
||
889 | protected function get_items_from_db( $type ) { |
||
903 | |||
904 | /** |
||
905 | * Return an array of fees within this order. |
||
906 | * @return array |
||
907 | */ |
||
908 | public function get_fees() { |
||
911 | |||
912 | /** |
||
913 | * Return an array of taxes within this order. |
||
914 | * @return array |
||
915 | */ |
||
916 | public function get_taxes() { |
||
919 | |||
920 | /** |
||
921 | * Return an array of shipping costs within this order. |
||
922 | * @return array |
||
923 | */ |
||
924 | public function get_shipping_methods() { |
||
927 | |||
928 | /** |
||
929 | * Gets formatted shipping method title. |
||
930 | * @return string |
||
931 | */ |
||
932 | public function get_shipping_method() { |
||
939 | |||
940 | /** |
||
941 | * Get coupon codes only. |
||
942 | * @return array |
||
943 | */ |
||
944 | public function get_used_coupons() { |
||
953 | |||
954 | /** |
||
955 | * Gets the count of order items of a certain type. |
||
956 | * |
||
957 | * @param string $item_type |
||
958 | * @return string |
||
959 | */ |
||
960 | public function get_item_count( $item_type = '' ) { |
||
970 | |||
971 | /** |
||
972 | * Get an order item object, based on it's type. |
||
973 | * @since 2.7.0 |
||
974 | * @param int $item_id |
||
975 | * @return WC_Order_Item |
||
976 | */ |
||
977 | public function get_item( $item_id ) { |
||
980 | |||
981 | /** |
||
982 | * Get key for where a certain item type is stored in _items. |
||
983 | * @since 2.7.0 |
||
984 | * @param $item object Order item (product, shipping, fee, coupon, tax) |
||
985 | * @return string |
||
986 | */ |
||
987 | protected function get_items_key( $item ) { |
||
1002 | |||
1003 | /** |
||
1004 | * Remove item from the order. |
||
1005 | * @param int $item_id |
||
1006 | */ |
||
1007 | public function remove_item( $item_id ) { |
||
1018 | |||
1019 | /** |
||
1020 | * Adds an order item to this order. The order item will not persist until save. |
||
1021 | * @param object Order item (product, shipping, fee, coupon, tax) |
||
1022 | */ |
||
1023 | public function add_item( $item ) { |
||
1040 | |||
1041 | /** |
||
1042 | * Add a product line item to the order. |
||
1043 | * @param \WC_Product $product |
||
1044 | * @param int $qty |
||
1045 | * @param array $args |
||
1046 | * @return int order item ID |
||
1047 | */ |
||
1048 | public function add_product( $product, $qty = 1, $args = array() ) { |
||
1087 | |||
1088 | /** |
||
1089 | * Add coupon code to the order. |
||
1090 | * @param string $code |
||
1091 | * @param int $discount tax amount. |
||
1092 | * @param int $discount_tax amount. |
||
1093 | * @return int order item ID |
||
1094 | */ |
||
1095 | public function add_coupon( $code = array(), $discount = 0, $discount_tax = 0 ) { |
||
1107 | |||
1108 | /** |
||
1109 | * Add a tax row to the order. |
||
1110 | * @param array $args |
||
1111 | * @param int $deprecated1 2.7.0 tax_rate_id, amount, shipping amount. |
||
1112 | * @param int $deprecated2 2.7.0 tax_rate_id, amount, shipping amount. |
||
1113 | * @return int order item ID |
||
1114 | */ |
||
1115 | public function add_tax( $args = array(), $deprecated1 = 0, $deprecated2 = 0 ) { |
||
1139 | |||
1140 | /** |
||
1141 | * Add a shipping row to the order. |
||
1142 | * @param WC_Shipping_Rate shipping_rate |
||
1143 | * @return int order item ID |
||
1144 | */ |
||
1145 | public function add_shipping( $shipping_rate ) { |
||
1159 | |||
1160 | /** |
||
1161 | * Add a fee to the order. |
||
1162 | * Order must be saved prior to adding items. |
||
1163 | * @param object $fee |
||
1164 | * @return int updated order item ID |
||
1165 | */ |
||
1166 | public function add_fee( $fee ) { |
||
1182 | |||
1183 | /* |
||
1184 | |-------------------------------------------------------------------------- |
||
1185 | | Payment Token Handling |
||
1186 | |-------------------------------------------------------------------------- |
||
1187 | | |
||
1188 | | Payment tokens are hashes used to take payments by certain gateways. |
||
1189 | | |
||
1190 | */ |
||
1191 | |||
1192 | /** |
||
1193 | * Add a payment token to an order |
||
1194 | * |
||
1195 | * @since 2.6 |
||
1196 | * @param WC_Payment_Token $token Payment token object |
||
1197 | * @return boolean|int The new token ID or false if it failed. |
||
1198 | */ |
||
1199 | public function add_payment_token( $token ) { |
||
1216 | |||
1217 | /** |
||
1218 | * Returns a list of all payment tokens associated with the current order |
||
1219 | * |
||
1220 | * @since 2.6 |
||
1221 | * @return array An array of payment token objects |
||
1222 | */ |
||
1223 | public function get_payment_tokens() { |
||
1226 | |||
1227 | /* |
||
1228 | |-------------------------------------------------------------------------- |
||
1229 | | Calculations. |
||
1230 | |-------------------------------------------------------------------------- |
||
1231 | | |
||
1232 | | These methods calculate order totals and taxes based on the current data. |
||
1233 | | |
||
1234 | */ |
||
1235 | |||
1236 | /** |
||
1237 | * Calculate shipping total. |
||
1238 | * |
||
1239 | * @since 2.2 |
||
1240 | * @return float |
||
1241 | */ |
||
1242 | public function calculate_shipping() { |
||
1254 | |||
1255 | /** |
||
1256 | * Get all tax classes for items in the order. |
||
1257 | * |
||
1258 | * @since 2.6.3 |
||
1259 | * @return array |
||
1260 | */ |
||
1261 | public function get_items_tax_classes() { |
||
1272 | |||
1273 | /** |
||
1274 | * Calculate taxes for all line items and shipping, and store the totals and tax rows. |
||
1275 | * |
||
1276 | * Will use the base country unless customer addresses are set. |
||
1277 | * @param $args array Added in 2.7.0 to pass things like location. |
||
1278 | */ |
||
1279 | public function calculate_taxes( $args = array() ) { |
||
1364 | |||
1365 | /** |
||
1366 | * Update tax lines for the order based on the line item taxes themselves. |
||
1367 | */ |
||
1368 | public function update_taxes() { |
||
1403 | |||
1404 | /** |
||
1405 | * Calculate totals by looking at the contents of the order. Stores the totals and returns the orders final total. |
||
1406 | * |
||
1407 | * @since 2.2 |
||
1408 | * @param bool $and_taxes Calc taxes if true. |
||
1409 | * @return float calculated grand total. |
||
1410 | */ |
||
1411 | public function calculate_totals( $and_taxes = true ) { |
||
1445 | |||
1446 | /** |
||
1447 | * Get item subtotal - this is the cost before discount. |
||
1448 | * |
||
1449 | * @param object $item |
||
1450 | * @param bool $inc_tax (default: false). |
||
1451 | * @param bool $round (default: true). |
||
1452 | * @return float |
||
1453 | */ |
||
1454 | View Code Duplication | public function get_item_subtotal( $item, $inc_tax = false, $round = true ) { |
|
1469 | |||
1470 | /** |
||
1471 | * Get line subtotal - this is the cost before discount. |
||
1472 | * |
||
1473 | * @param object $item |
||
1474 | * @param bool $inc_tax (default: false). |
||
1475 | * @param bool $round (default: true). |
||
1476 | * @return float |
||
1477 | */ |
||
1478 | View Code Duplication | public function get_line_subtotal( $item, $inc_tax = false, $round = true ) { |
|
1493 | |||
1494 | /** |
||
1495 | * Calculate item cost - useful for gateways. |
||
1496 | * |
||
1497 | * @param object $item |
||
1498 | * @param bool $inc_tax (default: false). |
||
1499 | * @param bool $round (default: true). |
||
1500 | * @return float |
||
1501 | */ |
||
1502 | View Code Duplication | public function get_item_total( $item, $inc_tax = false, $round = true ) { |
|
1517 | |||
1518 | /** |
||
1519 | * Calculate line total - useful for gateways. |
||
1520 | * |
||
1521 | * @param object $item |
||
1522 | * @param bool $inc_tax (default: false). |
||
1523 | * @param bool $round (default: true). |
||
1524 | * @return float |
||
1525 | */ |
||
1526 | View Code Duplication | public function get_line_total( $item, $inc_tax = false, $round = true ) { |
|
1539 | |||
1540 | /** |
||
1541 | * Get item tax - useful for gateways. |
||
1542 | * |
||
1543 | * @param mixed $item |
||
1544 | * @param bool $round (default: true). |
||
1545 | * @return float |
||
1546 | */ |
||
1547 | public function get_item_tax( $item, $round = true ) { |
||
1557 | |||
1558 | /** |
||
1559 | * Get line tax - useful for gateways. |
||
1560 | * |
||
1561 | * @param mixed $item |
||
1562 | * @return float |
||
1563 | */ |
||
1564 | public function get_line_tax( $item ) { |
||
1567 | |||
1568 | /** |
||
1569 | * Gets line subtotal - formatted for display. |
||
1570 | * |
||
1571 | * @param array $item |
||
1572 | * @param string $tax_display |
||
1573 | * @return string |
||
1574 | */ |
||
1575 | public function get_formatted_line_subtotal( $item, $tax_display = '' ) { |
||
1588 | |||
1589 | /** |
||
1590 | * Gets order total - formatted for display. |
||
1591 | * @return string |
||
1592 | */ |
||
1593 | public function get_formatted_order_total() { |
||
1597 | |||
1598 | /** |
||
1599 | * Gets subtotal - subtotal is shown before discounts, but with localised taxes. |
||
1600 | * |
||
1601 | * @param bool $compound (default: false). |
||
1602 | * @param string $tax_display (default: the tax_display_cart value). |
||
1603 | * @return string |
||
1604 | */ |
||
1605 | public function get_subtotal_to_display( $compound = false, $tax_display = '' ) { |
||
1651 | |||
1652 | /** |
||
1653 | * Gets shipping (formatted). |
||
1654 | * |
||
1655 | * @return string |
||
1656 | */ |
||
1657 | public function get_shipping_to_display( $tax_display = '' ) { |
||
1692 | |||
1693 | /** |
||
1694 | * Get the discount amount (formatted). |
||
1695 | * @since 2.3.0 |
||
1696 | * @return string |
||
1697 | */ |
||
1698 | public function get_discount_to_display( $tax_display = '' ) { |
||
1702 | |||
1703 | /** |
||
1704 | * Get totals for display on pages and in emails. |
||
1705 | * |
||
1706 | * @param mixed $tax_display |
||
1707 | * @return array |
||
1708 | */ |
||
1709 | public function get_order_item_totals( $tax_display = '' ) { |
||
1791 | |||
1792 | /* |
||
1793 | |-------------------------------------------------------------------------- |
||
1794 | | Conditionals |
||
1795 | |-------------------------------------------------------------------------- |
||
1796 | | |
||
1797 | | Checks if a condition is true or false. |
||
1798 | | |
||
1799 | */ |
||
1800 | |||
1801 | /** |
||
1802 | * Checks the order status against a passed in status. |
||
1803 | * |
||
1804 | * @return bool |
||
1805 | */ |
||
1806 | public function has_status( $status ) { |
||
1809 | |||
1810 | /** |
||
1811 | * Check whether this order has a specific shipping method or not. |
||
1812 | * |
||
1813 | * @param string $method_id |
||
1814 | * @return bool |
||
1815 | */ |
||
1816 | public function has_shipping_method( $method_id ) { |
||
1824 | |||
1825 | /** |
||
1826 | * Check if an order key is valid. |
||
1827 | * |
||
1828 | * @param mixed $key |
||
1829 | * @return bool |
||
1830 | */ |
||
1831 | public function key_is_valid( $key ) { |
||
1834 | |||
1835 | /** |
||
1836 | * Returns true if the order contains a free product. |
||
1837 | * @since 2.5.0 |
||
1838 | * @return bool |
||
1839 | */ |
||
1840 | public function has_free_item() { |
||
1848 | } |
||
1849 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.