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 |
||
49 | abstract class WC_Abstract_Order { |
||
50 | |||
51 | /** @public int Order (post) ID. */ |
||
52 | public $id = 0; |
||
53 | |||
54 | /** @var $post WP_Post. */ |
||
55 | public $post = null; |
||
56 | |||
57 | /** @public string Order type. */ |
||
58 | public $order_type = false; |
||
59 | |||
60 | /** @public string Order Date. */ |
||
61 | public $order_date = ''; |
||
62 | |||
63 | /** @public string Order Modified Date. */ |
||
64 | public $modified_date = ''; |
||
65 | |||
66 | /** @public string Customer Message (excerpt). */ |
||
67 | public $customer_message = ''; |
||
68 | |||
69 | /** @public string Customer Note */ |
||
70 | public $customer_note = ''; |
||
71 | |||
72 | /** @public string Order Status. */ |
||
73 | public $post_status = ''; |
||
74 | |||
75 | /** @public bool Do prices include tax? */ |
||
76 | public $prices_include_tax = false; |
||
77 | |||
78 | /** @public string Display mode for taxes in cart. */ |
||
79 | public $tax_display_cart = ''; |
||
80 | |||
81 | /** @public bool Do totals display ex tax? */ |
||
82 | public $display_totals_ex_tax = false; |
||
83 | |||
84 | /** @public bool Do cart prices display ex tax? */ |
||
85 | public $display_cart_ex_tax = false; |
||
86 | |||
87 | /** @protected string Formatted address. Accessed via get_formatted_billing_address(). */ |
||
88 | protected $formatted_billing_address = ''; |
||
89 | |||
90 | /** @protected string Formatted address. Accessed via get_formatted_shipping_address(). */ |
||
91 | protected $formatted_shipping_address = ''; |
||
92 | |||
93 | /** |
||
94 | * Get the order if ID is passed, otherwise the order is new and empty. |
||
95 | * This class should NOT be instantiated, but the get_order function or new WC_Order_Factory. |
||
96 | * should be used. It is possible, but the aforementioned are preferred and are the only. |
||
97 | * methods that will be maintained going forward. |
||
98 | * |
||
99 | * @param int|object|WC_Order $order Order to init. |
||
100 | */ |
||
101 | public function __construct( $order = 0 ) { |
||
108 | |||
109 | /** |
||
110 | * Init/load the order object. Called from the constructor. |
||
111 | * |
||
112 | * @param int|object|WC_Order $order Order to init. |
||
113 | */ |
||
114 | View Code Duplication | protected function init( $order ) { |
|
129 | |||
130 | /** |
||
131 | * Remove all line items (products, coupons, shipping, taxes) from the order. |
||
132 | * |
||
133 | * @param string $type Order item type. Default null. |
||
134 | */ |
||
135 | public function remove_order_items( $type = null ) { |
||
146 | |||
147 | /** |
||
148 | * Returns a list of all payment tokens associated with the current order |
||
149 | * |
||
150 | * @since 2.6 |
||
151 | * @return array An array of payment token objects |
||
152 | */ |
||
153 | public function get_payment_tokens() { |
||
156 | |||
157 | /** |
||
158 | * Add a payment token to an order |
||
159 | * |
||
160 | * @since 2.6 |
||
161 | * @param WC_Payment_Token $token Payment token object |
||
162 | * @return boolean True if the token was added, false if not |
||
163 | */ |
||
164 | public function add_payment_token( $token ) { |
||
179 | |||
180 | /** |
||
181 | * Set the payment method for the order. |
||
182 | * |
||
183 | * @param WC_Payment_Gateway $payment_method |
||
184 | */ |
||
185 | public function set_payment_method( $payment_method ) { |
||
192 | |||
193 | /** |
||
194 | * Set the customer address. |
||
195 | * |
||
196 | * @param array $address Address data. |
||
197 | * @param string $type billing or shipping. |
||
198 | */ |
||
199 | public function set_address( $address, $type = 'billing' ) { |
||
205 | |||
206 | /** |
||
207 | * Returns the requested address in raw, non-formatted way. |
||
208 | * @since 2.4.0 |
||
209 | * @param string $type Billing or shipping. Anything else besides 'billing' will return shipping address. |
||
210 | * @return array The stored address after filter. |
||
211 | */ |
||
212 | public function get_address( $type = 'billing' ) { |
||
244 | |||
245 | /** |
||
246 | * Add a product line item to the order. |
||
247 | * |
||
248 | * @since 2.2 |
||
249 | * @param \WC_Product $product |
||
250 | * @param int $qty Line item quantity. |
||
251 | * @param array $args |
||
252 | * @return int|bool Item ID or false. |
||
253 | */ |
||
254 | public function add_product( $product, $qty = 1, $args = array() ) { |
||
312 | |||
313 | |||
314 | /** |
||
315 | * Update a line item for the order. |
||
316 | * |
||
317 | * Note this does not update order totals. |
||
318 | * |
||
319 | * @since 2.2 |
||
320 | * @param int $item_id order item ID. |
||
321 | * @param array $args data to update. |
||
322 | * @param WC_Product $product |
||
323 | * @return bool |
||
324 | */ |
||
325 | public function update_product( $item_id, $product, $args ) { |
||
368 | |||
369 | |||
370 | /** |
||
371 | * Add coupon code to the order. |
||
372 | * |
||
373 | * @param string $code |
||
374 | * @param int $discount_amount |
||
375 | * @param int $discount_amount_tax "Discounted" tax - used for tax inclusive prices. |
||
376 | * @return int|bool Item ID or false. |
||
377 | */ |
||
378 | public function add_coupon( $code, $discount_amount = 0, $discount_amount_tax = 0 ) { |
||
395 | |||
396 | /** |
||
397 | * Update coupon for order. |
||
398 | * |
||
399 | * Note this does not update order totals. |
||
400 | * |
||
401 | * @since 2.2 |
||
402 | * @param int $item_id |
||
403 | * @param array $args |
||
404 | * @return bool |
||
405 | */ |
||
406 | public function update_coupon( $item_id, $args ) { |
||
428 | |||
429 | /** |
||
430 | * Add a tax row to the order. |
||
431 | * |
||
432 | * @since 2.2 |
||
433 | * @param int tax_rate_id |
||
434 | * @return int|bool Item ID or false. |
||
435 | */ |
||
436 | public function add_tax( $tax_rate_id, $tax_amount = 0, $shipping_tax_amount = 0 ) { |
||
463 | |||
464 | /** |
||
465 | * Add a shipping row to the order. |
||
466 | * |
||
467 | * @param WC_Shipping_Rate shipping_rate |
||
468 | * @return int|bool Item ID or false. |
||
469 | */ |
||
470 | public function add_shipping( $shipping_rate ) { |
||
503 | |||
504 | /** |
||
505 | * Update shipping method for order. |
||
506 | * |
||
507 | * Note this does not update the order total. |
||
508 | * |
||
509 | * @since 2.2 |
||
510 | * @param int $item_id |
||
511 | * @param array $args |
||
512 | * @return bool |
||
513 | */ |
||
514 | public function update_shipping( $item_id, $args ) { |
||
546 | |||
547 | /** |
||
548 | * Add a fee to the order. |
||
549 | * |
||
550 | * @param object $fee |
||
551 | * @return int|bool Item ID or false. |
||
552 | */ |
||
553 | public function add_fee( $fee ) { |
||
581 | |||
582 | /** |
||
583 | * Update fee for order. |
||
584 | * |
||
585 | * Note this does not update order totals. |
||
586 | * |
||
587 | * @since 2.2 |
||
588 | * @param int $item_id |
||
589 | * @param array $args |
||
590 | * @return bool |
||
591 | */ |
||
592 | public function update_fee( $item_id, $args ) { |
||
622 | |||
623 | /** |
||
624 | * Set an order total. |
||
625 | * |
||
626 | * @param float $amount |
||
627 | * @param string $total_type |
||
628 | * |
||
629 | * @return bool |
||
630 | */ |
||
631 | public function set_total( $amount, $total_type = 'total' ) { |
||
657 | |||
658 | /** |
||
659 | * Calculate taxes for all line items and shipping, and store the totals and tax rows. |
||
660 | * |
||
661 | * Will use the base country unless customer addresses are set. |
||
662 | * |
||
663 | * @return bool success or fail. |
||
664 | */ |
||
665 | public function calculate_taxes() { |
||
666 | $tax_total = 0; |
||
667 | $shipping_tax_total = 0; |
||
668 | $taxes = array(); |
||
669 | $shipping_taxes = array(); |
||
670 | $tax_based_on = get_option( 'woocommerce_tax_based_on' ); |
||
671 | |||
672 | // If is_vat_exempt is 'yes', or wc_tax_enabled is false, return and do nothing. |
||
673 | if ( 'yes' === $this->is_vat_exempt || ! wc_tax_enabled() ) { |
||
674 | return false; |
||
675 | } |
||
676 | |||
677 | if ( 'billing' === $tax_based_on ) { |
||
678 | $country = $this->billing_country; |
||
679 | $state = $this->billing_state; |
||
680 | $postcode = $this->billing_postcode; |
||
681 | $city = $this->billing_city; |
||
682 | } elseif ( 'shipping' === $tax_based_on ) { |
||
683 | $country = $this->shipping_country; |
||
684 | $state = $this->shipping_state; |
||
685 | $postcode = $this->shipping_postcode; |
||
686 | $city = $this->shipping_city; |
||
687 | } |
||
688 | |||
689 | // Default to base |
||
690 | View Code Duplication | if ( 'base' === $tax_based_on || empty( $country ) ) { |
|
691 | $default = wc_get_base_location(); |
||
692 | $country = $default['country']; |
||
693 | $state = $default['state']; |
||
694 | $postcode = ''; |
||
695 | $city = ''; |
||
696 | } |
||
697 | |||
698 | // Get items |
||
699 | foreach ( $this->get_items( array( 'line_item', 'fee' ) ) as $item_id => $item ) { |
||
700 | |||
701 | $product = $this->get_product_from_item( $item ); |
||
702 | $line_total = isset( $item['line_total'] ) ? $item['line_total'] : 0; |
||
703 | $line_subtotal = isset( $item['line_subtotal'] ) ? $item['line_subtotal'] : 0; |
||
704 | $tax_class = $item['tax_class']; |
||
705 | $item_tax_status = $product ? $product->get_tax_status() : 'taxable'; |
||
706 | |||
707 | if ( '0' !== $tax_class && 'taxable' === $item_tax_status ) { |
||
708 | |||
709 | $tax_rates = WC_Tax::find_rates( array( |
||
710 | 'country' => $country, |
||
711 | 'state' => $state, |
||
712 | 'postcode' => $postcode, |
||
713 | 'city' => $city, |
||
714 | 'tax_class' => $tax_class |
||
715 | ) ); |
||
716 | |||
717 | $line_subtotal_taxes = WC_Tax::calc_tax( $line_subtotal, $tax_rates, false ); |
||
718 | $line_taxes = WC_Tax::calc_tax( $line_total, $tax_rates, false ); |
||
719 | $line_subtotal_tax = max( 0, array_sum( $line_subtotal_taxes ) ); |
||
720 | $line_tax = max( 0, array_sum( $line_taxes ) ); |
||
721 | $tax_total += $line_tax; |
||
722 | |||
723 | wc_update_order_item_meta( $item_id, '_line_subtotal_tax', wc_format_decimal( $line_subtotal_tax ) ); |
||
724 | wc_update_order_item_meta( $item_id, '_line_tax', wc_format_decimal( $line_tax ) ); |
||
725 | wc_update_order_item_meta( $item_id, '_line_tax_data', array( 'total' => $line_taxes, 'subtotal' => $line_subtotal_taxes ) ); |
||
726 | |||
727 | // Sum the item taxes |
||
728 | View Code Duplication | foreach ( array_keys( $taxes + $line_taxes ) as $key ) { |
|
729 | $taxes[ $key ] = ( isset( $line_taxes[ $key ] ) ? $line_taxes[ $key ] : 0 ) + ( isset( $taxes[ $key ] ) ? $taxes[ $key ] : 0 ); |
||
730 | } |
||
731 | } |
||
732 | } |
||
733 | |||
734 | // Calc taxes for shipping |
||
735 | foreach ( $this->get_shipping_methods() as $item_id => $item ) { |
||
736 | $shipping_tax_class = get_option( 'woocommerce_shipping_tax_class' ); |
||
737 | |||
738 | // Inherit tax class from items |
||
739 | if ( '' === $shipping_tax_class ) { |
||
740 | $tax_classes = WC_Tax::get_tax_classes(); |
||
741 | |||
742 | foreach ( $tax_classes as $tax_class ) { |
||
743 | $tax_class = sanitize_title( $tax_class ); |
||
744 | if ( in_array( $tax_class, $found_tax_classes ) ) { |
||
745 | $tax_rates = WC_Tax::find_shipping_rates( array( |
||
746 | 'country' => $country, |
||
747 | 'state' => $state, |
||
748 | 'postcode' => $postcode, |
||
749 | 'city' => $city, |
||
750 | 'tax_class' => $tax_class, |
||
751 | ) ); |
||
752 | break; |
||
753 | } |
||
754 | } |
||
755 | } else { |
||
756 | $tax_rates = WC_Tax::find_shipping_rates( array( |
||
757 | 'country' => $country, |
||
758 | 'state' => $state, |
||
759 | 'postcode' => $postcode, |
||
760 | 'city' => $city, |
||
761 | 'tax_class' => 'standard' === $shipping_tax_class ? '' : $shipping_tax_class, |
||
762 | ) ); |
||
763 | } |
||
764 | |||
765 | $line_taxes = WC_Tax::calc_tax( $item['cost'], $tax_rates, false ); |
||
766 | $line_tax = max( 0, array_sum( $line_taxes ) ); |
||
767 | $shipping_tax_total += $line_tax; |
||
768 | |||
769 | wc_update_order_item_meta( $item_id, '_line_tax', wc_format_decimal( $line_tax ) ); |
||
770 | wc_update_order_item_meta( $item_id, '_line_tax_data', array( 'total' => $line_taxes ) ); |
||
771 | |||
772 | // Sum the item taxes |
||
773 | View Code Duplication | foreach ( array_keys( $shipping_taxes + $line_taxes ) as $key ) { |
|
774 | $shipping_taxes[ $key ] = ( isset( $line_taxes[ $key ] ) ? $line_taxes[ $key ] : 0 ) + ( isset( $shipping_taxes[ $key ] ) ? $shipping_taxes[ $key ] : 0 ); |
||
775 | } |
||
776 | } |
||
777 | |||
778 | // Save tax totals |
||
779 | $this->set_total( $shipping_tax_total, 'shipping_tax' ); |
||
780 | $this->set_total( $tax_total, 'tax' ); |
||
781 | |||
782 | // Tax rows |
||
783 | $this->remove_order_items( 'tax' ); |
||
784 | |||
785 | // Now merge to keep tax rows |
||
786 | View Code Duplication | foreach ( array_keys( $taxes + $shipping_taxes ) as $tax_rate_id ) { |
|
787 | $this->add_tax( $tax_rate_id, isset( $taxes[ $tax_rate_id ] ) ? $taxes[ $tax_rate_id ] : 0, isset( $shipping_taxes[ $tax_rate_id ] ) ? $shipping_taxes[ $tax_rate_id ] : 0 ); |
||
788 | } |
||
789 | |||
790 | return true; |
||
791 | } |
||
792 | |||
793 | |||
794 | /** |
||
795 | * Calculate shipping total. |
||
796 | * |
||
797 | * @since 2.2 |
||
798 | * @return float |
||
799 | */ |
||
800 | public function calculate_shipping() { |
||
812 | |||
813 | /** |
||
814 | * Update tax lines at order level by looking at the line item taxes themselves. |
||
815 | * |
||
816 | * @return bool success or fail. |
||
817 | */ |
||
818 | public function update_taxes() { |
||
819 | $order_taxes = array(); |
||
820 | $order_shipping_taxes = array(); |
||
821 | |||
822 | foreach ( $this->get_items( array( 'line_item', 'fee' ) ) as $item_id => $item ) { |
||
823 | |||
824 | $line_tax_data = maybe_unserialize( $item['line_tax_data'] ); |
||
825 | |||
826 | View Code Duplication | if ( isset( $line_tax_data['total'] ) ) { |
|
827 | |||
828 | foreach ( $line_tax_data['total'] as $tax_rate_id => $tax ) { |
||
829 | |||
830 | if ( ! isset( $order_taxes[ $tax_rate_id ] ) ) { |
||
831 | $order_taxes[ $tax_rate_id ] = 0; |
||
832 | } |
||
833 | |||
834 | $order_taxes[ $tax_rate_id ] += $tax; |
||
835 | } |
||
836 | } |
||
837 | } |
||
838 | |||
839 | foreach ( $this->get_items( array( 'shipping' ) ) as $item_id => $item ) { |
||
840 | |||
841 | $line_tax_data = maybe_unserialize( $item['taxes'] ); |
||
842 | |||
843 | View Code Duplication | if ( isset( $line_tax_data ) ) { |
|
844 | foreach ( $line_tax_data as $tax_rate_id => $tax ) { |
||
845 | if ( ! isset( $order_shipping_taxes[ $tax_rate_id ] ) ) { |
||
846 | $order_shipping_taxes[ $tax_rate_id ] = 0; |
||
847 | } |
||
848 | |||
849 | $order_shipping_taxes[ $tax_rate_id ] += $tax; |
||
850 | } |
||
851 | } |
||
852 | } |
||
853 | |||
854 | // Remove old existing tax rows. |
||
855 | $this->remove_order_items( 'tax' ); |
||
856 | |||
857 | // Now merge to keep tax rows. |
||
858 | foreach ( array_keys( $order_taxes + $order_shipping_taxes ) as $tax_rate_id ) { |
||
859 | $this->add_tax( $tax_rate_id, isset( $order_taxes[ $tax_rate_id ] ) ? $order_taxes[ $tax_rate_id ] : 0, isset( $order_shipping_taxes[ $tax_rate_id ] ) ? $order_shipping_taxes[ $tax_rate_id ] : 0 ); |
||
860 | } |
||
861 | |||
862 | // Save tax totals |
||
863 | $this->set_total( WC_Tax::round( array_sum( $order_shipping_taxes ) ), 'shipping_tax' ); |
||
864 | $this->set_total( WC_Tax::round( array_sum( $order_taxes ) ), 'tax' ); |
||
865 | |||
866 | return true; |
||
867 | } |
||
868 | |||
869 | /** |
||
870 | * Calculate totals by looking at the contents of the order. Stores the totals and returns the orders final total. |
||
871 | * |
||
872 | * @since 2.2 |
||
873 | * @param bool $and_taxes Calc taxes if true. |
||
874 | * @return float calculated grand total. |
||
875 | */ |
||
876 | public function calculate_totals( $and_taxes = true ) { |
||
910 | |||
911 | /** |
||
912 | * Gets an order from the database. |
||
913 | * |
||
914 | * @param int $id (default: 0). |
||
915 | * @return bool |
||
916 | */ |
||
917 | View Code Duplication | public function get_order( $id = 0 ) { |
|
930 | |||
931 | /** |
||
932 | * Populates an order from the loaded post data. |
||
933 | * |
||
934 | * @param mixed $result |
||
935 | */ |
||
936 | public function populate( $result ) { |
||
954 | |||
955 | /** |
||
956 | * __isset function. |
||
957 | * |
||
958 | * @param mixed $key |
||
959 | * @return bool |
||
960 | */ |
||
961 | public function __isset( $key ) { |
||
969 | |||
970 | /** |
||
971 | * __get function. |
||
972 | * |
||
973 | * @param mixed $key |
||
974 | * @return mixed |
||
975 | */ |
||
976 | public function __get( $key ) { |
||
990 | |||
991 | /** |
||
992 | * Return the order statuses without wc- internal prefix. |
||
993 | * |
||
994 | * Queries get_post_status() directly to avoid having out of date statuses, if updated elsewhere. |
||
995 | * |
||
996 | * @return string |
||
997 | */ |
||
998 | public function get_status() { |
||
1002 | |||
1003 | /** |
||
1004 | * Checks the order status against a passed in status. |
||
1005 | * |
||
1006 | * @return bool |
||
1007 | */ |
||
1008 | public function has_status( $status ) { |
||
1011 | |||
1012 | /** |
||
1013 | * Gets the user ID associated with the order. Guests are 0. |
||
1014 | * |
||
1015 | * @since 2.2 |
||
1016 | * @return int |
||
1017 | */ |
||
1018 | public function get_user_id() { |
||
1021 | |||
1022 | /** |
||
1023 | * Get the user associated with the order. False for guests. |
||
1024 | * |
||
1025 | * @since 2.2 |
||
1026 | * @return WP_User|false |
||
1027 | */ |
||
1028 | public function get_user() { |
||
1031 | |||
1032 | /** |
||
1033 | * Get transaction id for the order. |
||
1034 | * |
||
1035 | * @return string |
||
1036 | */ |
||
1037 | public function get_transaction_id() { |
||
1040 | |||
1041 | /** |
||
1042 | * Check if an order key is valid. |
||
1043 | * |
||
1044 | * @param mixed $key |
||
1045 | * @return bool |
||
1046 | */ |
||
1047 | public function key_is_valid( $key ) { |
||
1055 | |||
1056 | /** |
||
1057 | * get_order_number function. |
||
1058 | * |
||
1059 | * Gets the order number for display (by default, order ID). |
||
1060 | * |
||
1061 | * @return string |
||
1062 | */ |
||
1063 | public function get_order_number() { |
||
1066 | |||
1067 | /** |
||
1068 | * Get a formatted billing address for the order. |
||
1069 | * |
||
1070 | * @return string |
||
1071 | */ |
||
1072 | public function get_formatted_billing_address() { |
||
1093 | |||
1094 | /** |
||
1095 | * Get a formatted shipping address for the order. |
||
1096 | * |
||
1097 | * @return string |
||
1098 | */ |
||
1099 | public function get_formatted_shipping_address() { |
||
1123 | |||
1124 | /** |
||
1125 | * Get a formatted shipping address for the order. |
||
1126 | * |
||
1127 | * @return string |
||
1128 | */ |
||
1129 | public function get_shipping_address_map_url() { |
||
1141 | |||
1142 | /** |
||
1143 | * Get the billing address in an array. |
||
1144 | * @deprecated 2.3 |
||
1145 | * @return string |
||
1146 | */ |
||
1147 | public function get_billing_address() { |
||
1151 | |||
1152 | /** |
||
1153 | * Get the shipping address in an array. |
||
1154 | * @deprecated 2.3 |
||
1155 | * @return string |
||
1156 | */ |
||
1157 | public function get_shipping_address() { |
||
1161 | |||
1162 | /** |
||
1163 | * Get a formatted billing full name. |
||
1164 | * |
||
1165 | * @since 2.4.0 |
||
1166 | * |
||
1167 | * @return string |
||
1168 | */ |
||
1169 | public function get_formatted_billing_full_name() { |
||
1172 | |||
1173 | /** |
||
1174 | * Get a formatted shipping full name. |
||
1175 | * |
||
1176 | * @since 2.4.0 |
||
1177 | * |
||
1178 | * @return string |
||
1179 | */ |
||
1180 | public function get_formatted_shipping_full_name() { |
||
1183 | |||
1184 | /** |
||
1185 | * Return an array of items/products within this order. |
||
1186 | * |
||
1187 | * @param string|array $type Types of line items to get (array or string). |
||
1188 | * @return array |
||
1189 | */ |
||
1190 | public function get_items( $type = '' ) { |
||
1217 | |||
1218 | /** |
||
1219 | * Expand item meta into the $item array. |
||
1220 | * @since 2.4.0 |
||
1221 | * @param array $item before expansion. |
||
1222 | * @return array |
||
1223 | */ |
||
1224 | public function expand_item_meta( $item ) { |
||
1256 | |||
1257 | /** |
||
1258 | * Gets the count of order items of a certain type. |
||
1259 | * |
||
1260 | * @param string $item_type |
||
1261 | * @return string |
||
1262 | */ |
||
1263 | View Code Duplication | public function get_item_count( $item_type = '' ) { |
|
1280 | |||
1281 | /** |
||
1282 | * Get refunds |
||
1283 | * @return array |
||
1284 | */ |
||
1285 | public function get_refunds() { return array(); } |
||
1286 | |||
1287 | /** |
||
1288 | * Return an array of fees within this order. |
||
1289 | * |
||
1290 | * @return array |
||
1291 | */ |
||
1292 | public function get_fees() { |
||
1295 | |||
1296 | /** |
||
1297 | * Return an array of taxes within this order. |
||
1298 | * |
||
1299 | * @return array |
||
1300 | */ |
||
1301 | public function get_taxes() { |
||
1304 | |||
1305 | /** |
||
1306 | * Return an array of shipping costs within this order. |
||
1307 | * |
||
1308 | * @return array |
||
1309 | */ |
||
1310 | public function get_shipping_methods() { |
||
1313 | |||
1314 | /** |
||
1315 | * Check whether this order has a specific shipping method or not. |
||
1316 | * |
||
1317 | * @param string $method_id |
||
1318 | * |
||
1319 | * @return bool |
||
1320 | */ |
||
1321 | public function has_shipping_method( $method_id ) { |
||
1338 | |||
1339 | /** |
||
1340 | * Get taxes, merged by code, formatted ready for output. |
||
1341 | * |
||
1342 | * @return array |
||
1343 | */ |
||
1344 | public function get_tax_totals() { |
||
1345 | |||
1346 | $taxes = $this->get_items( 'tax' ); |
||
1347 | $tax_totals = array(); |
||
1348 | |||
1349 | foreach ( $taxes as $key => $tax ) { |
||
1350 | |||
1351 | $code = $tax[ 'name' ]; |
||
1352 | |||
1353 | View Code Duplication | if ( ! isset( $tax_totals[ $code ] ) ) { |
|
1354 | $tax_totals[ $code ] = new stdClass(); |
||
1355 | $tax_totals[ $code ]->amount = 0; |
||
1356 | } |
||
1357 | |||
1358 | $tax_totals[ $code ]->id = $key; |
||
1359 | $tax_totals[ $code ]->rate_id = $tax['rate_id']; |
||
1360 | $tax_totals[ $code ]->is_compound = $tax[ 'compound' ]; |
||
1361 | $tax_totals[ $code ]->label = isset( $tax[ 'label' ] ) ? $tax[ 'label' ] : $tax[ 'name' ]; |
||
1362 | $tax_totals[ $code ]->amount += $tax[ 'tax_amount' ] + $tax[ 'shipping_tax_amount' ]; |
||
1363 | $tax_totals[ $code ]->formatted_amount = wc_price( wc_round_tax_total( $tax_totals[ $code ]->amount ), array('currency' => $this->get_order_currency()) ); |
||
1364 | } |
||
1365 | |||
1366 | View Code Duplication | if ( apply_filters( 'woocommerce_order_hide_zero_taxes', true ) ) { |
|
1367 | $amounts = array_filter( wp_list_pluck( $tax_totals, 'amount' ) ); |
||
1368 | $tax_totals = array_intersect_key( $tax_totals, $amounts ); |
||
1369 | } |
||
1370 | |||
1371 | return apply_filters( 'woocommerce_order_tax_totals', $tax_totals, $this ); |
||
1372 | } |
||
1373 | |||
1374 | /** |
||
1375 | * has_meta function for order items. |
||
1376 | * |
||
1377 | * @param string $order_item_id |
||
1378 | * @return array of meta data. |
||
1379 | */ |
||
1380 | public function has_meta( $order_item_id ) { |
||
1387 | |||
1388 | /** |
||
1389 | * Get all item meta data in array format in the order it was saved. Does not group meta by key like get_item_meta(). |
||
1390 | * |
||
1391 | * @param mixed $order_item_id |
||
1392 | * @return array of objects |
||
1393 | */ |
||
1394 | public function get_item_meta_array( $order_item_id ) { |
||
1412 | |||
1413 | /** |
||
1414 | * Display meta data belonging to an item. |
||
1415 | * @param array $item |
||
1416 | */ |
||
1417 | public function display_item_meta( $item ) { |
||
1422 | |||
1423 | /** |
||
1424 | * Get order item meta. |
||
1425 | * |
||
1426 | * @param mixed $order_item_id |
||
1427 | * @param string $key (default: '') |
||
1428 | * @param bool $single (default: false) |
||
1429 | * @return array|string |
||
1430 | */ |
||
1431 | public function get_item_meta( $order_item_id, $key = '', $single = false ) { |
||
1434 | |||
1435 | /** Total Getters *******************************************************/ |
||
1436 | |||
1437 | /** |
||
1438 | * Gets the total discount amount. |
||
1439 | * @param bool $ex_tax Show discount excl any tax. |
||
1440 | * @return float |
||
1441 | */ |
||
1442 | public function get_total_discount( $ex_tax = true ) { |
||
1468 | |||
1469 | /** |
||
1470 | * Gets the discount amount. |
||
1471 | * @deprecated in favour of get_total_discount() since we now only have one discount type. |
||
1472 | * @return float |
||
1473 | */ |
||
1474 | public function get_cart_discount() { |
||
1478 | |||
1479 | /** |
||
1480 | * Get cart discount (formatted). |
||
1481 | * |
||
1482 | * @deprecated order (after tax) discounts removed in 2.3.0. |
||
1483 | * @return string |
||
1484 | */ |
||
1485 | public function get_order_discount_to_display() { |
||
1488 | |||
1489 | /** |
||
1490 | * Gets the total (order) discount amount - these are applied after tax. |
||
1491 | * |
||
1492 | * @deprecated order (after tax) discounts removed in 2.3.0. |
||
1493 | * @return float |
||
1494 | */ |
||
1495 | public function get_order_discount() { |
||
1499 | |||
1500 | /** |
||
1501 | * Gets cart tax amount. |
||
1502 | * |
||
1503 | * @return float |
||
1504 | */ |
||
1505 | public function get_cart_tax() { |
||
1508 | |||
1509 | /** |
||
1510 | * Gets shipping tax amount. |
||
1511 | * |
||
1512 | * @return float |
||
1513 | */ |
||
1514 | public function get_shipping_tax() { |
||
1517 | |||
1518 | /** |
||
1519 | * Gets shipping and product tax. |
||
1520 | * |
||
1521 | * @return float |
||
1522 | */ |
||
1523 | public function get_total_tax() { |
||
1526 | |||
1527 | /** |
||
1528 | * Gets shipping total. |
||
1529 | * |
||
1530 | * @return float |
||
1531 | */ |
||
1532 | public function get_total_shipping() { |
||
1535 | |||
1536 | /** |
||
1537 | * Gets order total. |
||
1538 | * |
||
1539 | * @return float |
||
1540 | */ |
||
1541 | public function get_total() { |
||
1544 | |||
1545 | /** |
||
1546 | * Gets order subtotal. |
||
1547 | * |
||
1548 | * @return mixed|void |
||
1549 | */ |
||
1550 | public function get_subtotal() { |
||
1559 | |||
1560 | /** |
||
1561 | * Get item subtotal - this is the cost before discount. |
||
1562 | * |
||
1563 | * @param mixed $item |
||
1564 | * @param bool $inc_tax (default: false). |
||
1565 | * @param bool $round (default: true). |
||
1566 | * @return float |
||
1567 | */ |
||
1568 | public function get_item_subtotal( $item, $inc_tax = false, $round = true ) { |
||
1579 | |||
1580 | /** |
||
1581 | * Get line subtotal - this is the cost before discount. |
||
1582 | * |
||
1583 | * @param mixed $item |
||
1584 | * @param bool $inc_tax (default: false). |
||
1585 | * @param bool $round (default: true). |
||
1586 | * @return float |
||
1587 | */ |
||
1588 | View Code Duplication | public function get_line_subtotal( $item, $inc_tax = false, $round = true ) { |
|
1599 | |||
1600 | /** |
||
1601 | * Calculate item cost - useful for gateways. |
||
1602 | * |
||
1603 | * @param mixed $item |
||
1604 | * @param bool $inc_tax (default: false). |
||
1605 | * @param bool $round (default: true). |
||
1606 | * @return float |
||
1607 | */ |
||
1608 | public function get_item_total( $item, $inc_tax = false, $round = true ) { |
||
1622 | |||
1623 | /** |
||
1624 | * Calculate line total - useful for gateways. |
||
1625 | * |
||
1626 | * @param mixed $item |
||
1627 | * @param bool $inc_tax (default: false). |
||
1628 | * @param bool $round (default: true). |
||
1629 | * @return float |
||
1630 | */ |
||
1631 | View Code Duplication | public function get_line_total( $item, $inc_tax = false, $round = true ) { |
|
1641 | |||
1642 | /** |
||
1643 | * Calculate item tax - useful for gateways. |
||
1644 | * |
||
1645 | * @param mixed $item |
||
1646 | * @param bool $round (default: true). |
||
1647 | * @return float |
||
1648 | */ |
||
1649 | public function get_item_tax( $item, $round = true ) { |
||
1656 | |||
1657 | /** |
||
1658 | * Calculate line tax - useful for gateways. |
||
1659 | * |
||
1660 | * @param mixed $item |
||
1661 | * @return float |
||
1662 | */ |
||
1663 | public function get_line_tax( $item ) { |
||
1666 | |||
1667 | /** End Total Getters *******************************************************/ |
||
1668 | |||
1669 | /** |
||
1670 | * Gets formatted shipping method title. |
||
1671 | * |
||
1672 | * @return string |
||
1673 | */ |
||
1674 | public function get_shipping_method() { |
||
1693 | |||
1694 | /** |
||
1695 | * Gets line subtotal - formatted for display. |
||
1696 | * |
||
1697 | * @param array $item |
||
1698 | * @param string $tax_display |
||
1699 | * @return string |
||
1700 | */ |
||
1701 | public function get_formatted_line_subtotal( $item, $tax_display = '' ) { |
||
1721 | |||
1722 | /** |
||
1723 | * Gets order currency. |
||
1724 | * |
||
1725 | * @return string |
||
1726 | */ |
||
1727 | public function get_order_currency() { |
||
1730 | |||
1731 | /** |
||
1732 | * Gets order total - formatted for display. |
||
1733 | * |
||
1734 | * @return string |
||
1735 | */ |
||
1736 | public function get_formatted_order_total() { |
||
1740 | |||
1741 | /** |
||
1742 | * Gets subtotal - subtotal is shown before discounts, but with localised taxes. |
||
1743 | * |
||
1744 | * @param bool $compound (default: false). |
||
1745 | * @param string $tax_display (default: the tax_display_cart value). |
||
1746 | * @return string |
||
1747 | */ |
||
1748 | public function get_subtotal_to_display( $compound = false, $tax_display = '' ) { |
||
1810 | |||
1811 | |||
1812 | /** |
||
1813 | * Gets shipping (formatted). |
||
1814 | * |
||
1815 | * @return string |
||
1816 | */ |
||
1817 | public function get_shipping_to_display( $tax_display = '' ) { |
||
1854 | |||
1855 | /** |
||
1856 | * Get the discount amount (formatted). |
||
1857 | * @since 2.3.0 |
||
1858 | * @return string |
||
1859 | */ |
||
1860 | public function get_discount_to_display( $tax_display = '' ) { |
||
1866 | |||
1867 | /** |
||
1868 | * Get cart discount (formatted). |
||
1869 | * @deprecated |
||
1870 | * @return string |
||
1871 | */ |
||
1872 | public function get_cart_discount_to_display( $tax_display = '' ) { |
||
1876 | |||
1877 | /** |
||
1878 | * Get a product (either product or variation). |
||
1879 | * |
||
1880 | * @param mixed $item |
||
1881 | * @return WC_Product |
||
1882 | */ |
||
1883 | public function get_product_from_item( $item ) { |
||
1895 | |||
1896 | |||
1897 | /** |
||
1898 | * Get totals for display on pages and in emails. |
||
1899 | * |
||
1900 | * @param mixed $tax_display |
||
1901 | * @return array |
||
1902 | */ |
||
1903 | public function get_order_item_totals( $tax_display = '' ) { |
||
2001 | |||
2002 | |||
2003 | /** |
||
2004 | * Output items for display in html emails. |
||
2005 | * |
||
2006 | * @param array $args Items args. |
||
2007 | * @param null $deprecated1 Deprecated arg. |
||
2008 | * @param null $deprecated2 Deprecated arg. |
||
2009 | * @param null $deprecated3 Deprecated arg. |
||
2010 | * @param null $deprecated4 Deprecated arg. |
||
2011 | * @param null $deprecated5 Deprecated arg. |
||
2012 | * @return string |
||
2013 | */ |
||
2014 | public function email_order_items_table( $args = array(), $deprecated1 = null, $deprecated2 = null, $deprecated3 = null, $deprecated4 = null, $deprecated5 = null ) { |
||
2046 | |||
2047 | /** |
||
2048 | * Returns if an order has been paid for based on the order status. |
||
2049 | * @since 2.5.0 |
||
2050 | * @return bool |
||
2051 | */ |
||
2052 | public function is_paid() { |
||
2055 | |||
2056 | /** |
||
2057 | * Checks if product download is permitted. |
||
2058 | * |
||
2059 | * @return bool |
||
2060 | */ |
||
2061 | public function is_download_permitted() { |
||
2064 | |||
2065 | /** |
||
2066 | * Returns true if the order contains a downloadable product. |
||
2067 | * @return bool |
||
2068 | */ |
||
2069 | public function has_downloadable_item() { |
||
2079 | |||
2080 | /** |
||
2081 | * Returns true if the order contains a free product. |
||
2082 | * @since 2.5.0 |
||
2083 | * @return bool |
||
2084 | */ |
||
2085 | public function has_free_item() { |
||
2093 | |||
2094 | /** |
||
2095 | * Generates a URL so that a customer can pay for their (unpaid - pending) order. Pass 'true' for the checkout version which doesn't offer gateway choices. |
||
2096 | * |
||
2097 | * @param bool $on_checkout |
||
2098 | * @return string |
||
2099 | */ |
||
2100 | public function get_checkout_payment_url( $on_checkout = false ) { |
||
2116 | |||
2117 | /** |
||
2118 | * Generates a URL for the thanks page (order received). |
||
2119 | * |
||
2120 | * @return string |
||
2121 | */ |
||
2122 | public function get_checkout_order_received_url() { |
||
2123 | |||
2124 | $order_received_url = wc_get_endpoint_url( 'order-received', $this->id, wc_get_page_permalink( 'checkout' ) ); |
||
2125 | |||
2126 | if ( 'yes' == get_option( 'woocommerce_force_ssl_checkout' ) || is_ssl() ) { |
||
2127 | $order_received_url = str_replace( 'http:', 'https:', $order_received_url ); |
||
2128 | } |
||
2129 | |||
2130 | $order_received_url = add_query_arg( 'key', $this->order_key, $order_received_url ); |
||
2131 | |||
2132 | return apply_filters( 'woocommerce_get_checkout_order_received_url', $order_received_url, $this ); |
||
2133 | } |
||
2134 | |||
2135 | /** |
||
2136 | * Generates a URL so that a customer can cancel their (unpaid - pending) order. |
||
2137 | * |
||
2138 | * @param string $redirect |
||
2139 | * |
||
2140 | * @return string |
||
2141 | */ |
||
2142 | View Code Duplication | public function get_cancel_order_url( $redirect = '' ) { |
|
2154 | |||
2155 | /** |
||
2156 | * Generates a raw (unescaped) cancel-order URL for use by payment gateways. |
||
2157 | * |
||
2158 | * @param string $redirect |
||
2159 | * @return string The unescaped cancel-order URL. |
||
2160 | */ |
||
2161 | View Code Duplication | public function get_cancel_order_url_raw( $redirect = '' ) { |
|
2173 | |||
2174 | |||
2175 | /** |
||
2176 | * Helper method to return the cancel endpoint. |
||
2177 | * |
||
2178 | * @return string the cancel endpoint; either the cart page or the home page. |
||
2179 | */ |
||
2180 | public function get_cancel_endpoint() { |
||
2193 | |||
2194 | |||
2195 | /** |
||
2196 | * Generates a URL to view an order from the my account page. |
||
2197 | * |
||
2198 | * @return string |
||
2199 | */ |
||
2200 | public function get_view_order_url() { |
||
2206 | |||
2207 | /** |
||
2208 | * Get the downloadable files for an item in this order. |
||
2209 | * |
||
2210 | * @param array $item |
||
2211 | * @return array |
||
2212 | */ |
||
2213 | public function get_item_downloads( $item ) { |
||
2246 | |||
2247 | /** |
||
2248 | * Display download links for an order item. |
||
2249 | * @param array $item |
||
2250 | */ |
||
2251 | public function display_item_downloads( $item ) { |
||
2268 | |||
2269 | /** |
||
2270 | * Get the Download URL. |
||
2271 | * |
||
2272 | * @param int $product_id |
||
2273 | * @param int $download_id |
||
2274 | * @return string |
||
2275 | */ |
||
2276 | public function get_download_url( $product_id, $download_id ) { |
||
2284 | |||
2285 | /** |
||
2286 | * Adds a note (comment) to the order. |
||
2287 | * |
||
2288 | * @param string $note Note to add. |
||
2289 | * @param int $is_customer_note (default: 0) Is this a note for the customer? |
||
2290 | * @param bool added_by_user Was the note added by a user? |
||
2291 | * @return int Comment ID. |
||
2292 | */ |
||
2293 | public function add_order_note( $note, $is_customer_note = 0, $added_by_user = false ) { |
||
2324 | |||
2325 | /** |
||
2326 | * Updates status of order. |
||
2327 | * |
||
2328 | * @param string $new_status Status to change the order to. No internal wc- prefix is required. |
||
2329 | * @param string $note (default: '') Optional note to add. |
||
2330 | * @param bool $manual is this a manual order status change? |
||
2331 | * @return bool Successful change or not |
||
2332 | */ |
||
2333 | public function update_status( $new_status, $note = '', $manual = false ) { |
||
2419 | |||
2420 | |||
2421 | /** |
||
2422 | * Cancel the order and restore the cart (before payment). |
||
2423 | * |
||
2424 | * @param string $note (default: '') Optional note to add. |
||
2425 | */ |
||
2426 | public function cancel_order( $note = '' ) { |
||
2430 | |||
2431 | /** |
||
2432 | * When a payment is complete this function is called. |
||
2433 | * |
||
2434 | * Most of the time this should mark an order as 'processing' so that admin can process/post the items. |
||
2435 | * If the cart contains only downloadable items then the order is 'completed' since the admin needs to take no action. |
||
2436 | * Stock levels are reduced at this point. |
||
2437 | * Sales are also recorded for products. |
||
2438 | * Finally, record the date of payment. |
||
2439 | * |
||
2440 | * @param string $transaction_id Optional transaction id to store in post meta. |
||
2441 | */ |
||
2442 | public function payment_complete( $transaction_id = '' ) { |
||
2488 | |||
2489 | |||
2490 | /** |
||
2491 | * Record sales. |
||
2492 | */ |
||
2493 | public function record_product_sales() { |
||
2522 | |||
2523 | |||
2524 | /** |
||
2525 | * Get coupon codes only. |
||
2526 | * |
||
2527 | * @return array |
||
2528 | */ |
||
2529 | public function get_used_coupons() { |
||
2540 | |||
2541 | |||
2542 | /** |
||
2543 | * Increase applied coupon counts. |
||
2544 | */ |
||
2545 | View Code Duplication | public function increase_coupon_usage_counts() { |
|
2571 | |||
2572 | |||
2573 | /** |
||
2574 | * Decrease applied coupon counts. |
||
2575 | */ |
||
2576 | View Code Duplication | public function decrease_coupon_usage_counts() { |
|
2603 | |||
2604 | /** |
||
2605 | * Reduce stock levels for all line items in the order. |
||
2606 | * Runs if stock management is enabled, but can be disabled on per-order basis by extensions @since 2.4.0 via woocommerce_can_reduce_order_stock hook. |
||
2607 | */ |
||
2608 | public function reduce_order_stock() { |
||
2634 | |||
2635 | /** |
||
2636 | * Send the stock notifications. |
||
2637 | * |
||
2638 | * @param WC_Product $product |
||
2639 | * @param int $new_stock |
||
2640 | * @param int $qty_ordered |
||
2641 | */ |
||
2642 | public function send_stock_notifications( $product, $new_stock, $qty_ordered ) { |
||
2663 | |||
2664 | |||
2665 | /** |
||
2666 | * List order notes (public) for the customer. |
||
2667 | * |
||
2668 | * @return array |
||
2669 | */ |
||
2670 | public function get_customer_order_notes() { |
||
2694 | |||
2695 | /** |
||
2696 | * Checks if an order needs payment, based on status and order total. |
||
2697 | * |
||
2698 | * @return bool |
||
2699 | */ |
||
2700 | public function needs_payment() { |
||
2712 | |||
2713 | /** |
||
2714 | * Checks if an order needs display the shipping address, based on shipping method. |
||
2715 | * |
||
2716 | * @return boolean |
||
2717 | */ |
||
2718 | public function needs_shipping_address() { |
||
2735 | |||
2736 | /** |
||
2737 | * Checks if an order can be edited, specifically for use on the Edit Order screen. |
||
2738 | * |
||
2739 | * @return bool |
||
2740 | */ |
||
2741 | public function is_editable() { |
||
2744 | } |
||
2745 |
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.