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 |
||
19 | abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order implements WC_Data { |
||
20 | |||
21 | /** |
||
22 | * Data array, with defaults. |
||
23 | * |
||
24 | * @todo when migrating to custom tables, these will be columns |
||
25 | * @since 2.6.0 |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $_data = array( |
||
29 | 'order_id' => 0, |
||
30 | 'parent_id' => 0, |
||
31 | 'status' => '', |
||
32 | /** |
||
33 | * @todo confusion. Was 'simple'. But this was not the same as post_type, which is shop_order. Other post types in core are shop_order_refund |
||
34 | * The order type for shop_order_refund is refund. |
||
35 | * Why do we need two separate variables? This should be unified, especially once this is in a custom table and post_type is redundent. |
||
36 | * Switching to 'shop_order', and then using this value in the order factory instead of post_type. @thenbrent might have feedback on this. |
||
37 | */ |
||
38 | 'order_type' => 'shop_order', |
||
39 | 'order_key' => '', |
||
40 | 'order_currency' => '', |
||
41 | 'date_created' => '', |
||
42 | 'date_modified' => '', |
||
43 | 'customer_id' => 0, |
||
44 | 'billing_first_name' => '', |
||
45 | 'billing_last_name' => '', |
||
46 | 'billing_company' => '', |
||
47 | 'billing_address_1' => '', |
||
48 | 'billing_address_2' => '', |
||
49 | 'billing_city' => '', |
||
50 | 'billing_state' => '', |
||
51 | 'billing_postcode' => '', |
||
52 | 'billing_country' => '', |
||
53 | 'billing_email' => '', |
||
54 | 'billing_phone' => '', |
||
55 | 'shipping_first_name' => '', |
||
56 | 'shipping_last_name' => '', |
||
57 | 'shipping_company' => '', |
||
58 | 'shipping_address_1' => '', |
||
59 | 'shipping_address_2' => '', |
||
60 | 'shipping_city' => '', |
||
61 | 'shipping_state' => '', |
||
62 | 'shipping_postcode' => '', |
||
63 | 'shipping_country' => '', |
||
64 | 'discount_total' => 0, |
||
65 | 'discount_tax' => 0, |
||
66 | 'shipping_total' => 0, |
||
67 | 'shipping_tax' => 0, |
||
68 | 'cart_tax' => 0, // cart_tax is the new name for the legacy 'order_tax' which is the tax for items only, not shipping. |
||
69 | 'order_total' => 0, |
||
70 | 'order_tax' => 0, // Sum of all taxes. |
||
71 | ); |
||
72 | |||
73 | /** |
||
74 | * Stores meta data. |
||
75 | * @var array |
||
76 | */ |
||
77 | protected $_meta = array( |
||
78 | 'payment_method' => '', |
||
79 | 'payment_method_title' => '', |
||
80 | 'transaction_id' => '', |
||
81 | 'customer_ip_address' => '', |
||
82 | 'customer_user_agent' => '', |
||
83 | 'created_via' => '', |
||
84 | 'order_version' => '', |
||
85 | 'prices_include_tax' => false, |
||
86 | 'customer_note' => '', |
||
87 | 'date_completed' => '', |
||
88 | 'date_paid' => '', |
||
89 | ); |
||
90 | |||
91 | /** |
||
92 | * Stores data about status changes so relevant hooks can be fired. |
||
93 | * @var bool|array |
||
94 | */ |
||
95 | protected $_status_transition = false; |
||
96 | |||
97 | /** |
||
98 | * Get the order if ID is passed, otherwise the order is new and empty. |
||
99 | * This class should NOT be instantiated, but the get_order function or new WC_Order_Factory. |
||
100 | * should be used. It is possible, but the aforementioned are preferred and are the only. |
||
101 | * methods that will be maintained going forward. |
||
102 | * |
||
103 | * @param int|object|WC_Order $order Order to init. |
||
104 | */ |
||
105 | View Code Duplication | public function __construct( $order = 0 ) { |
|
114 | |||
115 | /** |
||
116 | * Change data to JSON format. |
||
117 | * @return string Data in JSON format. |
||
118 | */ |
||
119 | public function __toString() { |
||
122 | |||
123 | /** |
||
124 | * Get Meta Data by Key |
||
125 | * @param string $key |
||
126 | * @return mixed |
||
127 | */ |
||
128 | public function get_meta( $key = null ){ |
||
135 | |||
136 | /* |
||
137 | |-------------------------------------------------------------------------- |
||
138 | | Getters |
||
139 | |-------------------------------------------------------------------------- |
||
140 | | |
||
141 | | Methods for getting data from the order object. |
||
142 | | |
||
143 | */ |
||
144 | |||
145 | /** |
||
146 | * Get all class data in array format. |
||
147 | * @since 2.6.0 |
||
148 | * @return array |
||
149 | */ |
||
150 | public function get_data() { |
||
163 | |||
164 | /** |
||
165 | * Get order ID. |
||
166 | * @since 2.6.0 |
||
167 | * @return integer |
||
168 | */ |
||
169 | public function get_id() { |
||
172 | |||
173 | /** |
||
174 | * Get order ID. |
||
175 | * @since 2.6.0 |
||
176 | * @return integer |
||
177 | */ |
||
178 | public function get_order_id() { |
||
181 | |||
182 | /** |
||
183 | * Get parent order ID. |
||
184 | * @since 2.6.0 |
||
185 | * @return integer |
||
186 | */ |
||
187 | public function get_parent_id() { |
||
190 | |||
191 | /** |
||
192 | * get_order_number function. |
||
193 | * |
||
194 | * Gets the order number for display (by default, order ID). |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | public function get_order_number() { |
||
201 | |||
202 | /** |
||
203 | * Get order key. |
||
204 | * @since 2.6.0 |
||
205 | * @return string |
||
206 | */ |
||
207 | public function get_order_key() { |
||
210 | |||
211 | /** |
||
212 | * Gets order currency. |
||
213 | * @return string |
||
214 | */ |
||
215 | public function get_order_currency() { |
||
218 | |||
219 | /** |
||
220 | * Get Order Type |
||
221 | * @return string |
||
222 | */ |
||
223 | public function get_order_type() { |
||
226 | |||
227 | /** |
||
228 | * Get date_created |
||
229 | * @return string |
||
230 | */ |
||
231 | public function get_date_created() { |
||
234 | |||
235 | /** |
||
236 | * Get date_modified |
||
237 | * @return string |
||
238 | */ |
||
239 | public function get_date_modified() { |
||
242 | |||
243 | /** |
||
244 | * Get customer_id |
||
245 | * @return int |
||
246 | */ |
||
247 | public function get_customer_id() { |
||
250 | |||
251 | /** |
||
252 | * Get billing_first_name |
||
253 | * @return string |
||
254 | */ |
||
255 | public function get_billing_first_name() { |
||
258 | |||
259 | /** |
||
260 | * Get billing_last_name |
||
261 | * @return string |
||
262 | */ |
||
263 | public function get_billing_last_name() { |
||
266 | |||
267 | /** |
||
268 | * Get billing_company |
||
269 | * @return string |
||
270 | */ |
||
271 | public function get_billing_company() { |
||
274 | |||
275 | /** |
||
276 | * Get billing_address_1 |
||
277 | * @return string |
||
278 | */ |
||
279 | public function get_billing_address_1() { |
||
282 | |||
283 | /** |
||
284 | * Get billing_address_2 |
||
285 | * @return string $value |
||
286 | */ |
||
287 | public function get_billing_address_2() { |
||
290 | |||
291 | /** |
||
292 | * Get billing_city |
||
293 | * @return string $value |
||
294 | */ |
||
295 | public function get_billing_city() { |
||
298 | |||
299 | /** |
||
300 | * Get billing_state |
||
301 | * @return string |
||
302 | */ |
||
303 | public function get_billing_state() { |
||
306 | |||
307 | /** |
||
308 | * Get billing_postcode |
||
309 | * @return string |
||
310 | */ |
||
311 | public function get_billing_postcode() { |
||
314 | |||
315 | /** |
||
316 | * Get billing_country |
||
317 | * @return string |
||
318 | */ |
||
319 | public function get_billing_country() { |
||
322 | |||
323 | /** |
||
324 | * Get billing_email |
||
325 | * @return string |
||
326 | */ |
||
327 | public function get_billing_email() { |
||
330 | |||
331 | /** |
||
332 | * Get billing_phone |
||
333 | * @return string |
||
334 | */ |
||
335 | public function get_billing_phone() { |
||
338 | |||
339 | /** |
||
340 | * Get shipping_first_name |
||
341 | * @return string |
||
342 | */ |
||
343 | public function get_shipping_first_name() { |
||
346 | |||
347 | /** |
||
348 | * Get shipping_last_name |
||
349 | * @return string |
||
350 | */ |
||
351 | public function get_shipping_last_name() { |
||
354 | |||
355 | /** |
||
356 | * Get shipping_company |
||
357 | * @return string |
||
358 | */ |
||
359 | public function get_shipping_company() { |
||
362 | |||
363 | /** |
||
364 | * Get shipping_address_1 |
||
365 | * @return string |
||
366 | */ |
||
367 | public function get_shipping_address_1() { |
||
370 | |||
371 | /** |
||
372 | * Get shipping_address_2 |
||
373 | * @return string |
||
374 | */ |
||
375 | public function get_shipping_address_2() { |
||
378 | |||
379 | /** |
||
380 | * Get shipping_city |
||
381 | * @return string |
||
382 | */ |
||
383 | public function get_shipping_city() { |
||
386 | |||
387 | /** |
||
388 | * Get shipping_state |
||
389 | * @return string |
||
390 | */ |
||
391 | public function get_shipping_state() { |
||
394 | |||
395 | /** |
||
396 | * Get shipping_postcode |
||
397 | * @return string |
||
398 | */ |
||
399 | public function get_shipping_postcode() { |
||
402 | |||
403 | /** |
||
404 | * Get shipping_country |
||
405 | * @return string |
||
406 | */ |
||
407 | public function get_shipping_country() { |
||
410 | |||
411 | /** |
||
412 | * Get the payment method. |
||
413 | * @return string |
||
414 | */ |
||
415 | public function get_payment_method() { |
||
418 | |||
419 | /** |
||
420 | * Get payment_method_title |
||
421 | * @return string |
||
422 | */ |
||
423 | public function get_payment_method_title() { |
||
426 | |||
427 | /** |
||
428 | * Get transaction_id |
||
429 | * @return string |
||
430 | */ |
||
431 | public function get_transaction_id() { |
||
434 | |||
435 | /** |
||
436 | * Get customer_ip_address |
||
437 | * @return string |
||
438 | */ |
||
439 | public function get_customer_ip_address() { |
||
442 | |||
443 | /** |
||
444 | * Get customer_user_agent |
||
445 | * @return string |
||
446 | */ |
||
447 | public function get_customer_user_agent() { |
||
450 | |||
451 | /** |
||
452 | * Get created_via |
||
453 | * @return string |
||
454 | */ |
||
455 | public function get_created_via() { |
||
458 | |||
459 | /** |
||
460 | * Get order_version |
||
461 | * @return string |
||
462 | */ |
||
463 | public function get_order_version() { |
||
466 | |||
467 | /** |
||
468 | * Get prices_include_tax |
||
469 | * @return bool |
||
470 | */ |
||
471 | public function get_prices_include_tax() { |
||
474 | |||
475 | /** |
||
476 | * Get customer_note |
||
477 | * @return string |
||
478 | */ |
||
479 | public function get_customer_note() { |
||
482 | |||
483 | /** |
||
484 | * Get date_completed |
||
485 | * @return string |
||
486 | */ |
||
487 | public function get_date_completed() { |
||
490 | |||
491 | /** |
||
492 | * Get date_paid |
||
493 | * @return string |
||
494 | */ |
||
495 | public function get_date_paid() { |
||
498 | |||
499 | /** |
||
500 | * Returns the requested address in raw, non-formatted way. |
||
501 | * @since 2.4.0 |
||
502 | * @param string $type Billing or shipping. Anything else besides 'billing' will return shipping address. |
||
503 | * @return array The stored address after filter. |
||
504 | */ |
||
505 | public function get_address( $type = 'billing' ) { |
||
535 | |||
536 | /** |
||
537 | * Return the order statuses without wc- internal prefix. |
||
538 | * @return string |
||
539 | */ |
||
540 | public function get_status() { |
||
543 | |||
544 | /** |
||
545 | * Alias for get_customer_id(). |
||
546 | * @since 2.2 |
||
547 | * @return int |
||
548 | */ |
||
549 | public function get_user_id() { |
||
552 | |||
553 | /** |
||
554 | * Get the user associated with the order. False for guests. |
||
555 | * |
||
556 | * @since 2.2 |
||
557 | * @return WP_User|false |
||
558 | */ |
||
559 | public function get_user() { |
||
562 | |||
563 | /** |
||
564 | * Get a formatted billing address for the order. |
||
565 | * @return string |
||
566 | */ |
||
567 | public function get_formatted_billing_address() { |
||
570 | |||
571 | /** |
||
572 | * Get a formatted shipping address for the order. |
||
573 | * @return string |
||
574 | */ |
||
575 | public function get_formatted_shipping_address() { |
||
582 | |||
583 | /** |
||
584 | * Get a formatted shipping address for the order. |
||
585 | * |
||
586 | * @return string |
||
587 | */ |
||
588 | public function get_shipping_address_map_url() { |
||
599 | |||
600 | /** |
||
601 | * Get a formatted billing full name. |
||
602 | * |
||
603 | * @since 2.4.0 |
||
604 | * |
||
605 | * @return string |
||
606 | */ |
||
607 | public function get_formatted_billing_full_name() { |
||
610 | |||
611 | /** |
||
612 | * Get a formatted shipping full name. |
||
613 | * |
||
614 | * @since 2.4.0 |
||
615 | * |
||
616 | * @return string |
||
617 | */ |
||
618 | public function get_formatted_shipping_full_name() { |
||
621 | |||
622 | /** |
||
623 | * Get discount_total |
||
624 | * @return string |
||
625 | */ |
||
626 | public function get_discount_total() { |
||
636 | |||
637 | /** |
||
638 | * Get discount_tax |
||
639 | * @return string |
||
640 | */ |
||
641 | public function get_discount_tax() { |
||
644 | |||
645 | /** |
||
646 | * Get shipping_total |
||
647 | * woocommerce_order_amount_total_shipping filter has been removed to avoid |
||
648 | * these values being modified and then saved back to the DB. There are |
||
649 | * other, later hooks available to change totals on display. e.g. |
||
650 | * woocommerce_get_order_item_totals. |
||
651 | * @return string |
||
652 | */ |
||
653 | public function get_shipping_total() { |
||
656 | |||
657 | /** |
||
658 | * Gets cart tax amount. |
||
659 | * |
||
660 | * @since 2.6.0 woocommerce_order_amount_cart_tax filter has been removed to avoid |
||
661 | * these values being modified and then saved back to the DB or used in |
||
662 | * calculations. There are other, later hooks available to change totals on |
||
663 | * display. e.g. woocommerce_get_order_item_totals. |
||
664 | * @return float |
||
665 | */ |
||
666 | public function get_cart_tax() { |
||
669 | |||
670 | /** |
||
671 | * Get shipping_tax. |
||
672 | * |
||
673 | * @since 2.6.0 woocommerce_order_amount_shipping_tax filter has been removed to avoid |
||
674 | * these values being modified and then saved back to the DB or used in |
||
675 | * calculations. There are other, later hooks available to change totals on |
||
676 | * display. e.g. woocommerce_get_order_item_totals. |
||
677 | * @return string |
||
678 | */ |
||
679 | public function get_shipping_tax() { |
||
682 | |||
683 | /** |
||
684 | * Order tax is the sum of all taxes. |
||
685 | * @return string |
||
686 | */ |
||
687 | public function get_order_tax() { |
||
690 | |||
691 | /** |
||
692 | * Get the stored order total. Includes taxes and everything else. |
||
693 | * @return string |
||
694 | */ |
||
695 | public function get_order_total() { |
||
698 | |||
699 | /** |
||
700 | * Gets the total discount amount. |
||
701 | * @param bool $ex_tax Show discount excl any tax. |
||
702 | * @return float |
||
703 | */ |
||
704 | public function get_total_discount( $ex_tax = true ) { |
||
712 | |||
713 | /** |
||
714 | * Get total tax amount. Alias for get_order_tax(). |
||
715 | * |
||
716 | * @since 2.6.0 woocommerce_order_amount_total_tax filter has been removed to avoid |
||
717 | * these values being modified and then saved back to the DB. There are |
||
718 | * other, later hooks available to change totals on display. e.g. |
||
719 | * woocommerce_get_order_item_totals. |
||
720 | * @return float |
||
721 | */ |
||
722 | public function get_total_tax() { |
||
725 | |||
726 | /** |
||
727 | * Gets shipping total. Alias of WC_Order::get_shipping_total(). |
||
728 | * |
||
729 | * @since 2.6.0 woocommerce_order_amount_total_shipping filter has been removed to avoid |
||
730 | * these values being modified and then saved back to the DB or used in |
||
731 | * calculations. There are other, later hooks available to change totals on |
||
732 | * display. e.g. woocommerce_get_order_item_totals. |
||
733 | * @return float |
||
734 | */ |
||
735 | public function get_total_shipping() { |
||
738 | |||
739 | /** |
||
740 | * Gets order grand total. incl. taxes. Used in gateways. Filtered. |
||
741 | * @return float |
||
742 | */ |
||
743 | public function get_total() { |
||
746 | |||
747 | /** |
||
748 | * Gets order subtotal. |
||
749 | * @return float |
||
750 | */ |
||
751 | public function get_subtotal() { |
||
760 | |||
761 | /** |
||
762 | * Get taxes, merged by code, formatted ready for output. |
||
763 | * |
||
764 | * @return array |
||
765 | */ |
||
766 | public function get_tax_totals() { |
||
787 | |||
788 | /** |
||
789 | * Gets formatted shipping method title. |
||
790 | * @return string |
||
791 | */ |
||
792 | public function get_shipping_method() { |
||
799 | |||
800 | /* |
||
801 | |-------------------------------------------------------------------------- |
||
802 | | Setters |
||
803 | |-------------------------------------------------------------------------- |
||
804 | | |
||
805 | | Functions for setting order data. These should not update anything in the |
||
806 | | database itself and should only change what is stored in the class |
||
807 | | object. However, for backwards compatibility pre 2.6.0 some of these |
||
808 | | setters may handle both. |
||
809 | | |
||
810 | */ |
||
811 | |||
812 | /** |
||
813 | * Set order ID. |
||
814 | * @since 2.6.0 |
||
815 | * @param int $value |
||
816 | */ |
||
817 | public function set_order_id( $value ) { |
||
820 | |||
821 | /** |
||
822 | * Set parent order ID. |
||
823 | * @since 2.6.0 |
||
824 | * @param int $value |
||
825 | */ |
||
826 | public function set_parent_id( $value ) { |
||
829 | |||
830 | /** |
||
831 | * Set order status. |
||
832 | * @since 2.6.0 |
||
833 | * @param string $new_status Status to change the order to. No internal wc- prefix is required. |
||
834 | * @param string $note (default: '') Optional note to add. |
||
835 | * @param bool $manual is this a manual order status change? |
||
836 | */ |
||
837 | public function set_status( $new_status, $note = '', $manual_update = false ) { |
||
856 | |||
857 | /** |
||
858 | * Updates status of order immediately. |
||
859 | * @uses WC_Order::set_status() |
||
860 | */ |
||
861 | public function update_status( $new_status, $note = '', $manual = false ) { |
||
869 | |||
870 | /** |
||
871 | * Set Order Type |
||
872 | * @param string $value |
||
873 | */ |
||
874 | public function set_order_type( $value ) { |
||
877 | |||
878 | /** |
||
879 | * Set order_key |
||
880 | * @param string $value |
||
881 | */ |
||
882 | public function set_order_key( $value ) { |
||
885 | |||
886 | /** |
||
887 | * Set order_currency |
||
888 | * @param string $value |
||
889 | */ |
||
890 | public function set_order_currency( $value ) { |
||
893 | |||
894 | /** |
||
895 | * Set date_created |
||
896 | * @param string $timestamp Timestamp |
||
897 | */ |
||
898 | public function set_date_created( $timestamp ) { |
||
901 | |||
902 | /** |
||
903 | * Set date_modified |
||
904 | * @param string $timestamp |
||
905 | */ |
||
906 | public function set_date_modified( $timestamp ) { |
||
909 | |||
910 | /** |
||
911 | * Set customer_id |
||
912 | * @param int $value |
||
913 | */ |
||
914 | public function set_customer_id( $value ) { |
||
917 | |||
918 | /** |
||
919 | * Set billing_first_name |
||
920 | * @param string $value |
||
921 | */ |
||
922 | public function set_billing_first_name( $value ) { |
||
925 | |||
926 | /** |
||
927 | * Set billing_last_name |
||
928 | * @param string $value |
||
929 | */ |
||
930 | public function set_billing_last_name( $value ) { |
||
933 | |||
934 | /** |
||
935 | * Set billing_company |
||
936 | * @param string $value |
||
937 | */ |
||
938 | public function set_billing_company( $value ) { |
||
941 | |||
942 | /** |
||
943 | * Set billing_address_1 |
||
944 | * @param string $value |
||
945 | */ |
||
946 | public function set_billing_address_1( $value ) { |
||
949 | |||
950 | /** |
||
951 | * Set billing_address_2 |
||
952 | * @param string $value |
||
953 | */ |
||
954 | public function set_billing_address_2( $value ) { |
||
957 | |||
958 | /** |
||
959 | * Set billing_city |
||
960 | * @param string $value |
||
961 | */ |
||
962 | public function set_billing_city( $value ) { |
||
965 | |||
966 | /** |
||
967 | * Set billing_state |
||
968 | * @param string $value |
||
969 | */ |
||
970 | public function set_billing_state( $value ) { |
||
973 | |||
974 | /** |
||
975 | * Set billing_postcode |
||
976 | * @param string $value |
||
977 | */ |
||
978 | public function set_billing_postcode( $value ) { |
||
981 | |||
982 | /** |
||
983 | * Set billing_country |
||
984 | * @param string $value |
||
985 | */ |
||
986 | public function set_billing_country( $value ) { |
||
989 | |||
990 | /** |
||
991 | * Set billing_email |
||
992 | * @param string $value |
||
993 | */ |
||
994 | public function set_billing_email( $value ) { |
||
998 | |||
999 | /** |
||
1000 | * Set billing_phone |
||
1001 | * @param string $value |
||
1002 | */ |
||
1003 | public function set_billing_phone( $value ) { |
||
1006 | |||
1007 | /** |
||
1008 | * Set shipping_first_name |
||
1009 | * @param string $value |
||
1010 | */ |
||
1011 | public function set_shipping_first_name( $value ) { |
||
1014 | |||
1015 | /** |
||
1016 | * Set shipping_last_name |
||
1017 | * @param string $value |
||
1018 | */ |
||
1019 | public function set_shipping_last_name( $value ) { |
||
1022 | |||
1023 | /** |
||
1024 | * Set shipping_company |
||
1025 | * @param string $value |
||
1026 | */ |
||
1027 | public function set_shipping_company( $value ) { |
||
1030 | |||
1031 | /** |
||
1032 | * Set shipping_address_1 |
||
1033 | * @param string $value |
||
1034 | */ |
||
1035 | public function set_shipping_address_1( $value ) { |
||
1038 | |||
1039 | /** |
||
1040 | * Set shipping_address_2 |
||
1041 | * @param string $value |
||
1042 | */ |
||
1043 | public function set_shipping_address_2( $value ) { |
||
1046 | |||
1047 | /** |
||
1048 | * Set shipping_city |
||
1049 | * @param string $value |
||
1050 | */ |
||
1051 | public function set_shipping_city( $value ) { |
||
1054 | |||
1055 | /** |
||
1056 | * Set shipping_state |
||
1057 | * @param string $value |
||
1058 | */ |
||
1059 | public function set_shipping_state( $value ) { |
||
1062 | |||
1063 | /** |
||
1064 | * Set shipping_postcode |
||
1065 | * @param string $value |
||
1066 | */ |
||
1067 | public function set_shipping_postcode( $value ) { |
||
1070 | |||
1071 | /** |
||
1072 | * Set shipping_country |
||
1073 | * @param string $value |
||
1074 | */ |
||
1075 | public function set_shipping_country( $value ) { |
||
1078 | |||
1079 | /** |
||
1080 | * Set discount_total |
||
1081 | * @param string $value |
||
1082 | */ |
||
1083 | public function set_discount_total( $value ) { |
||
1086 | |||
1087 | /** |
||
1088 | * Set discount_tax |
||
1089 | * @param string $value |
||
1090 | */ |
||
1091 | public function set_discount_tax( $value ) { |
||
1094 | |||
1095 | /** |
||
1096 | * Set shipping_total |
||
1097 | * @param string $value |
||
1098 | */ |
||
1099 | public function set_shipping_total( $value ) { |
||
1102 | |||
1103 | /** |
||
1104 | * Set shipping_tax |
||
1105 | * @param string $value |
||
1106 | */ |
||
1107 | public function set_shipping_tax( $value ) { |
||
1111 | |||
1112 | /** |
||
1113 | * Set cart tax |
||
1114 | * @param string $value |
||
1115 | */ |
||
1116 | public function set_cart_tax( $value ) { |
||
1120 | |||
1121 | /** |
||
1122 | * Sets order tax (sum of cart and shipping tax). Used internaly only. |
||
1123 | * @access protected |
||
1124 | * @param string $value |
||
1125 | */ |
||
1126 | protected function set_order_tax( $value ) { |
||
1129 | |||
1130 | /** |
||
1131 | * Set order_total |
||
1132 | * @param string $value |
||
1133 | */ |
||
1134 | public function set_order_total( $value ) { |
||
1137 | |||
1138 | /** |
||
1139 | * Set the payment method. |
||
1140 | * @since 2.2.0 |
||
1141 | * @param string $value Supports WC_Payment_Gateway for bw compatibility with < 2.6 |
||
1142 | */ |
||
1143 | public function set_payment_method( $value ) { |
||
1151 | |||
1152 | /** |
||
1153 | * Set payment_method_title |
||
1154 | * @param string $value |
||
1155 | */ |
||
1156 | public function set_payment_method_title( $value ) { |
||
1159 | |||
1160 | /** |
||
1161 | * Set transaction_id |
||
1162 | * @param string $value |
||
1163 | */ |
||
1164 | public function set_transaction_id( $value ) { |
||
1167 | |||
1168 | /** |
||
1169 | * Set customer_ip_address |
||
1170 | * @param string $value |
||
1171 | */ |
||
1172 | public function set_customer_ip_address( $value ) { |
||
1175 | |||
1176 | /** |
||
1177 | * Set customer_user_agent |
||
1178 | * @param string $value |
||
1179 | */ |
||
1180 | public function set_customer_user_agent( $value ) { |
||
1183 | |||
1184 | /** |
||
1185 | * Set created_via |
||
1186 | * @param string $value |
||
1187 | */ |
||
1188 | public function set_created_via( $value ) { |
||
1191 | |||
1192 | /** |
||
1193 | * Set order_version |
||
1194 | * @param string $value |
||
1195 | */ |
||
1196 | public function set_order_version( $value ) { |
||
1199 | |||
1200 | /** |
||
1201 | * Set prices_include_tax |
||
1202 | * @param bool $value |
||
1203 | */ |
||
1204 | public function set_prices_include_tax( $value ) { |
||
1207 | |||
1208 | /** |
||
1209 | * Set customer_note |
||
1210 | * @param string $value |
||
1211 | */ |
||
1212 | public function set_customer_note( $value ) { |
||
1215 | |||
1216 | /** |
||
1217 | * Set date_completed |
||
1218 | * @param string $timestamp |
||
1219 | */ |
||
1220 | public function set_date_completed( $timestamp ) { |
||
1223 | |||
1224 | /** |
||
1225 | * Set date_paid |
||
1226 | * @param string $timestamp |
||
1227 | */ |
||
1228 | public function set_date_paid( $timestamp ) { |
||
1231 | |||
1232 | /* |
||
1233 | |-------------------------------------------------------------------------- |
||
1234 | | CRUD methods |
||
1235 | |-------------------------------------------------------------------------- |
||
1236 | | |
||
1237 | | Methods which create, read, update and delete orders from the database. |
||
1238 | | Written in abstract fashion so that the way orders are stored can be |
||
1239 | | changed more easily in the future. |
||
1240 | | |
||
1241 | | A save method is included for convenience (chooses update or create based |
||
1242 | | on if the order exists yet). |
||
1243 | | |
||
1244 | */ |
||
1245 | |||
1246 | /** |
||
1247 | * Insert data into the database. |
||
1248 | * @since 2.6.0 |
||
1249 | * @access protected |
||
1250 | */ |
||
1251 | public function create() { |
||
1303 | |||
1304 | /** |
||
1305 | * Read from the database. |
||
1306 | * @since 2.6.0 |
||
1307 | * @param int $id ID of object to read. |
||
1308 | */ |
||
1309 | public function read( $id ) { |
||
1380 | |||
1381 | /** |
||
1382 | * Update data in the database. |
||
1383 | * @since 2.6.0 |
||
1384 | * @access protected |
||
1385 | */ |
||
1386 | public function update() { |
||
1457 | |||
1458 | /** |
||
1459 | * Delete data from the database. |
||
1460 | * @since 2.6.0 |
||
1461 | */ |
||
1462 | public function delete() { |
||
1465 | |||
1466 | /** |
||
1467 | * Save data to the database. |
||
1468 | * @since 2.6.0 |
||
1469 | * @access protected |
||
1470 | */ |
||
1471 | public function save() { |
||
1479 | |||
1480 | /* |
||
1481 | |-------------------------------------------------------------------------- |
||
1482 | | Order Item Handling |
||
1483 | |-------------------------------------------------------------------------- |
||
1484 | | |
||
1485 | | Order items are used for products, taxes, shipping, and fees within |
||
1486 | | each order. |
||
1487 | | |
||
1488 | */ |
||
1489 | |||
1490 | /** |
||
1491 | * Return an array of items/products within this order. |
||
1492 | * |
||
1493 | * @param string|array $type Types of line items to get (array or string). |
||
1494 | * @return Array of WC_Order_item |
||
1495 | */ |
||
1496 | public function get_items( $type = 'line_item' ) { |
||
1512 | |||
1513 | /** |
||
1514 | * Get an order item object, based on it's type. |
||
1515 | * @param int $item_id |
||
1516 | * @return WC_Order_Item |
||
1517 | */ |
||
1518 | public function get_item( $item_id ) { |
||
1521 | |||
1522 | /** |
||
1523 | * Display meta data belonging to an item. @todo |
||
1524 | * @param array $item |
||
1525 | */ |
||
1526 | public function display_item_meta( $item ) { |
||
1531 | |||
1532 | /** |
||
1533 | * Return an array of fees within this order. |
||
1534 | * |
||
1535 | * @return array |
||
1536 | */ |
||
1537 | public function get_fees() { |
||
1540 | |||
1541 | /** |
||
1542 | * Return an array of taxes within this order. |
||
1543 | * |
||
1544 | * @return array |
||
1545 | */ |
||
1546 | public function get_taxes() { |
||
1549 | |||
1550 | /** |
||
1551 | * Return an array of shipping costs within this order. |
||
1552 | * |
||
1553 | * @return array |
||
1554 | */ |
||
1555 | public function get_shipping_methods() { |
||
1558 | |||
1559 | /** |
||
1560 | * Get coupon codes only. |
||
1561 | * |
||
1562 | * @return array |
||
1563 | */ |
||
1564 | public function get_used_coupons() { |
||
1567 | |||
1568 | /** |
||
1569 | * Gets the count of order items of a certain type. |
||
1570 | * |
||
1571 | * @param string $item_type |
||
1572 | * @return string |
||
1573 | */ |
||
1574 | public function get_item_count( $item_type = '' ) { |
||
1591 | |||
1592 | /** |
||
1593 | * Remove all line items (products, coupons, shipping, taxes) from the order. |
||
1594 | * |
||
1595 | * @param string $type Order item type. Default null. |
||
1596 | */ |
||
1597 | public function remove_order_items( $type = null ) { |
||
1608 | |||
1609 | /** |
||
1610 | * Add a product line item to the order. |
||
1611 | * Order must be saved prior to adding items. |
||
1612 | * |
||
1613 | * @since 2.2 |
||
1614 | * @param \WC_Product $product |
||
1615 | * @param int $qty Line item quantity. |
||
1616 | * @param array $args |
||
1617 | * @return int updated order item ID |
||
1618 | */ |
||
1619 | public function add_product( $product, $qty = 1, $args = array() ) { |
||
1643 | |||
1644 | /** |
||
1645 | * Update a line item for the order. |
||
1646 | * |
||
1647 | * Note this does not update order totals. |
||
1648 | * |
||
1649 | * @since 2.2 |
||
1650 | * @param object|int $item order item ID or item object. |
||
1651 | * @param WC_Product $product |
||
1652 | * @param array $args data to update. |
||
1653 | * @return int updated order item ID |
||
1654 | */ |
||
1655 | public function update_product( $item, $product, $args ) { |
||
1752 | |||
1753 | /** |
||
1754 | * Add coupon code to the order. |
||
1755 | * Order must be saved prior to adding items. |
||
1756 | * |
||
1757 | * @param string $code |
||
1758 | * @param int $discount_amount |
||
1759 | * @param int $discount_amount_tax "Discounted" tax - used for tax inclusive prices. |
||
1760 | * @return int updated order item ID |
||
1761 | */ |
||
1762 | public function add_coupon( $code, $discount_amount = 0, $discount_amount_tax = 0 ) { |
||
1775 | |||
1776 | /** |
||
1777 | * Update coupon for order. Note this does not update order totals. |
||
1778 | * @since 2.2 |
||
1779 | * @param object|int $item |
||
1780 | * @param array $args |
||
1781 | * @return int updated order item ID |
||
1782 | */ |
||
1783 | public function update_coupon( $item, $args ) { |
||
1822 | |||
1823 | /** |
||
1824 | * Add a shipping row to the order. |
||
1825 | * Order must be saved prior to adding items. |
||
1826 | * |
||
1827 | * @param WC_Shipping_Rate shipping_rate |
||
1828 | * @return int updated order item ID |
||
1829 | */ |
||
1830 | public function add_shipping( $shipping_rate ) { |
||
1846 | |||
1847 | /** |
||
1848 | * Update shipping method for order. |
||
1849 | * |
||
1850 | * Note this does not update the order total. |
||
1851 | * |
||
1852 | * @since 2.2 |
||
1853 | * @param object|int $item |
||
1854 | * @param array $args |
||
1855 | * @return int updated order item ID |
||
1856 | */ |
||
1857 | public function update_shipping( $item, $args ) { |
||
1915 | |||
1916 | /** |
||
1917 | * Add a fee to the order. |
||
1918 | * Order must be saved prior to adding items. |
||
1919 | * @param object $fee |
||
1920 | * @return int updated order item ID |
||
1921 | */ |
||
1922 | public function add_fee( $fee ) { |
||
1939 | |||
1940 | /** |
||
1941 | * Update fee for order. |
||
1942 | * |
||
1943 | * Note this does not update order totals. |
||
1944 | * |
||
1945 | * @since 2.2 |
||
1946 | * @param object|int $item |
||
1947 | * @param array $args |
||
1948 | * @return int updated order item ID |
||
1949 | */ |
||
1950 | public function update_fee( $item, $args ) { |
||
1999 | |||
2000 | /** |
||
2001 | * Add a tax row to the order. |
||
2002 | * Order must be saved prior to adding items. |
||
2003 | * @since 2.2 |
||
2004 | * @param int tax_rate_id |
||
2005 | * @return int updated order item ID |
||
2006 | */ |
||
2007 | public function add_tax( $tax_rate_id, $tax_amount = 0, $shipping_tax_amount = 0 ) { |
||
2027 | |||
2028 | /** |
||
2029 | * Update tax line on order. |
||
2030 | * Note this does not update order totals. |
||
2031 | * |
||
2032 | * @since 2.6 |
||
2033 | * @param object|int $item |
||
2034 | * @param array $args |
||
2035 | * @return int updated order item ID |
||
2036 | */ |
||
2037 | public function update_tax( $item, $args ) { |
||
2090 | |||
2091 | /** |
||
2092 | * Update tax lines at order level by looking at the line item taxes themselves. |
||
2093 | * @return bool success or fail. |
||
2094 | */ |
||
2095 | public function update_taxes() { |
||
2137 | |||
2138 | /* |
||
2139 | |-------------------------------------------------------------------------- |
||
2140 | | Calculations. |
||
2141 | |-------------------------------------------------------------------------- |
||
2142 | | |
||
2143 | | These methods calculate order totals and taxes based on the current data. |
||
2144 | | |
||
2145 | */ |
||
2146 | |||
2147 | /** |
||
2148 | * Calculate shipping total. |
||
2149 | * |
||
2150 | * @since 2.2 |
||
2151 | * @return float |
||
2152 | */ |
||
2153 | public function calculate_shipping() { |
||
2164 | |||
2165 | /** |
||
2166 | * Calculate taxes for all line items and shipping, and store the totals and tax rows. |
||
2167 | * |
||
2168 | * Will use the base country unless customer addresses are set. |
||
2169 | * |
||
2170 | * @return bool success or fail. |
||
2171 | */ |
||
2172 | public function calculate_taxes() { |
||
2280 | |||
2281 | /** |
||
2282 | * Calculate totals by looking at the contents of the order. Stores the totals and returns the orders final total. |
||
2283 | * |
||
2284 | * @since 2.2 |
||
2285 | * @param bool $and_taxes Calc taxes if true. |
||
2286 | * @return float calculated grand total. |
||
2287 | */ |
||
2288 | public function calculate_totals( $and_taxes = true ) { |
||
2322 | |||
2323 | /* |
||
2324 | |-------------------------------------------------------------------------- |
||
2325 | | Total Getters |
||
2326 | |-------------------------------------------------------------------------- |
||
2327 | | |
||
2328 | | Methods for getting totals e.g. for display on the frontend. |
||
2329 | | |
||
2330 | */ |
||
2331 | |||
2332 | /** |
||
2333 | * Get a product (either product or variation). |
||
2334 | * |
||
2335 | * @param object $item |
||
2336 | * @return WC_Product|bool |
||
2337 | */ |
||
2338 | public function get_product_from_item( $item ) { |
||
2346 | |||
2347 | /** |
||
2348 | * Get item subtotal - this is the cost before discount. |
||
2349 | * |
||
2350 | * @param object $item |
||
2351 | * @param bool $inc_tax (default: false). |
||
2352 | * @param bool $round (default: true). |
||
2353 | * @return float |
||
2354 | */ |
||
2355 | View Code Duplication | public function get_item_subtotal( $item, $inc_tax = false, $round = true ) { |
|
2370 | |||
2371 | /** |
||
2372 | * Get line subtotal - this is the cost before discount. |
||
2373 | * |
||
2374 | * @param object $item |
||
2375 | * @param bool $inc_tax (default: false). |
||
2376 | * @param bool $round (default: true). |
||
2377 | * @return float |
||
2378 | */ |
||
2379 | View Code Duplication | public function get_line_subtotal( $item, $inc_tax = false, $round = true ) { |
|
2394 | |||
2395 | /** |
||
2396 | * Calculate item cost - useful for gateways. |
||
2397 | * |
||
2398 | * @param object $item |
||
2399 | * @param bool $inc_tax (default: false). |
||
2400 | * @param bool $round (default: true). |
||
2401 | * @return float |
||
2402 | */ |
||
2403 | View Code Duplication | public function get_item_total( $item, $inc_tax = false, $round = true ) { |
|
2418 | |||
2419 | /** |
||
2420 | * Calculate line total - useful for gateways. |
||
2421 | * |
||
2422 | * @param object $item |
||
2423 | * @param bool $inc_tax (default: false). |
||
2424 | * @param bool $round (default: true). |
||
2425 | * @return float |
||
2426 | */ |
||
2427 | View Code Duplication | public function get_line_total( $item, $inc_tax = false, $round = true ) { |
|
2440 | |||
2441 | /** |
||
2442 | * Get item tax - useful for gateways. |
||
2443 | * |
||
2444 | * @param mixed $item |
||
2445 | * @param bool $round (default: true). |
||
2446 | * @return float |
||
2447 | */ |
||
2448 | public function get_item_tax( $item, $round = true ) { |
||
2458 | |||
2459 | /** |
||
2460 | * Get line tax - useful for gateways. |
||
2461 | * |
||
2462 | * @param mixed $item |
||
2463 | * @return float |
||
2464 | */ |
||
2465 | public function get_line_tax( $item ) { |
||
2468 | |||
2469 | /** |
||
2470 | * Gets line subtotal - formatted for display. |
||
2471 | * |
||
2472 | * @param array $item |
||
2473 | * @param string $tax_display |
||
2474 | * @return string |
||
2475 | */ |
||
2476 | public function get_formatted_line_subtotal( $item, $tax_display = '' ) { |
||
2493 | |||
2494 | /** |
||
2495 | * Gets order total - formatted for display. |
||
2496 | * |
||
2497 | * @return string |
||
2498 | */ |
||
2499 | public function get_formatted_order_total() { |
||
2503 | |||
2504 | /** |
||
2505 | * Gets subtotal - subtotal is shown before discounts, but with localised taxes. |
||
2506 | * |
||
2507 | * @param bool $compound (default: false). |
||
2508 | * @param string $tax_display (default: the tax_display_cart value). |
||
2509 | * @return string |
||
2510 | */ |
||
2511 | public function get_subtotal_to_display( $compound = false, $tax_display = '' ) { |
||
2557 | |||
2558 | /** |
||
2559 | * Gets shipping (formatted). |
||
2560 | * |
||
2561 | * @return string |
||
2562 | */ |
||
2563 | public function get_shipping_to_display( $tax_display = '' ) { |
||
2598 | |||
2599 | /** |
||
2600 | * Get the discount amount (formatted). |
||
2601 | * @since 2.3.0 |
||
2602 | * @return string |
||
2603 | */ |
||
2604 | public function get_discount_to_display( $tax_display = '' ) { |
||
2608 | |||
2609 | /** |
||
2610 | * Get totals for display on pages and in emails. |
||
2611 | * |
||
2612 | * @param mixed $tax_display |
||
2613 | * @return array |
||
2614 | */ |
||
2615 | public function get_order_item_totals( $tax_display = '' ) { |
||
2697 | |||
2698 | /* |
||
2699 | |-------------------------------------------------------------------------- |
||
2700 | | Conditionals |
||
2701 | |-------------------------------------------------------------------------- |
||
2702 | | |
||
2703 | | Checks if a condition is true or false. |
||
2704 | | |
||
2705 | */ |
||
2706 | |||
2707 | /** |
||
2708 | * Checks the order status against a passed in status. |
||
2709 | * |
||
2710 | * @return bool |
||
2711 | */ |
||
2712 | public function has_status( $status ) { |
||
2715 | |||
2716 | /** |
||
2717 | * has_meta function for order items. |
||
2718 | * |
||
2719 | * @param string $order_item_id |
||
2720 | * @return array of meta data. |
||
2721 | */ |
||
2722 | public function has_meta( $order_item_id ) { |
||
2729 | |||
2730 | /** |
||
2731 | * Check whether this order has a specific shipping method or not. |
||
2732 | * |
||
2733 | * @param string $method_id |
||
2734 | * @return bool |
||
2735 | */ |
||
2736 | public function has_shipping_method( $method_id ) { |
||
2744 | |||
2745 | /** |
||
2746 | * Check if an order key is valid. |
||
2747 | * |
||
2748 | * @param mixed $key |
||
2749 | * @return bool |
||
2750 | */ |
||
2751 | public function key_is_valid( $key ) { |
||
2754 | |||
2755 | /** |
||
2756 | * Returns true if the order contains a free product. |
||
2757 | * @since 2.5.0 |
||
2758 | * @return bool |
||
2759 | */ |
||
2760 | public function has_free_item() { |
||
2768 | } |
||
2769 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.