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_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_Order, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class WC_Order extends WC_Abstract_Order { |
||
19 | |||
20 | /** |
||
21 | * Data stored in meta keys, but not considered "meta" for an order. |
||
22 | * @since 2.7.0 |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $_internal_meta_keys = array( |
||
26 | '_customer_user', '_order_key', '_order_currency', '_billing_first_name', |
||
27 | '_billing_last_name', '_billing_company', '_billing_address_1', '_billing_address_2', |
||
28 | '_billing_city', '_billing_state', '_billing_postcode', '_billing_country', |
||
29 | '_billing_email', '_billing_phone', '_shipping_first_name', '_shipping_last_name', |
||
30 | '_shipping_company', '_shipping_address_1', '_shipping_address_2', '_shipping_city', |
||
31 | '_shipping_state', '_shipping_postcode', '_shipping_country', '_completed_date', |
||
32 | '_paid_date', '_edit_lock', '_edit_last', '_cart_discount', '_cart_discount_tax', |
||
33 | '_order_shipping', '_order_shipping_tax', '_order_tax', '_order_total', '_order_total', |
||
34 | '_payment_method', '_payment_method_title', '_transaction_id', '_customer_ip_address', |
||
35 | '_customer_user_agent', '_created_via', '_order_version', '_prices_include_tax', |
||
36 | '_customer_note', '_date_completed', '_date_paid', '_payment_tokens', |
||
37 | ); |
||
38 | |||
39 | /** |
||
40 | * Stores data about status changes so relevant hooks can be fired. |
||
41 | * @var bool|array |
||
42 | */ |
||
43 | protected $_status_transition = false; |
||
44 | |||
45 | /** |
||
46 | * Order Data array. This is the core order data exposed in APIs since 2.7.0. |
||
47 | * @since 2.7.0 |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $_data = array( |
||
51 | // Abstract order props |
||
52 | 'id' => 0, |
||
53 | 'parent_id' => 0, |
||
54 | 'status' => '', |
||
55 | 'currency' => '', |
||
56 | 'version' => '', |
||
57 | 'prices_include_tax' => false, |
||
58 | 'date_created' => '', |
||
59 | 'date_modified' => '', |
||
60 | 'discount_total' => 0, |
||
61 | 'discount_tax' => 0, |
||
62 | 'shipping_total' => 0, |
||
63 | 'shipping_tax' => 0, |
||
64 | 'cart_tax' => 0, |
||
65 | 'total' => 0, |
||
66 | 'total_tax' => 0, |
||
67 | |||
68 | // Order props |
||
69 | 'customer_id' => 0, |
||
70 | 'order_key' => '', |
||
71 | 'billing' => array( |
||
72 | 'first_name' => '', |
||
73 | 'last_name' => '', |
||
74 | 'company' => '', |
||
75 | 'address_1' => '', |
||
76 | 'address_2' => '', |
||
77 | 'city' => '', |
||
78 | 'state' => '', |
||
79 | 'postcode' => '', |
||
80 | 'country' => '', |
||
81 | 'email' => '', |
||
82 | 'phone' => '', |
||
83 | ), |
||
84 | 'shipping' => array( |
||
85 | 'first_name' => '', |
||
86 | 'last_name' => '', |
||
87 | 'company' => '', |
||
88 | 'address_1' => '', |
||
89 | 'address_2' => '', |
||
90 | 'city' => '', |
||
91 | 'state' => '', |
||
92 | 'postcode' => '', |
||
93 | 'country' => '', |
||
94 | ), |
||
95 | 'payment_method' => '', |
||
96 | 'payment_method_title' => '', |
||
97 | 'transaction_id' => '', |
||
98 | 'customer_ip_address' => '', |
||
99 | 'customer_user_agent' => '', |
||
100 | 'created_via' => '', |
||
101 | 'customer_note' => '', |
||
102 | 'date_completed' => '', |
||
103 | 'date_paid' => '', |
||
104 | 'cart_hash' => '', |
||
105 | ); |
||
106 | |||
107 | /** |
||
108 | * When a payment is complete this function is called. |
||
109 | * |
||
110 | * Most of the time this should mark an order as 'processing' so that admin can process/post the items. |
||
111 | * If the cart contains only downloadable items then the order is 'completed' since the admin needs to take no action. |
||
112 | * Stock levels are reduced at this point. |
||
113 | * Sales are also recorded for products. |
||
114 | * Finally, record the date of payment. |
||
115 | * |
||
116 | * Order must exist. |
||
117 | * |
||
118 | * @param string $transaction_id Optional transaction id to store in post meta. |
||
119 | * @return bool success |
||
120 | */ |
||
121 | public function payment_complete( $transaction_id = '' ) { |
||
165 | |||
166 | /** |
||
167 | * Gets order total - formatted for display. |
||
168 | * @return string |
||
169 | */ |
||
170 | public function get_formatted_order_total( $tax_display = '', $display_refunded = true ) { |
||
202 | |||
203 | /* |
||
204 | |-------------------------------------------------------------------------- |
||
205 | | CRUD methods |
||
206 | |-------------------------------------------------------------------------- |
||
207 | | |
||
208 | | Methods which create, read, update and delete orders from the database. |
||
209 | | Written in abstract fashion so that the way orders are stored can be |
||
210 | | changed more easily in the future. |
||
211 | | |
||
212 | | A save method is included for convenience (chooses update or create based |
||
213 | | on if the order exists yet). |
||
214 | | |
||
215 | */ |
||
216 | |||
217 | /** |
||
218 | * Insert data into the database. |
||
219 | * @since 2.7.0 |
||
220 | */ |
||
221 | public function create() { |
||
262 | |||
263 | /** |
||
264 | * Read from the database. |
||
265 | * @since 2.7.0 |
||
266 | * @param int $id ID of object to read. |
||
267 | */ |
||
268 | public function read( $id ) { |
||
315 | |||
316 | /** |
||
317 | * Update data in the database. |
||
318 | * @since 2.7.0 |
||
319 | */ |
||
320 | public function update() { |
||
383 | |||
384 | /** |
||
385 | * Set order status. |
||
386 | * @since 2.7.0 |
||
387 | * @param string $new_status Status to change the order to. No internal wc- prefix is required. |
||
388 | * @param string $note (default: '') Optional note to add. |
||
389 | * @param bool $manual_update is this a manual order status change? |
||
390 | * @param array details of change |
||
391 | */ |
||
392 | public function set_status( $new_status, $note = '', $manual_update = false ) { |
||
414 | |||
415 | /** |
||
416 | * Updates status of order immediately. Order must exist. |
||
417 | * @uses WC_Order::set_status() |
||
418 | * @return bool success |
||
419 | */ |
||
420 | public function update_status( $new_status, $note = '', $manual = false ) { |
||
432 | |||
433 | /** |
||
434 | * Handle the status transition. |
||
435 | */ |
||
436 | protected function status_transition() { |
||
456 | |||
457 | /* |
||
458 | |-------------------------------------------------------------------------- |
||
459 | | Getters |
||
460 | |-------------------------------------------------------------------------- |
||
461 | | |
||
462 | | Methods for getting data from the order object. |
||
463 | | |
||
464 | */ |
||
465 | |||
466 | /** |
||
467 | * Get all class data in array format. |
||
468 | * @since 2.7.0 |
||
469 | * @return array |
||
470 | */ |
||
471 | View Code Duplication | public function get_data() { |
|
485 | |||
486 | /** |
||
487 | * get_order_number function. |
||
488 | * |
||
489 | * Gets the order number for display (by default, order ID). |
||
490 | * |
||
491 | * @return string |
||
492 | */ |
||
493 | public function get_order_number() { |
||
496 | |||
497 | /** |
||
498 | * Get order key. |
||
499 | * @since 2.7.0 |
||
500 | * @return string |
||
501 | */ |
||
502 | public function get_order_key() { |
||
505 | |||
506 | /** |
||
507 | * Get customer_id |
||
508 | * @return int |
||
509 | */ |
||
510 | public function get_customer_id() { |
||
513 | |||
514 | /** |
||
515 | * Alias for get_customer_id(). |
||
516 | * @return int |
||
517 | */ |
||
518 | public function get_user_id() { |
||
521 | |||
522 | /** |
||
523 | * Get the user associated with the order. False for guests. |
||
524 | * @return WP_User|false |
||
525 | */ |
||
526 | public function get_user() { |
||
529 | |||
530 | /** |
||
531 | * Get billing_first_name |
||
532 | * @return string |
||
533 | */ |
||
534 | public function get_billing_first_name() { |
||
537 | |||
538 | /** |
||
539 | * Get billing_last_name |
||
540 | * @return string |
||
541 | */ |
||
542 | public function get_billing_last_name() { |
||
545 | |||
546 | /** |
||
547 | * Get billing_company |
||
548 | * @return string |
||
549 | */ |
||
550 | public function get_billing_company() { |
||
553 | |||
554 | /** |
||
555 | * Get billing_address_1 |
||
556 | * @return string |
||
557 | */ |
||
558 | public function get_billing_address_1() { |
||
561 | |||
562 | /** |
||
563 | * Get billing_address_2 |
||
564 | * @return string $value |
||
565 | */ |
||
566 | public function get_billing_address_2() { |
||
569 | |||
570 | /** |
||
571 | * Get billing_city |
||
572 | * @return string $value |
||
573 | */ |
||
574 | public function get_billing_city() { |
||
577 | |||
578 | /** |
||
579 | * Get billing_state |
||
580 | * @return string |
||
581 | */ |
||
582 | public function get_billing_state() { |
||
585 | |||
586 | /** |
||
587 | * Get billing_postcode |
||
588 | * @return string |
||
589 | */ |
||
590 | public function get_billing_postcode() { |
||
593 | |||
594 | /** |
||
595 | * Get billing_country |
||
596 | * @return string |
||
597 | */ |
||
598 | public function get_billing_country() { |
||
601 | |||
602 | /** |
||
603 | * Get billing_email |
||
604 | * @return string |
||
605 | */ |
||
606 | public function get_billing_email() { |
||
609 | |||
610 | /** |
||
611 | * Get billing_phone |
||
612 | * @return string |
||
613 | */ |
||
614 | public function get_billing_phone() { |
||
617 | |||
618 | /** |
||
619 | * Get shipping_first_name |
||
620 | * @return string |
||
621 | */ |
||
622 | public function get_shipping_first_name() { |
||
625 | |||
626 | /** |
||
627 | * Get shipping_last_name |
||
628 | * @return string |
||
629 | */ |
||
630 | public function get_shipping_last_name() { |
||
633 | |||
634 | /** |
||
635 | * Get shipping_company |
||
636 | * @return string |
||
637 | */ |
||
638 | public function get_shipping_company() { |
||
641 | |||
642 | /** |
||
643 | * Get shipping_address_1 |
||
644 | * @return string |
||
645 | */ |
||
646 | public function get_shipping_address_1() { |
||
649 | |||
650 | /** |
||
651 | * Get shipping_address_2 |
||
652 | * @return string |
||
653 | */ |
||
654 | public function get_shipping_address_2() { |
||
657 | |||
658 | /** |
||
659 | * Get shipping_city |
||
660 | * @return string |
||
661 | */ |
||
662 | public function get_shipping_city() { |
||
665 | |||
666 | /** |
||
667 | * Get shipping_state |
||
668 | * @return string |
||
669 | */ |
||
670 | public function get_shipping_state() { |
||
673 | |||
674 | /** |
||
675 | * Get shipping_postcode |
||
676 | * @return string |
||
677 | */ |
||
678 | public function get_shipping_postcode() { |
||
681 | |||
682 | /** |
||
683 | * Get shipping_country |
||
684 | * @return string |
||
685 | */ |
||
686 | public function get_shipping_country() { |
||
689 | |||
690 | /** |
||
691 | * Get the payment method. |
||
692 | * @return string |
||
693 | */ |
||
694 | public function get_payment_method() { |
||
697 | |||
698 | /** |
||
699 | * Get payment_method_title |
||
700 | * @return string |
||
701 | */ |
||
702 | public function get_payment_method_title() { |
||
705 | |||
706 | /** |
||
707 | * Get transaction_id |
||
708 | * @return string |
||
709 | */ |
||
710 | public function get_transaction_id() { |
||
713 | |||
714 | /** |
||
715 | * Get customer_ip_address |
||
716 | * @return string |
||
717 | */ |
||
718 | public function get_customer_ip_address() { |
||
721 | |||
722 | /** |
||
723 | * Get customer_user_agent |
||
724 | * @return string |
||
725 | */ |
||
726 | public function get_customer_user_agent() { |
||
729 | |||
730 | /** |
||
731 | * Get created_via |
||
732 | * @return string |
||
733 | */ |
||
734 | public function get_created_via() { |
||
737 | |||
738 | /** |
||
739 | * Get customer_note |
||
740 | * @return string |
||
741 | */ |
||
742 | public function get_customer_note() { |
||
745 | |||
746 | /** |
||
747 | * Get date_completed |
||
748 | * @return int |
||
749 | */ |
||
750 | public function get_date_completed() { |
||
753 | |||
754 | /** |
||
755 | * Get date_paid |
||
756 | * @return int |
||
757 | */ |
||
758 | public function get_date_paid() { |
||
761 | |||
762 | /** |
||
763 | * Returns the requested address in raw, non-formatted way. |
||
764 | * @since 2.4.0 |
||
765 | * @param string $type Billing or shipping. Anything else besides 'billing' will return shipping address. |
||
766 | * @return array The stored address after filter. |
||
767 | */ |
||
768 | public function get_address( $type = 'billing' ) { |
||
771 | |||
772 | /** |
||
773 | * Get a formatted shipping address for the order. |
||
774 | * |
||
775 | * @return string |
||
776 | */ |
||
777 | public function get_shipping_address_map_url() { |
||
781 | |||
782 | /** |
||
783 | * Get a formatted billing full name. |
||
784 | * @return string |
||
785 | */ |
||
786 | public function get_formatted_billing_full_name() { |
||
789 | |||
790 | /** |
||
791 | * Get a formatted shipping full name. |
||
792 | * @return string |
||
793 | */ |
||
794 | public function get_formatted_shipping_full_name() { |
||
797 | |||
798 | /** |
||
799 | * Get a formatted billing address for the order. |
||
800 | * @return string |
||
801 | */ |
||
802 | public function get_formatted_billing_address() { |
||
805 | |||
806 | /** |
||
807 | * Get a formatted shipping address for the order. |
||
808 | * @return string |
||
809 | */ |
||
810 | public function get_formatted_shipping_address() { |
||
817 | |||
818 | /** |
||
819 | * Get cart hash |
||
820 | * @return string |
||
821 | */ |
||
822 | public function get_cart_hash() { |
||
825 | |||
826 | /* |
||
827 | |-------------------------------------------------------------------------- |
||
828 | | Setters |
||
829 | |-------------------------------------------------------------------------- |
||
830 | | |
||
831 | | Functions for setting order data. These should not update anything in the |
||
832 | | database itself and should only change what is stored in the class |
||
833 | | object. However, for backwards compatibility pre 2.7.0 some of these |
||
834 | | setters may handle both. |
||
835 | | |
||
836 | */ |
||
837 | |||
838 | /** |
||
839 | * Set order_key. |
||
840 | * @param string $value Max length 20 chars. |
||
841 | * @throws WC_Data_Exception |
||
842 | */ |
||
843 | public function set_order_key( $value ) { |
||
846 | |||
847 | /** |
||
848 | * Set customer_id |
||
849 | * @param int $value |
||
850 | * @throws WC_Data_Exception |
||
851 | */ |
||
852 | public function set_customer_id( $value ) { |
||
855 | |||
856 | /** |
||
857 | * Set billing_first_name |
||
858 | * @param string $value |
||
859 | * @throws WC_Data_Exception |
||
860 | */ |
||
861 | public function set_billing_first_name( $value ) { |
||
864 | |||
865 | /** |
||
866 | * Set billing_last_name |
||
867 | * @param string $value |
||
868 | * @throws WC_Data_Exception |
||
869 | */ |
||
870 | public function set_billing_last_name( $value ) { |
||
873 | |||
874 | /** |
||
875 | * Set billing_company |
||
876 | * @param string $value |
||
877 | * @throws WC_Data_Exception |
||
878 | */ |
||
879 | public function set_billing_company( $value ) { |
||
882 | |||
883 | /** |
||
884 | * Set billing_address_1 |
||
885 | * @param string $value |
||
886 | * @throws WC_Data_Exception |
||
887 | */ |
||
888 | public function set_billing_address_1( $value ) { |
||
891 | |||
892 | /** |
||
893 | * Set billing_address_2 |
||
894 | * @param string $value |
||
895 | * @throws WC_Data_Exception |
||
896 | */ |
||
897 | public function set_billing_address_2( $value ) { |
||
900 | |||
901 | /** |
||
902 | * Set billing_city |
||
903 | * @param string $value |
||
904 | * @throws WC_Data_Exception |
||
905 | */ |
||
906 | public function set_billing_city( $value ) { |
||
909 | |||
910 | /** |
||
911 | * Set billing_state |
||
912 | * @param string $value |
||
913 | * @throws WC_Data_Exception |
||
914 | */ |
||
915 | public function set_billing_state( $value ) { |
||
918 | |||
919 | /** |
||
920 | * Set billing_postcode |
||
921 | * @param string $value |
||
922 | * @throws WC_Data_Exception |
||
923 | */ |
||
924 | public function set_billing_postcode( $value ) { |
||
927 | |||
928 | /** |
||
929 | * Set billing_country |
||
930 | * @param string $value |
||
931 | * @throws WC_Data_Exception |
||
932 | */ |
||
933 | public function set_billing_country( $value ) { |
||
936 | |||
937 | /** |
||
938 | * Maybe set empty billing email to that of the user who owns the order. |
||
939 | */ |
||
940 | protected function maybe_set_user_billing_email() { |
||
949 | |||
950 | /** |
||
951 | * Set billing_email |
||
952 | * @param string $value |
||
953 | * @throws WC_Data_Exception |
||
954 | */ |
||
955 | public function set_billing_email( $value ) { |
||
961 | |||
962 | /** |
||
963 | * Set billing_phone |
||
964 | * @param string $value |
||
965 | * @throws WC_Data_Exception |
||
966 | */ |
||
967 | public function set_billing_phone( $value ) { |
||
970 | |||
971 | /** |
||
972 | * Set shipping_first_name |
||
973 | * @param string $value |
||
974 | * @throws WC_Data_Exception |
||
975 | */ |
||
976 | public function set_shipping_first_name( $value ) { |
||
979 | |||
980 | /** |
||
981 | * Set shipping_last_name |
||
982 | * @param string $value |
||
983 | * @throws WC_Data_Exception |
||
984 | */ |
||
985 | public function set_shipping_last_name( $value ) { |
||
988 | |||
989 | /** |
||
990 | * Set shipping_company |
||
991 | * @param string $value |
||
992 | * @throws WC_Data_Exception |
||
993 | */ |
||
994 | public function set_shipping_company( $value ) { |
||
997 | |||
998 | /** |
||
999 | * Set shipping_address_1 |
||
1000 | * @param string $value |
||
1001 | * @throws WC_Data_Exception |
||
1002 | */ |
||
1003 | public function set_shipping_address_1( $value ) { |
||
1006 | |||
1007 | /** |
||
1008 | * Set shipping_address_2 |
||
1009 | * @param string $value |
||
1010 | * @throws WC_Data_Exception |
||
1011 | */ |
||
1012 | public function set_shipping_address_2( $value ) { |
||
1015 | |||
1016 | /** |
||
1017 | * Set shipping_city |
||
1018 | * @param string $value |
||
1019 | * @throws WC_Data_Exception |
||
1020 | */ |
||
1021 | public function set_shipping_city( $value ) { |
||
1024 | |||
1025 | /** |
||
1026 | * Set shipping_state |
||
1027 | * @param string $value |
||
1028 | * @throws WC_Data_Exception |
||
1029 | */ |
||
1030 | public function set_shipping_state( $value ) { |
||
1033 | |||
1034 | /** |
||
1035 | * Set shipping_postcode |
||
1036 | * @param string $value |
||
1037 | * @throws WC_Data_Exception |
||
1038 | */ |
||
1039 | public function set_shipping_postcode( $value ) { |
||
1042 | |||
1043 | /** |
||
1044 | * Set shipping_country |
||
1045 | * @param string $value |
||
1046 | * @throws WC_Data_Exception |
||
1047 | */ |
||
1048 | public function set_shipping_country( $value ) { |
||
1051 | |||
1052 | /** |
||
1053 | * Set the payment method. |
||
1054 | * @param string $payment_method Supports WC_Payment_Gateway for bw compatibility with < 2.7 |
||
1055 | * @throws WC_Data_Exception |
||
1056 | */ |
||
1057 | public function set_payment_method( $payment_method = '' ) { |
||
1058 | if ( is_object( $payment_method ) ) { |
||
1059 | $this->set_payment_method( $payment_method->id ); |
||
1060 | $this->set_payment_method_title( $payment_method->get_title() ); |
||
1061 | } elseif ( '' === $payment_method ) { |
||
1062 | $this->_data['payment_method'] = ''; |
||
1063 | $this->_data['payment_method_title'] = ''; |
||
1064 | } else { |
||
1065 | $this->_data['payment_method'] = $payment_method; |
||
1066 | } |
||
1067 | } |
||
1068 | |||
1069 | /** |
||
1070 | * Set payment_method_title |
||
1071 | * @param string $value |
||
1072 | * @throws WC_Data_Exception |
||
1073 | */ |
||
1074 | public function set_payment_method_title( $value ) { |
||
1077 | |||
1078 | /** |
||
1079 | * Set transaction_id |
||
1080 | * @param string $value |
||
1081 | * @throws WC_Data_Exception |
||
1082 | */ |
||
1083 | public function set_transaction_id( $value ) { |
||
1086 | |||
1087 | /** |
||
1088 | * Set customer_ip_address |
||
1089 | * @param string $value |
||
1090 | * @throws WC_Data_Exception |
||
1091 | */ |
||
1092 | public function set_customer_ip_address( $value ) { |
||
1095 | |||
1096 | /** |
||
1097 | * Set customer_user_agent |
||
1098 | * @param string $value |
||
1099 | * @throws WC_Data_Exception |
||
1100 | */ |
||
1101 | public function set_customer_user_agent( $value ) { |
||
1104 | |||
1105 | /** |
||
1106 | * Set created_via |
||
1107 | * @param string $value |
||
1108 | * @throws WC_Data_Exception |
||
1109 | */ |
||
1110 | public function set_created_via( $value ) { |
||
1113 | |||
1114 | /** |
||
1115 | * Set customer_note |
||
1116 | * @param string $value |
||
1117 | * @throws WC_Data_Exception |
||
1118 | */ |
||
1119 | public function set_customer_note( $value ) { |
||
1122 | |||
1123 | /** |
||
1124 | * Set date_completed |
||
1125 | * @param string $timestamp |
||
1126 | * @throws WC_Data_Exception |
||
1127 | */ |
||
1128 | public function set_date_completed( $timestamp ) { |
||
1129 | $this->_data['date_completed'] = is_numeric( $timestamp ) ? $timestamp : strtotime( $timestamp ); |
||
1130 | } |
||
1131 | |||
1132 | /** |
||
1133 | * Set date_paid |
||
1134 | * @param string $timestamp |
||
1135 | * @throws WC_Data_Exception |
||
1136 | */ |
||
1137 | public function set_date_paid( $timestamp ) { |
||
1138 | $this->_data['date_paid'] = is_numeric( $timestamp ) ? $timestamp : strtotime( $timestamp ); |
||
1139 | } |
||
1140 | |||
1141 | /** |
||
1142 | * Set cart hash |
||
1143 | * @param string $value |
||
1144 | * @throws WC_Data_Exception |
||
1145 | */ |
||
1146 | public function set_cart_hash( $value ) { |
||
1149 | |||
1150 | /* |
||
1151 | |-------------------------------------------------------------------------- |
||
1152 | | Conditionals |
||
1153 | |-------------------------------------------------------------------------- |
||
1154 | | |
||
1155 | | Checks if a condition is true or false. |
||
1156 | | |
||
1157 | */ |
||
1158 | |||
1159 | /** |
||
1160 | * Check if an order key is valid. |
||
1161 | * |
||
1162 | * @param mixed $key |
||
1163 | * @return bool |
||
1164 | */ |
||
1165 | public function key_is_valid( $key ) { |
||
1168 | |||
1169 | /** |
||
1170 | * See if order matches cart_hash. |
||
1171 | * @return bool |
||
1172 | */ |
||
1173 | public function has_cart_hash( $cart_hash = '' ) { |
||
1176 | |||
1177 | /** |
||
1178 | * Checks if an order can be edited, specifically for use on the Edit Order screen. |
||
1179 | * @return bool |
||
1180 | */ |
||
1181 | public function is_editable() { |
||
1184 | |||
1185 | /** |
||
1186 | * Returns if an order has been paid for based on the order status. |
||
1187 | * @since 2.5.0 |
||
1188 | * @return bool |
||
1189 | */ |
||
1190 | public function is_paid() { |
||
1193 | |||
1194 | /** |
||
1195 | * Checks if product download is permitted. |
||
1196 | * |
||
1197 | * @return bool |
||
1198 | */ |
||
1199 | public function is_download_permitted() { |
||
1202 | |||
1203 | /** |
||
1204 | * Checks if an order needs display the shipping address, based on shipping method. |
||
1205 | * @return bool |
||
1206 | */ |
||
1207 | public function needs_shipping_address() { |
||
1227 | |||
1228 | /** |
||
1229 | * Returns true if the order contains a downloadable product. |
||
1230 | * @return bool |
||
1231 | */ |
||
1232 | public function has_downloadable_item() { |
||
1240 | |||
1241 | /** |
||
1242 | * Checks if an order needs payment, based on status and order total. |
||
1243 | * |
||
1244 | * @return bool |
||
1245 | */ |
||
1246 | public function needs_payment() { |
||
1250 | |||
1251 | /* |
||
1252 | |-------------------------------------------------------------------------- |
||
1253 | | URLs and Endpoints |
||
1254 | |-------------------------------------------------------------------------- |
||
1255 | */ |
||
1256 | |||
1257 | /** |
||
1258 | * 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. |
||
1259 | * |
||
1260 | * @param bool $on_checkout |
||
1261 | * @return string |
||
1262 | */ |
||
1263 | public function get_checkout_payment_url( $on_checkout = false ) { |
||
1278 | |||
1279 | /** |
||
1280 | * Generates a URL for the thanks page (order received). |
||
1281 | * |
||
1282 | * @return string |
||
1283 | */ |
||
1284 | public function get_checkout_order_received_url() { |
||
1295 | |||
1296 | /** |
||
1297 | * Generates a URL so that a customer can cancel their (unpaid - pending) order. |
||
1298 | * |
||
1299 | * @param string $redirect |
||
1300 | * |
||
1301 | * @return string |
||
1302 | */ |
||
1303 | View Code Duplication | public function get_cancel_order_url( $redirect = '' ) { |
|
1311 | |||
1312 | /** |
||
1313 | * Generates a raw (unescaped) cancel-order URL for use by payment gateways. |
||
1314 | * |
||
1315 | * @param string $redirect |
||
1316 | * |
||
1317 | * @return string The unescaped cancel-order URL. |
||
1318 | */ |
||
1319 | View Code Duplication | public function get_cancel_order_url_raw( $redirect = '' ) { |
|
1328 | |||
1329 | /** |
||
1330 | * Helper method to return the cancel endpoint. |
||
1331 | * |
||
1332 | * @return string the cancel endpoint; either the cart page or the home page. |
||
1333 | */ |
||
1334 | public function get_cancel_endpoint() { |
||
1346 | |||
1347 | /** |
||
1348 | * Generates a URL to view an order from the my account page. |
||
1349 | * |
||
1350 | * @return string |
||
1351 | */ |
||
1352 | public function get_view_order_url() { |
||
1355 | |||
1356 | /* |
||
1357 | |-------------------------------------------------------------------------- |
||
1358 | | Order notes. |
||
1359 | |-------------------------------------------------------------------------- |
||
1360 | */ |
||
1361 | |||
1362 | /** |
||
1363 | * Adds a note (comment) to the order. Order must exist. |
||
1364 | * |
||
1365 | * @param string $note Note to add. |
||
1366 | * @param int $is_customer_note (default: 0) Is this a note for the customer? |
||
1367 | * @param bool added_by_user Was the note added by a user? |
||
1368 | * @return int Comment ID. |
||
1369 | */ |
||
1370 | public function add_order_note( $note, $is_customer_note = 0, $added_by_user = false ) { |
||
1407 | |||
1408 | /** |
||
1409 | * List order notes (public) for the customer. |
||
1410 | * |
||
1411 | * @return array |
||
1412 | */ |
||
1413 | public function get_customer_order_notes() { |
||
1437 | |||
1438 | /* |
||
1439 | |-------------------------------------------------------------------------- |
||
1440 | | Refunds |
||
1441 | |-------------------------------------------------------------------------- |
||
1442 | */ |
||
1443 | |||
1444 | /** |
||
1445 | * Get order refunds. |
||
1446 | * @since 2.2 |
||
1447 | * @return array of WC_Order_Refund objects |
||
1448 | */ |
||
1449 | public function get_refunds() { |
||
1457 | |||
1458 | /** |
||
1459 | * Get amount already refunded. |
||
1460 | * |
||
1461 | * @since 2.2 |
||
1462 | * @return string |
||
1463 | */ |
||
1464 | View Code Duplication | public function get_total_refunded() { |
|
1477 | |||
1478 | /** |
||
1479 | * Get the total tax refunded. |
||
1480 | * |
||
1481 | * @since 2.3 |
||
1482 | * @return float |
||
1483 | */ |
||
1484 | View Code Duplication | public function get_total_tax_refunded() { |
|
1498 | |||
1499 | /** |
||
1500 | * Get the total shipping refunded. |
||
1501 | * |
||
1502 | * @since 2.4 |
||
1503 | * @return float |
||
1504 | */ |
||
1505 | View Code Duplication | public function get_total_shipping_refunded() { |
|
1519 | |||
1520 | /** |
||
1521 | * Gets the count of order items of a certain type that have been refunded. |
||
1522 | * @since 2.4.0 |
||
1523 | * @param string $item_type |
||
1524 | * @return string |
||
1525 | */ |
||
1526 | public function get_item_count_refunded( $item_type = '' ) { |
||
1543 | |||
1544 | /** |
||
1545 | * Get the total number of items refunded. |
||
1546 | * |
||
1547 | * @since 2.4.0 |
||
1548 | * @param string $item_type type of the item we're checking, if not a line_item |
||
1549 | * @return integer |
||
1550 | */ |
||
1551 | public function get_total_qty_refunded( $item_type = 'line_item' ) { |
||
1560 | |||
1561 | /** |
||
1562 | * Get the refunded amount for a line item. |
||
1563 | * |
||
1564 | * @param int $item_id ID of the item we're checking |
||
1565 | * @param string $item_type type of the item we're checking, if not a line_item |
||
1566 | * @return integer |
||
1567 | */ |
||
1568 | View Code Duplication | public function get_qty_refunded_for_item( $item_id, $item_type = 'line_item' ) { |
|
1579 | |||
1580 | /** |
||
1581 | * Get the refunded amount for a line item. |
||
1582 | * |
||
1583 | * @param int $item_id ID of the item we're checking |
||
1584 | * @param string $item_type type of the item we're checking, if not a line_item |
||
1585 | * @return integer |
||
1586 | */ |
||
1587 | View Code Duplication | public function get_total_refunded_for_item( $item_id, $item_type = 'line_item' ) { |
|
1598 | |||
1599 | /** |
||
1600 | * Get the refunded amount for a line item. |
||
1601 | * |
||
1602 | * @param int $item_id ID of the item we're checking |
||
1603 | * @param int $tax_id ID of the tax we're checking |
||
1604 | * @param string $item_type type of the item we're checking, if not a line_item |
||
1605 | * @return double |
||
1606 | */ |
||
1607 | View Code Duplication | public function get_tax_refunded_for_item( $item_id, $tax_id, $item_type = 'line_item' ) { |
|
1618 | |||
1619 | /** |
||
1620 | * Get total tax refunded by rate ID. |
||
1621 | * |
||
1622 | * @param int $rate_id |
||
1623 | * |
||
1624 | * @return float |
||
1625 | */ |
||
1626 | public function get_total_tax_refunded_by_rate_id( $rate_id ) { |
||
1638 | |||
1639 | /** |
||
1640 | * How much money is left to refund? |
||
1641 | * @return string |
||
1642 | */ |
||
1643 | public function get_remaining_refund_amount() { |
||
1646 | |||
1647 | /** |
||
1648 | * How many items are left to refund? |
||
1649 | * @return int |
||
1650 | */ |
||
1651 | public function get_remaining_refund_items() { |
||
1654 | } |
||
1655 |
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.