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_Coupon 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_Coupon, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class WC_Coupon extends WC_Legacy_Coupon { |
||
20 | |||
21 | /** |
||
22 | * Data array, with defaults. |
||
23 | * @since 2.7.0 |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $data = array( |
||
27 | 'code' => '', |
||
28 | 'amount' => 0, |
||
29 | 'date_created' => '', |
||
30 | 'date_modified' => '', |
||
31 | 'discount_type' => 'fixed_cart', |
||
32 | 'description' => '', |
||
33 | 'date_expires' => '', |
||
34 | 'usage_count' => 0, |
||
35 | 'individual_use' => false, |
||
36 | 'product_ids' => array(), |
||
37 | 'excluded_product_ids' => array(), |
||
38 | 'usage_limit' => 0, |
||
39 | 'usage_limit_per_user' => 0, |
||
40 | 'limit_usage_to_x_items' => 0, |
||
41 | 'free_shipping' => false, |
||
42 | 'product_categories' => array(), |
||
43 | 'excluded_product_categories' => array(), |
||
44 | 'exclude_sale_items' => false, |
||
45 | 'minimum_amount' => '', |
||
46 | 'maximum_amount' => '', |
||
47 | 'email_restrictions' => array(), |
||
48 | 'used_by' => '', |
||
49 | ); |
||
50 | |||
51 | // Coupon message codes |
||
52 | const E_WC_COUPON_INVALID_FILTERED = 100; |
||
53 | const E_WC_COUPON_INVALID_REMOVED = 101; |
||
54 | const E_WC_COUPON_NOT_YOURS_REMOVED = 102; |
||
55 | const E_WC_COUPON_ALREADY_APPLIED = 103; |
||
56 | const E_WC_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY = 104; |
||
57 | const E_WC_COUPON_NOT_EXIST = 105; |
||
58 | const E_WC_COUPON_USAGE_LIMIT_REACHED = 106; |
||
59 | const E_WC_COUPON_EXPIRED = 107; |
||
60 | const E_WC_COUPON_MIN_SPEND_LIMIT_NOT_MET = 108; |
||
61 | const E_WC_COUPON_NOT_APPLICABLE = 109; |
||
62 | const E_WC_COUPON_NOT_VALID_SALE_ITEMS = 110; |
||
63 | const E_WC_COUPON_PLEASE_ENTER = 111; |
||
64 | const E_WC_COUPON_MAX_SPEND_LIMIT_MET = 112; |
||
65 | const E_WC_COUPON_EXCLUDED_PRODUCTS = 113; |
||
66 | const E_WC_COUPON_EXCLUDED_CATEGORIES = 114; |
||
67 | const WC_COUPON_SUCCESS = 200; |
||
68 | const WC_COUPON_REMOVED = 201; |
||
69 | |||
70 | /** |
||
71 | * Internal meta type used to store coupon data. |
||
72 | * @since 2.7.0 |
||
73 | * @var string |
||
74 | */ |
||
75 | protected $meta_type = 'post'; |
||
76 | |||
77 | /** |
||
78 | * Data stored in meta keys, but not considered "meta" for a coupon. |
||
79 | * @since 2.7.0 |
||
80 | * @var array |
||
81 | */ |
||
82 | protected $internal_meta_keys = array( |
||
83 | 'discount_type', |
||
84 | 'coupon_amount', |
||
85 | 'expiry_date', |
||
86 | 'usage_count', |
||
87 | 'individual_use', |
||
88 | 'product_ids', |
||
89 | 'exclude_product_ids', |
||
90 | 'usage_limit', |
||
91 | 'usage_limit_per_user', |
||
92 | 'limit_usage_to_x_items', |
||
93 | 'free_shipping', |
||
94 | 'product_categories', |
||
95 | 'exclude_product_categories', |
||
96 | 'exclude_sale_items', |
||
97 | 'minimum_amount', |
||
98 | 'maximum_amount', |
||
99 | 'customer_email', |
||
100 | '_used_by', |
||
101 | '_edit_lock', |
||
102 | '_edit_last', |
||
103 | ); |
||
104 | |||
105 | /** |
||
106 | * Coupon constructor. Loads coupon data. |
||
107 | * @param mixed $data Coupon data, object, ID or code. |
||
108 | */ |
||
109 | public function __construct( $data = '' ) { |
||
110 | parent::__construct( $data ); |
||
111 | |||
112 | if ( $data instanceof WC_Coupon ) { |
||
113 | $this->read( absint( $data->get_id() ) ); |
||
114 | } elseif ( $coupon = apply_filters( 'woocommerce_get_shop_coupon_data', false, $data ) ) { |
||
115 | _doing_it_wrong( 'woocommerce_get_shop_coupon_data', 'Reading a manual coupon via woocommerce_get_shop_coupon_data has been deprecated. Please sent an instance of WC_Coupon instead.', '2.7' ); |
||
116 | $this->read_manual_coupon( $data, $coupon ); |
||
117 | } elseif ( is_numeric( $data ) && 'shop_coupon' === get_post_type( $data ) ) { |
||
118 | $this->read( $data ); |
||
119 | } elseif ( ! empty( $data ) ) { |
||
120 | $this->read( wc_get_coupon_id_by_code( $data ) ); |
||
121 | $this->set_code( $data ); |
||
122 | } |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Checks the coupon type. |
||
127 | * @param string $type Array or string of types |
||
128 | * @return bool |
||
129 | */ |
||
130 | public function is_type( $type ) { |
||
133 | |||
134 | /* |
||
135 | |-------------------------------------------------------------------------- |
||
136 | | Getters |
||
137 | |-------------------------------------------------------------------------- |
||
138 | | |
||
139 | | Methods for getting data from the coupon object. |
||
140 | | |
||
141 | */ |
||
142 | |||
143 | /** |
||
144 | * Get coupon code. |
||
145 | * @since 2.7.0 |
||
146 | * @return string |
||
147 | */ |
||
148 | public function get_code() { |
||
149 | return $this->data['code']; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Get coupon description. |
||
154 | * @since 2.7.0 |
||
155 | * @return string |
||
156 | */ |
||
157 | public function get_description() { |
||
160 | |||
161 | /** |
||
162 | * Get discount type. |
||
163 | * @since 2.7.0 |
||
164 | * @return string |
||
165 | */ |
||
166 | public function get_discount_type() { |
||
169 | |||
170 | /** |
||
171 | * Get coupon code. |
||
172 | * @since 2.7.0 |
||
173 | * @return float |
||
174 | */ |
||
175 | public function get_amount() { |
||
178 | |||
179 | /** |
||
180 | * Get coupon expiration date. |
||
181 | * @since 2.7.0 |
||
182 | * @return int |
||
183 | */ |
||
184 | public function get_date_expires() { |
||
185 | return $this->data['date_expires']; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Get date_created |
||
190 | * @since 2.7.0 |
||
191 | * @return int |
||
192 | */ |
||
193 | public function get_date_created() { |
||
194 | return $this->data['date_created']; |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Get date_modified |
||
199 | * @since 2.7.0 |
||
200 | * @return int |
||
201 | */ |
||
202 | public function get_date_modified() { |
||
203 | return $this->data['date_modified']; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Get coupon usage count. |
||
208 | * @since 2.7.0 |
||
209 | * @return integer |
||
210 | */ |
||
211 | public function get_usage_count() { |
||
212 | return $this->data['usage_count']; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Get the "indvidual use" checkbox status. |
||
217 | * @since 2.7.0 |
||
218 | * @return bool |
||
219 | */ |
||
220 | public function get_individual_use() { |
||
221 | return $this->data['individual_use']; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Get product IDs this coupon can apply to. |
||
226 | * @since 2.7.0 |
||
227 | * @return array |
||
228 | */ |
||
229 | public function get_product_ids() { |
||
232 | |||
233 | /** |
||
234 | * Get product IDs that this coupon should not apply to. |
||
235 | * @since 2.7.0 |
||
236 | * @return array |
||
237 | */ |
||
238 | public function get_excluded_product_ids() { |
||
239 | return $this->data['excluded_product_ids']; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Get coupon usage limit. |
||
244 | * @since 2.7.0 |
||
245 | * @return integer |
||
246 | */ |
||
247 | public function get_usage_limit() { |
||
248 | return $this->data['usage_limit']; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Get coupon usage limit per customer (for a single customer) |
||
253 | * @since 2.7.0 |
||
254 | * @return integer |
||
255 | */ |
||
256 | public function get_usage_limit_per_user() { |
||
257 | return $this->data['usage_limit_per_user']; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Usage limited to certain amount of items |
||
262 | * @since 2.7.0 |
||
263 | * @return integer |
||
264 | */ |
||
265 | public function get_limit_usage_to_x_items() { |
||
268 | |||
269 | /** |
||
270 | * If this coupon grants free shipping or not. |
||
271 | * @since 2.7.0 |
||
272 | * @return bool |
||
273 | */ |
||
274 | public function get_free_shipping() { |
||
275 | return $this->data['free_shipping']; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Get product categories this coupon can apply to. |
||
280 | * @since 2.7.0 |
||
281 | * @return array |
||
282 | */ |
||
283 | public function get_product_categories() { |
||
286 | |||
287 | /** |
||
288 | * Get product categories this coupon cannot not apply to. |
||
289 | * @since 2.7.0 |
||
290 | * @return array |
||
291 | */ |
||
292 | public function get_excluded_product_categories() { |
||
293 | return $this->data['excluded_product_categories']; |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * If this coupon should exclude items on sale. |
||
298 | * @since 2.7.0 |
||
299 | * @return bool |
||
300 | */ |
||
301 | public function get_exclude_sale_items() { |
||
302 | return $this->data['exclude_sale_items']; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Get minium spend amount. |
||
307 | * @since 2.7.0 |
||
308 | * @return float |
||
309 | */ |
||
310 | public function get_minimum_amount() { |
||
313 | /** |
||
314 | * Get maximum spend amount. |
||
315 | * @since 2.7.0 |
||
316 | * @return float |
||
317 | */ |
||
318 | public function get_maximum_amount() { |
||
319 | return wc_format_decimal( $this->data['maximum_amount'] ); |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * Get emails to check customer usage restrictions. |
||
324 | * @since 2.7.0 |
||
325 | * @return array |
||
326 | */ |
||
327 | public function get_email_restrictions() { |
||
328 | return $this->data['email_restrictions']; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Get records of all users who have used the current coupon. |
||
333 | * |
||
334 | * @return array |
||
335 | */ |
||
336 | public function get_used_by() { |
||
339 | |||
340 | /* |
||
341 | |-------------------------------------------------------------------------- |
||
342 | | Setters |
||
343 | |-------------------------------------------------------------------------- |
||
344 | | |
||
345 | | Functions for setting coupon data. These should not update anything in the |
||
346 | | database itself and should only change what is stored in the class |
||
347 | | object. |
||
348 | | |
||
349 | */ |
||
350 | |||
351 | /** |
||
352 | * Set coupon code. |
||
353 | * @since 2.7.0 |
||
354 | * @param string $code |
||
355 | * @throws WC_Data_Exception |
||
356 | */ |
||
357 | public function set_code( $code ) { |
||
360 | |||
361 | /** |
||
362 | * Set coupon description. |
||
363 | * @since 2.7.0 |
||
364 | * @param string $description |
||
365 | * @throws WC_Data_Exception |
||
366 | */ |
||
367 | public function set_description( $description ) { |
||
370 | |||
371 | /** |
||
372 | * Set discount type. |
||
373 | * @since 2.7.0 |
||
374 | * @param string $discount_type |
||
375 | * @throws WC_Data_Exception |
||
376 | */ |
||
377 | public function set_discount_type( $discount_type ) { |
||
383 | |||
384 | /** |
||
385 | * Set amount. |
||
386 | * @since 2.7.0 |
||
387 | * @param float $amount |
||
388 | * @throws WC_Data_Exception |
||
389 | */ |
||
390 | public function set_amount( $amount ) { |
||
393 | |||
394 | /** |
||
395 | * Set expiration date. |
||
396 | * @since 2.7.0 |
||
397 | * @param string $timestamp Timestamp |
||
398 | * @throws WC_Data_Exception |
||
399 | */ |
||
400 | public function set_date_expires( $timestamp ) { |
||
403 | |||
404 | /** |
||
405 | * Set date_created |
||
406 | * @since 2.7.0 |
||
407 | * @param string $timestamp Timestamp |
||
408 | * @throws WC_Data_Exception |
||
409 | */ |
||
410 | public function set_date_created( $timestamp ) { |
||
413 | |||
414 | /** |
||
415 | * Set date_modified |
||
416 | * @since 2.7.0 |
||
417 | * @param string $timestamp |
||
418 | * @throws WC_Data_Exception |
||
419 | */ |
||
420 | public function set_date_modified( $timestamp ) { |
||
423 | |||
424 | /** |
||
425 | * Set how many times this coupon has been used. |
||
426 | * @since 2.7.0 |
||
427 | * @param int $usage_count |
||
428 | * @throws WC_Data_Exception |
||
429 | */ |
||
430 | public function set_usage_count( $usage_count ) { |
||
433 | |||
434 | /** |
||
435 | * Set if this coupon can only be used once. |
||
436 | * @since 2.7.0 |
||
437 | * @param bool $is_individual_use |
||
438 | * @throws WC_Data_Exception |
||
439 | */ |
||
440 | public function set_individual_use( $is_individual_use ) { |
||
443 | |||
444 | /** |
||
445 | * Set the product IDs this coupon can be used with. |
||
446 | * @since 2.7.0 |
||
447 | * @param array $product_ids |
||
448 | * @throws WC_Data_Exception |
||
449 | */ |
||
450 | public function set_product_ids( $product_ids ) { |
||
453 | |||
454 | /** |
||
455 | * Set the product IDs this coupon cannot be used with. |
||
456 | * @since 2.7.0 |
||
457 | * @param array $excluded_product_ids |
||
458 | * @throws WC_Data_Exception |
||
459 | */ |
||
460 | public function set_excluded_product_ids( $excluded_product_ids ) { |
||
463 | |||
464 | /** |
||
465 | * Set the amount of times this coupon can be used. |
||
466 | * @since 2.7.0 |
||
467 | * @param int $usage_limit |
||
468 | * @throws WC_Data_Exception |
||
469 | */ |
||
470 | public function set_usage_limit( $usage_limit ) { |
||
473 | |||
474 | /** |
||
475 | * Set the amount of times this coupon can be used per user. |
||
476 | * @since 2.7.0 |
||
477 | * @param int $usage_limit |
||
478 | * @throws WC_Data_Exception |
||
479 | */ |
||
480 | public function set_usage_limit_per_user( $usage_limit ) { |
||
483 | |||
484 | /** |
||
485 | * Set usage limit to x number of items. |
||
486 | * @since 2.7.0 |
||
487 | * @param int $limit_usage_to_x_items |
||
488 | * @throws WC_Data_Exception |
||
489 | */ |
||
490 | public function set_limit_usage_to_x_items( $limit_usage_to_x_items ) { |
||
493 | |||
494 | /** |
||
495 | * Set if this coupon enables free shipping or not. |
||
496 | * @since 2.7.0 |
||
497 | * @param bool $free_shipping |
||
498 | * @throws WC_Data_Exception |
||
499 | */ |
||
500 | public function set_free_shipping( $free_shipping ) { |
||
503 | |||
504 | /** |
||
505 | * Set the product category IDs this coupon can be used with. |
||
506 | * @since 2.7.0 |
||
507 | * @param array $product_categories |
||
508 | * @throws WC_Data_Exception |
||
509 | */ |
||
510 | public function set_product_categories( $product_categories ) { |
||
513 | |||
514 | /** |
||
515 | * Set the product category IDs this coupon cannot be used with. |
||
516 | * @since 2.7.0 |
||
517 | * @param array $excluded_product_categories |
||
518 | * @throws WC_Data_Exception |
||
519 | */ |
||
520 | public function set_excluded_product_categories( $excluded_product_categories ) { |
||
523 | |||
524 | /** |
||
525 | * Set if this coupon should excluded sale items or not. |
||
526 | * @since 2.7.0 |
||
527 | * @param bool $exclude_sale_items |
||
528 | * @throws WC_Data_Exception |
||
529 | */ |
||
530 | public function set_exclude_sale_items( $exclude_sale_items ) { |
||
533 | |||
534 | /** |
||
535 | * Set the minimum spend amount. |
||
536 | * @since 2.7.0 |
||
537 | * @param float $amount |
||
538 | * @throws WC_Data_Exception |
||
539 | */ |
||
540 | public function set_minimum_amount( $amount ) { |
||
543 | |||
544 | /** |
||
545 | * Set the maximum spend amount. |
||
546 | * @since 2.7.0 |
||
547 | * @param float $amount |
||
548 | * @throws WC_Data_Exception |
||
549 | */ |
||
550 | public function set_maximum_amount( $amount ) { |
||
553 | |||
554 | /** |
||
555 | * Set email restrictions. |
||
556 | * @since 2.7.0 |
||
557 | * @param array $emails |
||
558 | * @throws WC_Data_Exception |
||
559 | */ |
||
560 | public function set_email_restrictions( $emails = array() ) { |
||
569 | |||
570 | /** |
||
571 | * Set which users have used this coupon. |
||
572 | * @since 2.7.0 |
||
573 | * @param array $used_by |
||
574 | * @throws WC_Data_Exception |
||
575 | */ |
||
576 | public function set_used_by( $used_by ) { |
||
579 | |||
580 | /* |
||
581 | |-------------------------------------------------------------------------- |
||
582 | | CRUD methods |
||
583 | |-------------------------------------------------------------------------- |
||
584 | | |
||
585 | | Methods which create, read, update and delete coupons from the database. |
||
586 | | |
||
587 | | A save method is included for convenience (chooses update or create based |
||
588 | | on if the order exists yet). |
||
589 | | |
||
590 | */ |
||
591 | |||
592 | /** |
||
593 | * Reads an coupon from the database and sets its data to the class. |
||
594 | * @since 2.7.0 |
||
595 | * @param int $coupon_id |
||
596 | */ |
||
597 | public function read( $coupon_id ) { |
||
633 | |||
634 | /** |
||
635 | * Create a new coupon. |
||
636 | * @since 2.7.0 |
||
637 | */ |
||
638 | public function create() { |
||
659 | |||
660 | /** |
||
661 | * Updates an existing coupon. |
||
662 | * @since 2.7.0 |
||
663 | */ |
||
664 | public function update() { |
||
678 | |||
679 | /** |
||
680 | * Save data (either create or update depending on if we are working on an existing coupon) |
||
681 | * @since 2.7.0 |
||
682 | */ |
||
683 | public function save() { |
||
690 | |||
691 | /** |
||
692 | * Delete coupon from the database. |
||
693 | * @since 2.7.0 |
||
694 | */ |
||
695 | public function delete() { |
||
700 | |||
701 | /** |
||
702 | * Helper method that updates all the post meta for a coupon based on it's settings in the WC_Coupon class. |
||
703 | * @since 2.7.0 |
||
704 | * @param int $coupon_id |
||
705 | */ |
||
706 | private function update_post_meta( $coupon_id ) { |
||
725 | |||
726 | /** |
||
727 | * Developers can programically return coupons. This function will read those values into our WC_Coupon class. |
||
728 | * @since 2.7.0 |
||
729 | * @param string $code Coupon code |
||
730 | * @param array $coupon Array of coupon properties |
||
731 | */ |
||
732 | public function read_manual_coupon( $code, $coupon ) { |
||
771 | |||
772 | /* |
||
773 | |-------------------------------------------------------------------------- |
||
774 | | Other Actions |
||
775 | |-------------------------------------------------------------------------- |
||
776 | */ |
||
777 | |||
778 | /** |
||
779 | * Increase usage count for current coupon. |
||
780 | * |
||
781 | * @param string $used_by Either user ID or billing email |
||
782 | */ |
||
783 | public function inc_usage_count( $used_by = '' ) { |
||
793 | |||
794 | /** |
||
795 | * Decrease usage count for current coupon. |
||
796 | * |
||
797 | * @param string $used_by Either user ID or billing email |
||
798 | */ |
||
799 | public function dcr_usage_count( $used_by = '' ) { |
||
817 | |||
818 | /* |
||
819 | |-------------------------------------------------------------------------- |
||
820 | | Validation & Error Handling |
||
821 | |-------------------------------------------------------------------------- |
||
822 | */ |
||
823 | |||
824 | /** |
||
825 | * Returns the error_message string. |
||
826 | * |
||
827 | * @access public |
||
828 | * @return string |
||
829 | */ |
||
830 | public function get_error_message() { |
||
833 | |||
834 | /** |
||
835 | * Ensure coupon exists or throw exception. |
||
836 | * |
||
837 | * @throws Exception |
||
838 | */ |
||
839 | private function validate_exists() { |
||
844 | |||
845 | /** |
||
846 | * Ensure coupon usage limit is valid or throw exception. |
||
847 | * |
||
848 | * @throws Exception |
||
849 | */ |
||
850 | private function validate_usage_limit() { |
||
855 | |||
856 | /** |
||
857 | * Ensure coupon user usage limit is valid or throw exception. |
||
858 | * |
||
859 | * Per user usage limit - check here if user is logged in (against user IDs). |
||
860 | * Checked again for emails later on in WC_Cart::check_customer_coupons(). |
||
861 | * |
||
862 | * @param int $user_id |
||
863 | * @throws Exception |
||
864 | */ |
||
865 | private function validate_user_usage_limit( $user_id = 0 ) { |
||
878 | |||
879 | /** |
||
880 | * Ensure coupon date is valid or throw exception. |
||
881 | * |
||
882 | * @throws Exception |
||
883 | */ |
||
884 | private function validate_expiry_date() { |
||
889 | |||
890 | /** |
||
891 | * Ensure coupon amount is valid or throw exception. |
||
892 | * |
||
893 | * @throws Exception |
||
894 | */ |
||
895 | private function validate_minimum_amount() { |
||
900 | |||
901 | /** |
||
902 | * Ensure coupon amount is valid or throw exception. |
||
903 | * |
||
904 | * @throws Exception |
||
905 | */ |
||
906 | private function validate_maximum_amount() { |
||
911 | |||
912 | /** |
||
913 | * Ensure coupon is valid for products in the cart is valid or throw exception. |
||
914 | * |
||
915 | * @throws Exception |
||
916 | */ |
||
917 | View Code Duplication | private function validate_product_ids() { |
|
932 | |||
933 | /** |
||
934 | * Ensure coupon is valid for product categories in the cart is valid or throw exception. |
||
935 | * |
||
936 | * @throws Exception |
||
937 | */ |
||
938 | View Code Duplication | private function validate_product_categories() { |
|
956 | |||
957 | /** |
||
958 | * Ensure coupon is valid for sale items in the cart is valid or throw exception. |
||
959 | * |
||
960 | * @throws Exception |
||
961 | */ |
||
962 | View Code Duplication | private function validate_sale_items() { |
|
983 | |||
984 | /** |
||
985 | * All exclusion rules must pass at the same time for a product coupon to be valid. |
||
986 | */ |
||
987 | private function validate_excluded_items() { |
||
1003 | |||
1004 | /** |
||
1005 | * Cart discounts cannot be added if non-eligble product is found in cart. |
||
1006 | */ |
||
1007 | private function validate_cart_excluded_items() { |
||
1014 | |||
1015 | /** |
||
1016 | * Exclude products from cart. |
||
1017 | * |
||
1018 | * @throws Exception |
||
1019 | */ |
||
1020 | View Code Duplication | private function validate_cart_excluded_product_ids() { |
|
1036 | |||
1037 | /** |
||
1038 | * Exclude categories from cart. |
||
1039 | * |
||
1040 | * @throws Exception |
||
1041 | */ |
||
1042 | View Code Duplication | private function validate_cart_excluded_product_categories() { |
|
1058 | |||
1059 | /** |
||
1060 | * Exclude sale items from cart. |
||
1061 | * |
||
1062 | * @throws Exception |
||
1063 | */ |
||
1064 | View Code Duplication | private function validate_cart_excluded_sale_items() { |
|
1084 | |||
1085 | /** |
||
1086 | * Check if a coupon is valid. |
||
1087 | * |
||
1088 | * @return boolean validity |
||
1089 | * @throws Exception |
||
1090 | */ |
||
1091 | public function is_valid() { |
||
1115 | |||
1116 | /** |
||
1117 | * Check if a coupon is valid. |
||
1118 | * |
||
1119 | * @return bool |
||
1120 | */ |
||
1121 | public function is_valid_for_cart() { |
||
1124 | |||
1125 | /** |
||
1126 | * Check if a coupon is valid for a product. |
||
1127 | * |
||
1128 | * @param WC_Product $product |
||
1129 | * @return boolean |
||
1130 | */ |
||
1131 | public function is_valid_for_product( $product, $values = array() ) { |
||
1180 | |||
1181 | /** |
||
1182 | * Converts one of the WC_Coupon message/error codes to a message string and. |
||
1183 | * displays the message/error. |
||
1184 | * |
||
1185 | * @param int $msg_code Message/error code. |
||
1186 | */ |
||
1187 | public function add_coupon_message( $msg_code ) { |
||
1200 | |||
1201 | /** |
||
1202 | * Map one of the WC_Coupon message codes to a message string. |
||
1203 | * |
||
1204 | * @param integer $msg_code |
||
1205 | * @return string| Message/error string |
||
1206 | */ |
||
1207 | View Code Duplication | public function get_coupon_message( $msg_code ) { |
|
1221 | |||
1222 | /** |
||
1223 | * Map one of the WC_Coupon error codes to a message string. |
||
1224 | * |
||
1225 | * @param int $err_code Message/error code. |
||
1226 | * @return string| Message/error string |
||
1227 | */ |
||
1228 | public function get_coupon_error( $err_code ) { |
||
1304 | |||
1305 | /** |
||
1306 | * Map one of the WC_Coupon error codes to an error string. |
||
1307 | * No coupon instance will be available where a coupon does not exist, |
||
1308 | * so this static method exists. |
||
1309 | * |
||
1310 | * @param int $err_code Error code |
||
1311 | * @return string| Error string |
||
1312 | */ |
||
1313 | View Code Duplication | public static function get_generic_coupon_error( $err_code ) { |
|
1328 | } |
||
1329 |
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.