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_Product 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_Product, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class WC_Product { |
||
37 | |||
38 | /** |
||
39 | * The product (post) ID. |
||
40 | * |
||
41 | * @var int |
||
42 | */ |
||
43 | public $id = 0; |
||
44 | |||
45 | /** |
||
46 | * $post Stores post data. |
||
47 | * |
||
48 | * @var $post WP_Post |
||
49 | */ |
||
50 | public $post = null; |
||
51 | |||
52 | /** |
||
53 | * The product's type (simple, variable etc). |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | public $product_type = null; |
||
58 | |||
59 | /** |
||
60 | * Product shipping class. |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $shipping_class = ''; |
||
65 | |||
66 | /** |
||
67 | * ID of the shipping class this product has. |
||
68 | * |
||
69 | * @var int |
||
70 | */ |
||
71 | protected $shipping_class_id = 0; |
||
72 | |||
73 | /** @public string The product's total stock, including that of its children. */ |
||
74 | public $total_stock; |
||
75 | |||
76 | /** |
||
77 | * Supported features such as 'ajax_add_to_cart'. |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $supports = array(); |
||
81 | |||
82 | /** |
||
83 | * Constructor gets the post object and sets the ID for the loaded product. |
||
84 | * |
||
85 | * @param int|WC_Product|object $product Product ID, post object, or product object |
||
86 | */ |
||
87 | public function __construct( $product ) { |
||
99 | |||
100 | /** |
||
101 | * __isset function. |
||
102 | * |
||
103 | * @param mixed $key |
||
104 | * @return bool |
||
105 | */ |
||
106 | public function __isset( $key ) { |
||
109 | |||
110 | /** |
||
111 | * __get function. |
||
112 | * |
||
113 | * @param string $key |
||
114 | * @return mixed |
||
115 | */ |
||
116 | public function __get( $key ) { |
||
146 | |||
147 | /** |
||
148 | * Get the product's post data. |
||
149 | * |
||
150 | * @return object |
||
151 | */ |
||
152 | public function get_post_data() { |
||
155 | |||
156 | /** |
||
157 | * Check if a product supports a given feature. |
||
158 | * |
||
159 | * Product classes should override this to declare support (or lack of support) for a feature. |
||
160 | * |
||
161 | * @param string $feature string The name of a feature to test support for. |
||
162 | * @return bool True if the product supports the feature, false otherwise. |
||
163 | * @since 2.5.0 |
||
164 | */ |
||
165 | public function supports( $feature ) { |
||
168 | |||
169 | /** |
||
170 | * Return the product ID |
||
171 | * |
||
172 | * @since 2.5.0 |
||
173 | * @return int product (post) ID |
||
174 | */ |
||
175 | public function get_id() { |
||
179 | |||
180 | /** |
||
181 | * Returns the gallery attachment ids. |
||
182 | * |
||
183 | * @return array |
||
184 | */ |
||
185 | public function get_gallery_attachment_ids() { |
||
188 | |||
189 | /** |
||
190 | * Wrapper for get_permalink. |
||
191 | * |
||
192 | * @return string |
||
193 | */ |
||
194 | public function get_permalink() { |
||
197 | |||
198 | /** |
||
199 | * Get SKU (Stock-keeping unit) - product unique ID. |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | public function get_sku() { |
||
206 | |||
207 | /** |
||
208 | * Returns number of items available for sale. |
||
209 | * |
||
210 | * @return int |
||
211 | */ |
||
212 | public function get_stock_quantity() { |
||
215 | |||
216 | /** |
||
217 | * Get total stock. |
||
218 | * |
||
219 | * This is the stock of parent and children combined. |
||
220 | * |
||
221 | * @return int |
||
222 | */ |
||
223 | public function get_total_stock() { |
||
240 | |||
241 | /** |
||
242 | * Check if the stock status needs changing. |
||
243 | */ |
||
244 | public function check_stock_status() { |
||
255 | |||
256 | /** |
||
257 | * Set stock level of the product. |
||
258 | * |
||
259 | * Uses queries rather than update_post_meta so we can do this in one query (to avoid stock issues). |
||
260 | * We cannot rely on the original loaded value in case another order was made since then. |
||
261 | * |
||
262 | * @param int $amount (default: null) |
||
263 | * @param string $mode can be set, add, or subtract |
||
264 | * @return int new stock level |
||
265 | */ |
||
266 | public function set_stock( $amount = null, $mode = 'set' ) { |
||
302 | |||
303 | /** |
||
304 | * Reduce stock level of the product. |
||
305 | * |
||
306 | * @param int $amount Amount to reduce by. Default: 1 |
||
307 | * @return int new stock level |
||
308 | */ |
||
309 | public function reduce_stock( $amount = 1 ) { |
||
312 | |||
313 | /** |
||
314 | * Increase stock level of the product. |
||
315 | * |
||
316 | * @param int $amount Amount to increase by. Default 1. |
||
317 | * @return int new stock level |
||
318 | */ |
||
319 | public function increase_stock( $amount = 1 ) { |
||
322 | |||
323 | /** |
||
324 | * Set stock status of the product. |
||
325 | * |
||
326 | * @param string $status |
||
327 | */ |
||
328 | public function set_stock_status( $status ) { |
||
344 | |||
345 | /** |
||
346 | * Return the product type. |
||
347 | * |
||
348 | * @return string |
||
349 | */ |
||
350 | public function get_type() { |
||
353 | |||
354 | /** |
||
355 | * Checks the product type. |
||
356 | * |
||
357 | * Backwards compat with downloadable/virtual. |
||
358 | * |
||
359 | * @param string $type Array or string of types |
||
360 | * @return bool |
||
361 | */ |
||
362 | public function is_type( $type ) { |
||
365 | |||
366 | /** |
||
367 | * Checks if a product is downloadable. |
||
368 | * |
||
369 | * @return bool |
||
370 | */ |
||
371 | public function is_downloadable() { |
||
374 | |||
375 | /** |
||
376 | * Check if downloadable product has a file attached. |
||
377 | * |
||
378 | * @since 1.6.2 |
||
379 | * |
||
380 | * @param string $download_id file identifier |
||
381 | * @return bool Whether downloadable product has a file attached. |
||
382 | */ |
||
383 | public function has_file( $download_id = '' ) { |
||
386 | |||
387 | /** |
||
388 | * Gets an array of downloadable files for this product. |
||
389 | * |
||
390 | * @since 2.1.0 |
||
391 | * |
||
392 | * @return array |
||
393 | */ |
||
394 | public function get_files() { |
||
421 | |||
422 | /** |
||
423 | * Get a file by $download_id. |
||
424 | * |
||
425 | * @param string $download_id file identifier |
||
426 | * @return array|false if not found |
||
427 | */ |
||
428 | public function get_file( $download_id = '' ) { |
||
443 | |||
444 | /** |
||
445 | * Get file download path identified by $download_id. |
||
446 | * |
||
447 | * @param string $download_id file identifier |
||
448 | * @return string |
||
449 | */ |
||
450 | public function get_file_download_path( $download_id ) { |
||
462 | |||
463 | /** |
||
464 | * Checks if a product is virtual (has no shipping). |
||
465 | * |
||
466 | * @return bool |
||
467 | */ |
||
468 | public function is_virtual() { |
||
471 | |||
472 | /** |
||
473 | * Checks if a product needs shipping. |
||
474 | * |
||
475 | * @return bool |
||
476 | */ |
||
477 | public function needs_shipping() { |
||
480 | |||
481 | /** |
||
482 | * Check if a product is sold individually (no quantities). |
||
483 | * |
||
484 | * @return bool |
||
485 | */ |
||
486 | public function is_sold_individually() { |
||
496 | |||
497 | /** |
||
498 | * Returns the child product. |
||
499 | * |
||
500 | * @param mixed $child_id |
||
501 | * @return WC_Product|WC_Product|WC_Product_variation |
||
502 | */ |
||
503 | public function get_child( $child_id ) { |
||
506 | |||
507 | /** |
||
508 | * Returns the children. |
||
509 | * |
||
510 | * @return array |
||
511 | */ |
||
512 | public function get_children() { |
||
515 | |||
516 | /** |
||
517 | * Returns whether or not the product has any child product. |
||
518 | * |
||
519 | * @return bool |
||
520 | */ |
||
521 | public function has_child() { |
||
524 | |||
525 | /** |
||
526 | * Returns whether or not the product post exists. |
||
527 | * |
||
528 | * @return bool |
||
529 | */ |
||
530 | public function exists() { |
||
533 | |||
534 | /** |
||
535 | * Returns whether or not the product is taxable. |
||
536 | * |
||
537 | * @return bool |
||
538 | */ |
||
539 | public function is_taxable() { |
||
543 | |||
544 | /** |
||
545 | * Returns whether or not the product shipping is taxable. |
||
546 | * |
||
547 | * @return bool |
||
548 | */ |
||
549 | public function is_shipping_taxable() { |
||
552 | |||
553 | /** |
||
554 | * Get the title of the post. |
||
555 | * |
||
556 | * @return string |
||
557 | */ |
||
558 | public function get_title() { |
||
561 | |||
562 | /** |
||
563 | * Get the parent of the post. |
||
564 | * |
||
565 | * @return int |
||
566 | */ |
||
567 | public function get_parent() { |
||
570 | |||
571 | /** |
||
572 | * Get the add to url used mainly in loops. |
||
573 | * |
||
574 | * @return string |
||
575 | */ |
||
576 | public function add_to_cart_url() { |
||
579 | |||
580 | /** |
||
581 | * Get the add to cart button text for the single page. |
||
582 | * |
||
583 | * @return string |
||
584 | */ |
||
585 | public function single_add_to_cart_text() { |
||
588 | |||
589 | /** |
||
590 | * Get the add to cart button text. |
||
591 | * |
||
592 | * @return string |
||
593 | */ |
||
594 | public function add_to_cart_text() { |
||
597 | |||
598 | /** |
||
599 | * Returns whether or not the product is stock managed. |
||
600 | * |
||
601 | * @return bool |
||
602 | */ |
||
603 | public function managing_stock() { |
||
606 | |||
607 | /** |
||
608 | * Returns whether or not the product is in stock. |
||
609 | * |
||
610 | * @return bool |
||
611 | */ |
||
612 | View Code Duplication | public function is_in_stock() { |
|
628 | |||
629 | /** |
||
630 | * Returns whether or not the product can be backordered. |
||
631 | * |
||
632 | * @return bool |
||
633 | */ |
||
634 | public function backorders_allowed() { |
||
637 | |||
638 | /** |
||
639 | * Returns whether or not the product needs to notify the customer on backorder. |
||
640 | * |
||
641 | * @return bool |
||
642 | */ |
||
643 | public function backorders_require_notification() { |
||
646 | |||
647 | /** |
||
648 | * Check if a product is on backorder. |
||
649 | * |
||
650 | * @param int $qty_in_cart (default: 0) |
||
651 | * @return bool |
||
652 | */ |
||
653 | public function is_on_backorder( $qty_in_cart = 0 ) { |
||
656 | |||
657 | /** |
||
658 | * Returns whether or not the product has enough stock for the order. |
||
659 | * |
||
660 | * @param mixed $quantity |
||
661 | * @return bool |
||
662 | */ |
||
663 | public function has_enough_stock( $quantity ) { |
||
666 | |||
667 | /** |
||
668 | * Returns the availability of the product. |
||
669 | * |
||
670 | * @return string |
||
671 | */ |
||
672 | public function get_availability() { |
||
673 | // Default to in-stock |
||
674 | $availability = __( 'In stock', 'woocommerce' ); |
||
675 | $class = 'in-stock'; |
||
676 | |||
677 | // If out of stock, this takes priority over all other settings. |
||
678 | if ( ! $this->is_in_stock() ) { |
||
679 | $availability = __( 'Out of stock', 'woocommerce' ); |
||
680 | $class = 'out-of-stock'; |
||
681 | |||
682 | // Any further we can assume status is set to in stock. |
||
683 | } elseif ( $this->managing_stock() && $this->is_on_backorder( 1 ) ) { |
||
684 | $availability = __( 'Available on backorder', 'woocommerce' ); |
||
685 | $class = 'available-on-backorder'; |
||
686 | |||
687 | View Code Duplication | } elseif ( $this->managing_stock() ) { |
|
688 | switch ( get_option( 'woocommerce_stock_format' ) ) { |
||
689 | case 'no_amount' : |
||
690 | $availability = __( 'In stock', 'woocommerce' ); |
||
691 | break; |
||
692 | case 'low_amount' : |
||
693 | if ( $this->get_total_stock() <= get_option( 'woocommerce_notify_low_stock_amount' ) ) { |
||
694 | $availability = sprintf( __( 'Only %s left in stock', 'woocommerce' ), $this->get_total_stock() ); |
||
695 | |||
696 | if ( $this->backorders_allowed() && $this->backorders_require_notification() ) { |
||
697 | $availability .= ' ' . __( '(also available on backorder)', 'woocommerce' ); |
||
698 | } |
||
699 | } else { |
||
700 | $availability = __( 'In stock', 'woocommerce' ); |
||
701 | } |
||
702 | break; |
||
703 | default : |
||
704 | $availability = sprintf( __( '%s in stock', 'woocommerce' ), $this->get_total_stock() ); |
||
705 | |||
706 | if ( $this->backorders_allowed() && $this->backorders_require_notification() ) { |
||
707 | $availability .= ' ' . __( '(also available on backorder)', 'woocommerce' ); |
||
708 | } |
||
709 | break; |
||
710 | } |
||
711 | } |
||
712 | |||
713 | return apply_filters( 'woocommerce_get_availability', array( 'availability' => $availability, 'class' => $class ), $this ); |
||
714 | } |
||
715 | |||
716 | /** |
||
717 | * Returns whether or not the product is featured. |
||
718 | * |
||
719 | * @return bool |
||
720 | */ |
||
721 | public function is_featured() { |
||
724 | |||
725 | /** |
||
726 | * Returns whether or not the product is visible in the catalog. |
||
727 | * |
||
728 | * @return bool |
||
729 | */ |
||
730 | public function is_visible() { |
||
757 | |||
758 | /** |
||
759 | * Returns whether or not the product is on sale. |
||
760 | * |
||
761 | * @return bool |
||
762 | */ |
||
763 | public function is_on_sale() { |
||
766 | |||
767 | /** |
||
768 | * Returns false if the product cannot be bought. |
||
769 | * |
||
770 | * @return bool |
||
771 | */ |
||
772 | public function is_purchasable() { |
||
791 | |||
792 | /** |
||
793 | * Set a products price dynamically. |
||
794 | * |
||
795 | * @param float $price Price to set. |
||
796 | */ |
||
797 | public function set_price( $price ) { |
||
800 | |||
801 | /** |
||
802 | * Adjust a products price dynamically. |
||
803 | * |
||
804 | * @param mixed $price |
||
805 | */ |
||
806 | public function adjust_price( $price ) { |
||
809 | |||
810 | /** |
||
811 | * Returns the product's sale price. |
||
812 | * |
||
813 | * @return string price |
||
814 | */ |
||
815 | public function get_sale_price() { |
||
818 | |||
819 | /** |
||
820 | * Returns the product's regular price. |
||
821 | * |
||
822 | * @return string price |
||
823 | */ |
||
824 | public function get_regular_price() { |
||
827 | |||
828 | /** |
||
829 | * Returns the product's active price. |
||
830 | * |
||
831 | * @return string price |
||
832 | */ |
||
833 | public function get_price() { |
||
836 | |||
837 | /** |
||
838 | * Returns the price (including tax). Uses customer tax rates. Can work for a specific $qty for more accurate taxes. |
||
839 | * |
||
840 | * @param int $qty |
||
841 | * @param string $price to calculate, left blank to just use get_price() |
||
842 | * @return string |
||
843 | */ |
||
844 | public function get_price_including_tax( $qty = 1, $price = '' ) { |
||
895 | |||
896 | /** |
||
897 | * Returns the price (excluding tax) - ignores tax_class filters since the price may *include* tax and thus needs subtracting. |
||
898 | * Uses store base tax rates. Can work for a specific $qty for more accurate taxes. |
||
899 | * |
||
900 | * @param int $qty |
||
901 | * @param string $price to calculate, left blank to just use get_price() |
||
902 | * @return string |
||
903 | */ |
||
904 | public function get_price_excluding_tax( $qty = 1, $price = '' ) { |
||
920 | |||
921 | /** |
||
922 | * Returns the price including or excluding tax, based on the 'woocommerce_tax_display_shop' setting. |
||
923 | * |
||
924 | * @param string $price to calculate, left blank to just use get_price() |
||
925 | * @param integer $qty passed on to get_price_including_tax() or get_price_excluding_tax() |
||
926 | * @return string |
||
927 | */ |
||
928 | public function get_display_price( $price = '', $qty = 1 ) { |
||
939 | |||
940 | /** |
||
941 | * Get the suffix to display after prices > 0. |
||
942 | * |
||
943 | * @param string $price to calculate, left blank to just use get_price() |
||
944 | * @param integer $qty passed on to get_price_including_tax() or get_price_excluding_tax() |
||
945 | * @return string |
||
946 | */ |
||
947 | public function get_price_suffix( $price = '', $qty = 1 ) { |
||
974 | |||
975 | /** |
||
976 | * Returns the price in html format. |
||
977 | * |
||
978 | * @param string $price (default: '') |
||
979 | * @return string |
||
980 | */ |
||
981 | public function get_price_html( $price = '' ) { |
||
1025 | |||
1026 | /** |
||
1027 | * Functions for getting parts of a price, in html, used by get_price_html. |
||
1028 | * |
||
1029 | * @return string |
||
1030 | */ |
||
1031 | public function get_price_html_from_text() { |
||
1036 | |||
1037 | /** |
||
1038 | * Functions for getting parts of a price, in html, used by get_price_html. |
||
1039 | * |
||
1040 | * @param string $from String or float to wrap with 'from' text |
||
1041 | * @param mixed $to String or float to wrap with 'to' text |
||
1042 | * @return string |
||
1043 | */ |
||
1044 | public function get_price_html_from_to( $from, $to ) { |
||
1049 | |||
1050 | /** |
||
1051 | * Returns the tax class. |
||
1052 | * |
||
1053 | * @return string |
||
1054 | */ |
||
1055 | public function get_tax_class() { |
||
1058 | |||
1059 | /** |
||
1060 | * Returns the tax status. |
||
1061 | * |
||
1062 | * @return string |
||
1063 | */ |
||
1064 | public function get_tax_status() { |
||
1067 | |||
1068 | /** |
||
1069 | * Get the average rating of product. This is calculated once and stored in postmeta. |
||
1070 | * @return string |
||
1071 | */ |
||
1072 | public function get_average_rating() { |
||
1080 | |||
1081 | /** |
||
1082 | * Get the total amount (COUNT) of ratings. |
||
1083 | * @param int $value Optional. Rating value to get the count for. By default returns the count of all rating values. |
||
1084 | * @return int |
||
1085 | */ |
||
1086 | public function get_rating_count( $value = null ) { |
||
1100 | |||
1101 | /** |
||
1102 | * Sync product rating. Can be called statically. |
||
1103 | * @param int $post_id |
||
1104 | */ |
||
1105 | public static function sync_average_rating( $post_id ) { |
||
1129 | |||
1130 | /** |
||
1131 | * Sync product rating count. Can be called statically. |
||
1132 | * @param int $post_id |
||
1133 | */ |
||
1134 | public static function sync_rating_count( $post_id ) { |
||
1154 | |||
1155 | /** |
||
1156 | * Returns the product rating in html format. |
||
1157 | * |
||
1158 | * @param string $rating (default: '') |
||
1159 | * |
||
1160 | * @return string |
||
1161 | */ |
||
1162 | public function get_rating_html( $rating = null ) { |
||
1180 | |||
1181 | /** |
||
1182 | * Get the total amount (COUNT) of reviews. |
||
1183 | * |
||
1184 | * @since 2.3.2 |
||
1185 | * @return int The total numver of product reviews |
||
1186 | */ |
||
1187 | public function get_review_count() { |
||
1206 | |||
1207 | /** |
||
1208 | * Returns the upsell product ids. |
||
1209 | * |
||
1210 | * @return array |
||
1211 | */ |
||
1212 | public function get_upsells() { |
||
1215 | |||
1216 | /** |
||
1217 | * Returns the cross sell product ids. |
||
1218 | * |
||
1219 | * @return array |
||
1220 | */ |
||
1221 | public function get_cross_sells() { |
||
1224 | |||
1225 | /** |
||
1226 | * Returns the product categories. |
||
1227 | * |
||
1228 | * @param string $sep (default: ', ') |
||
1229 | * @param string $before (default: '') |
||
1230 | * @param string $after (default: '') |
||
1231 | * @return string |
||
1232 | */ |
||
1233 | public function get_categories( $sep = ', ', $before = '', $after = '' ) { |
||
1236 | |||
1237 | /** |
||
1238 | * Returns the product tags. |
||
1239 | * |
||
1240 | * @param string $sep (default: ', ') |
||
1241 | * @param string $before (default: '') |
||
1242 | * @param string $after (default: '') |
||
1243 | * @return array |
||
1244 | */ |
||
1245 | public function get_tags( $sep = ', ', $before = '', $after = '' ) { |
||
1248 | |||
1249 | /** |
||
1250 | * Returns the product shipping class. |
||
1251 | * |
||
1252 | * @return string |
||
1253 | */ |
||
1254 | View Code Duplication | public function get_shipping_class() { |
|
1270 | |||
1271 | /** |
||
1272 | * Returns the product shipping class ID. |
||
1273 | * |
||
1274 | * @return int |
||
1275 | */ |
||
1276 | View Code Duplication | public function get_shipping_class_id() { |
|
1291 | |||
1292 | /** |
||
1293 | * Get and return related products. |
||
1294 | * |
||
1295 | * Notes: |
||
1296 | * - Results are cached in a transient for faster queries. |
||
1297 | * - To make results appear random, we query and extra 10 products and shuffle them. |
||
1298 | * - To ensure we always have enough results, it will check $limit before returning the cached result, if not recalc. |
||
1299 | * - This used to rely on transient version to invalidate cache, but to avoid multiple transients we now just expire daily. |
||
1300 | * This means if a related product is edited and no longer related, it won't be removed for 24 hours. Acceptable trade-off for performance. |
||
1301 | * - Saving a product will flush caches for that product. |
||
1302 | * |
||
1303 | * @param int $limit (default: 5) Should be an integer greater than 0. |
||
1304 | * @return array Array of post IDs |
||
1305 | */ |
||
1306 | public function get_related( $limit = 5 ) { |
||
1342 | |||
1343 | /** |
||
1344 | * Returns a single product attribute. |
||
1345 | * |
||
1346 | * @param mixed $attr |
||
1347 | * @return string |
||
1348 | */ |
||
1349 | public function get_attribute( $attr ) { |
||
1372 | |||
1373 | /** |
||
1374 | * Returns product attributes. |
||
1375 | * |
||
1376 | * @return array |
||
1377 | */ |
||
1378 | public function get_attributes() { |
||
1393 | |||
1394 | /** |
||
1395 | * Returns whether or not the product has any attributes set. |
||
1396 | * |
||
1397 | * @return boolean |
||
1398 | */ |
||
1399 | public function has_attributes() { |
||
1413 | |||
1414 | /** |
||
1415 | * Returns whether or not we are showing dimensions on the product page. |
||
1416 | * |
||
1417 | * @return bool |
||
1418 | */ |
||
1419 | public function enable_dimensions_display() { |
||
1422 | |||
1423 | /** |
||
1424 | * Returns whether or not the product has dimensions set. |
||
1425 | * |
||
1426 | * @return bool |
||
1427 | */ |
||
1428 | public function has_dimensions() { |
||
1431 | |||
1432 | /** |
||
1433 | * Returns the product length. |
||
1434 | * @return string |
||
1435 | */ |
||
1436 | public function get_length() { |
||
1439 | |||
1440 | /** |
||
1441 | * Returns the product width. |
||
1442 | * @return string |
||
1443 | */ |
||
1444 | public function get_width() { |
||
1447 | |||
1448 | /** |
||
1449 | * Returns the product height. |
||
1450 | * @return string |
||
1451 | */ |
||
1452 | public function get_height() { |
||
1455 | |||
1456 | /** |
||
1457 | * Returns the product's weight. |
||
1458 | * @todo refactor filters in this class to naming woocommerce_product_METHOD |
||
1459 | * @return string |
||
1460 | */ |
||
1461 | public function get_weight() { |
||
1464 | |||
1465 | /** |
||
1466 | * Returns whether or not the product has weight set. |
||
1467 | * |
||
1468 | * @return bool |
||
1469 | */ |
||
1470 | public function has_weight() { |
||
1473 | |||
1474 | /** |
||
1475 | * Returns formatted dimensions. |
||
1476 | * @return string |
||
1477 | */ |
||
1478 | public function get_dimensions() { |
||
1491 | |||
1492 | /** |
||
1493 | * Lists a table of attributes for the product page. |
||
1494 | */ |
||
1495 | public function list_attributes() { |
||
1500 | |||
1501 | /** |
||
1502 | * Gets the main product image ID. |
||
1503 | * |
||
1504 | * @return int |
||
1505 | */ |
||
1506 | public function get_image_id() { |
||
1518 | |||
1519 | /** |
||
1520 | * Returns the main product image. |
||
1521 | * |
||
1522 | * @param string $size (default: 'shop_thumbnail') |
||
1523 | * @param array $attr |
||
1524 | * @param bool True to return $placeholder if no image is found, or false to return an empty string. |
||
1525 | * @return string |
||
1526 | */ |
||
1527 | public function get_image( $size = 'shop_thumbnail', $attr = array(), $placeholder = true ) { |
||
1540 | |||
1541 | /** |
||
1542 | * Get product name with SKU or ID. Used within admin. |
||
1543 | * |
||
1544 | * @return string Formatted product name |
||
1545 | */ |
||
1546 | public function get_formatted_name() { |
||
1555 | |||
1556 | /** |
||
1557 | * Retrieves related product terms. |
||
1558 | * |
||
1559 | * @param string $term |
||
1560 | * @return array |
||
1561 | */ |
||
1562 | protected function get_related_terms( $term ) { |
||
1572 | |||
1573 | /** |
||
1574 | * Builds the related posts query. |
||
1575 | * |
||
1576 | * @param array $cats_array |
||
1577 | * @param array $tags_array |
||
1578 | * @param array $exclude_ids |
||
1579 | * @param int $limit |
||
1580 | * @return string |
||
1581 | */ |
||
1582 | protected function build_related_query( $cats_array, $tags_array, $exclude_ids, $limit ) { |
||
1633 | } |
||
1634 |
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.