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 | 'id' => 0, |
||
| 28 | 'code' => '', |
||
| 29 | 'description' => '', |
||
| 30 | 'discount_type' => 'fixed_cart', |
||
| 31 | 'amount' => 0, |
||
| 32 | 'expiry_date' => '', |
||
| 33 | 'usage_count' => 0, |
||
| 34 | 'used_by' => '', |
||
| 35 | 'individual_use' => false, |
||
| 36 | 'product_ids' => array(), |
||
| 37 | 'exclude_product_ids' => array(), |
||
| 38 | 'usage_limit' => '', |
||
| 39 | 'usage_limit_per_user' => '', |
||
| 40 | 'limit_usage_to_x_items' => '', |
||
| 41 | 'free_shipping' => false, |
||
| 42 | 'product_categories' => array(), |
||
| 43 | 'exclude_product_categories' => array(), |
||
| 44 | 'exclude_sale_items' => false, |
||
| 45 | 'minimum_amount' => '', |
||
| 46 | 'maximum_amount' => '', |
||
| 47 | 'customer_email' => array(), |
||
| 48 | ); |
||
| 49 | |||
| 50 | // Coupon message codes |
||
| 51 | const E_WC_COUPON_INVALID_FILTERED = 100; |
||
| 52 | const E_WC_COUPON_INVALID_REMOVED = 101; |
||
| 53 | const E_WC_COUPON_NOT_YOURS_REMOVED = 102; |
||
| 54 | const E_WC_COUPON_ALREADY_APPLIED = 103; |
||
| 55 | const E_WC_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY = 104; |
||
| 56 | const E_WC_COUPON_NOT_EXIST = 105; |
||
| 57 | const E_WC_COUPON_USAGE_LIMIT_REACHED = 106; |
||
| 58 | const E_WC_COUPON_EXPIRED = 107; |
||
| 59 | const E_WC_COUPON_MIN_SPEND_LIMIT_NOT_MET = 108; |
||
| 60 | const E_WC_COUPON_NOT_APPLICABLE = 109; |
||
| 61 | const E_WC_COUPON_NOT_VALID_SALE_ITEMS = 110; |
||
| 62 | const E_WC_COUPON_PLEASE_ENTER = 111; |
||
| 63 | const E_WC_COUPON_MAX_SPEND_LIMIT_MET = 112; |
||
| 64 | const E_WC_COUPON_EXCLUDED_PRODUCTS = 113; |
||
| 65 | const E_WC_COUPON_EXCLUDED_CATEGORIES = 114; |
||
| 66 | const WC_COUPON_SUCCESS = 200; |
||
| 67 | const WC_COUPON_REMOVED = 201; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Internal meta type used to store coupon data. |
||
| 71 | * @since 2.7.0 |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $_meta_type = 'post'; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Data stored in meta keys, but not considered "meta" for a coupon. |
||
| 78 | * @since 2.7.0 |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | protected $_internal_meta_keys = array( |
||
| 82 | 'discount_type', 'coupon_amount', 'expiry_date', 'usage_count', |
||
| 83 | 'individual_use', 'product_ids', 'exclude_product_ids', 'usage_limit', |
||
| 84 | 'usage_limit_per_user', 'limit_usage_to_x_items', 'free_shipping', |
||
| 85 | 'product_categories', 'exclude_product_categories', 'exclude_sale_items', |
||
| 86 | 'minimum_amount', 'maximum_amount', 'customer_email', '_used_by', |
||
| 87 | ); |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Coupon constructor. Loads coupon data. |
||
| 91 | * @param mixed $code code of the coupon to load |
||
| 92 | */ |
||
| 93 | public function __construct( $code = '' ) { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Checks the coupon type. |
||
| 107 | * @param string $type Array or string of types |
||
| 108 | * @return bool |
||
| 109 | */ |
||
| 110 | public function is_type( $type ) { |
||
| 113 | |||
| 114 | /* |
||
| 115 | |-------------------------------------------------------------------------- |
||
| 116 | | Getters |
||
| 117 | |-------------------------------------------------------------------------- |
||
| 118 | | |
||
| 119 | | Methods for getting data from the coupon object. |
||
| 120 | | |
||
| 121 | */ |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Get coupon ID. |
||
| 125 | * @since 2.7.0 |
||
| 126 | * @return integer |
||
| 127 | */ |
||
| 128 | public function get_id() { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get coupon code. |
||
| 134 | * @since 2.7.0 |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | public function get_code() { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get coupon description. |
||
| 143 | * @since 2.7.0 |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | public function get_description() { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Get discount type. |
||
| 152 | * @since 2.7.0 |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public function get_discount_type() { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get coupon code. |
||
| 161 | * @since 2.7.0 |
||
| 162 | * @return float |
||
| 163 | */ |
||
| 164 | public function get_amount() { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Get coupon expiration date. |
||
| 170 | * @since 2.7.0 |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | public function get_expiry_date() { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get coupon usage count. |
||
| 179 | * @since 2.7.0 |
||
| 180 | * @return integer |
||
| 181 | */ |
||
| 182 | public function get_usage_count() { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Get the "indvidual use" checkbox status. |
||
| 188 | * @since 2.7.0 |
||
| 189 | * @return bool |
||
| 190 | */ |
||
| 191 | public function get_individual_use() { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get product IDs this coupon can apply to. |
||
| 197 | * @since 2.7.0 |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | public function get_product_ids() { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get product IDs that this coupon should not apply to. |
||
| 206 | * @since 2.7.0 |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | public function get_excluded_product_ids() { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get coupon usage limit. |
||
| 215 | * @since 2.7.0 |
||
| 216 | * @return integer |
||
| 217 | */ |
||
| 218 | public function get_usage_limit() { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get coupon usage limit per customer (for a single customer) |
||
| 224 | * @since 2.7.0 |
||
| 225 | * @return integer |
||
| 226 | */ |
||
| 227 | public function get_usage_limit_per_user() { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Usage limited to certain amount of items |
||
| 233 | * @since 2.7.0 |
||
| 234 | * @return integer |
||
| 235 | */ |
||
| 236 | public function get_limit_usage_to_x_items() { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * If this coupon grants free shipping or not. |
||
| 242 | * @since 2.7.0 |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | public function get_free_shipping() { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Get product categories this coupon can apply to. |
||
| 251 | * @since 2.7.0 |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | public function get_product_categories() { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get product categories this coupon cannot not apply to. |
||
| 260 | * @since 2.7.0 |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | public function get_excluded_product_categories() { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * If this coupon should exclude items on sale. |
||
| 269 | * @since 2.7.0 |
||
| 270 | * @return bool |
||
| 271 | */ |
||
| 272 | public function get_exclude_sale_items() { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Get minium spend amount. |
||
| 278 | * @since 2.7.0 |
||
| 279 | * @return float |
||
| 280 | */ |
||
| 281 | public function get_minimum_amount() { |
||
| 284 | /** |
||
| 285 | * Get maximum spend amount. |
||
| 286 | * @since 2.7.0 |
||
| 287 | * @return float |
||
| 288 | */ |
||
| 289 | public function get_maximum_amount() { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Get emails to check customer usage restrictions. |
||
| 295 | * @since 2.7.0 |
||
| 296 | * @return array |
||
| 297 | */ |
||
| 298 | public function get_email_restrictions() { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Get records of all users who have used the current coupon. |
||
| 304 | * |
||
| 305 | * @return array |
||
| 306 | */ |
||
| 307 | public function get_used_by() { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Get a coupon ID from it's code. |
||
| 313 | * @since 2.5.0 woocommerce_coupon_code_query was removed in favour of woocommerce_get_coupon_id_from_code filter on the return. wp_cache was also implemented. |
||
| 314 | * @param string $code |
||
| 315 | * @return int |
||
| 316 | */ |
||
| 317 | private function get_coupon_id_from_code( $code ) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Get discount amount for a cart item. |
||
| 334 | * |
||
| 335 | * @param float $discounting_amount Amount the coupon is being applied to |
||
| 336 | * @param array|null $cart_item Cart item being discounted if applicable |
||
| 337 | * @param boolean $single True if discounting a single qty item, false if its the line |
||
| 338 | * @return float Amount this coupon has discounted |
||
| 339 | */ |
||
| 340 | public function get_discount_amount( $discounting_amount, $cart_item = null, $single = false ) { |
||
| 391 | |||
| 392 | /* |
||
| 393 | |-------------------------------------------------------------------------- |
||
| 394 | | Setters |
||
| 395 | |-------------------------------------------------------------------------- |
||
| 396 | | |
||
| 397 | | Functions for setting coupon data. These should not update anything in the |
||
| 398 | | database itself and should only change what is stored in the class |
||
| 399 | | object. |
||
| 400 | | |
||
| 401 | */ |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Set coupon code. |
||
| 405 | * @since 2.7.0 |
||
| 406 | * @param string $code |
||
| 407 | */ |
||
| 408 | public function set_code( $code ) { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Set coupon description. |
||
| 414 | * @since 2.7.0 |
||
| 415 | * @param string $description |
||
| 416 | */ |
||
| 417 | public function set_description( $description ) { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Set discount type. |
||
| 423 | * @since 2.7.0 |
||
| 424 | * @param string $discount_type |
||
| 425 | */ |
||
| 426 | public function set_discount_type( $discount_type ) { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Set amount. |
||
| 432 | * @since 2.7.0 |
||
| 433 | * @param float $amount |
||
| 434 | */ |
||
| 435 | public function set_amount( $amount ) { |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Set expiration date. |
||
| 441 | * @since 2.7.0 |
||
| 442 | * @param string $date |
||
| 443 | */ |
||
| 444 | public function set_expiry_date( $date ) { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Set how many times this coupon has been used. |
||
| 454 | * @since 2.7.0 |
||
| 455 | * @param int $usage_count |
||
| 456 | */ |
||
| 457 | public function set_usage_count( $usage_count ) { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Set if this coupon can only be used once. |
||
| 463 | * @since 2.7.0 |
||
| 464 | * @param bool $is_individual_use |
||
| 465 | */ |
||
| 466 | public function set_individual_use( $is_individual_use ) { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Set the product IDs this coupon can be used with. |
||
| 472 | * @since 2.7.0 |
||
| 473 | * @param array $product_ids |
||
| 474 | */ |
||
| 475 | public function set_product_ids( $product_ids ) { |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Set the product IDs this coupon cannot be used with. |
||
| 481 | * @since 2.7.0 |
||
| 482 | * @param array $excluded_product_ids |
||
| 483 | */ |
||
| 484 | public function set_excluded_product_ids( $excluded_product_ids ) { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Set the amount of times this coupon can be used. |
||
| 490 | * @since 2.7.0 |
||
| 491 | * @param int $usage_limit |
||
| 492 | */ |
||
| 493 | public function set_usage_limit( $usage_limit ) { |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Set the amount of times this coupon can be used per user. |
||
| 499 | * @since 2.7.0 |
||
| 500 | * @param int $usage_limit |
||
| 501 | */ |
||
| 502 | public function set_usage_limit_per_user( $usage_limit ) { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Set usage limit to x number of items. |
||
| 508 | * @since 2.7.0 |
||
| 509 | * @param int $limit_usage_to_x_items |
||
| 510 | */ |
||
| 511 | public function set_limit_usage_to_x_items( $limit_usage_to_x_items ) { |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Set if this coupon enables free shipping or not. |
||
| 517 | * @since 2.7.0 |
||
| 518 | * @param bool $free_shipping |
||
| 519 | */ |
||
| 520 | public function set_free_shipping( $free_shipping ) { |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Set the product category IDs this coupon can be used with. |
||
| 526 | * @since 2.7.0 |
||
| 527 | * @param array $product_categories |
||
| 528 | */ |
||
| 529 | public function set_product_categories( $product_categories ) { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Set the product category IDs this coupon cannot be used with. |
||
| 535 | * @since 2.7.0 |
||
| 536 | * @param array $excluded_product_categories |
||
| 537 | */ |
||
| 538 | public function set_excluded_product_categories( $excluded_product_categories ) { |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Set if this coupon should excluded sale items or not. |
||
| 544 | * @since 2.7.0 |
||
| 545 | * @param bool $exclude_sale_items |
||
| 546 | */ |
||
| 547 | public function set_exclude_sale_items( $exclude_sale_items ) { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Set the minimum spend amount. |
||
| 553 | * @since 2.7.0 |
||
| 554 | * @param float $amount |
||
| 555 | */ |
||
| 556 | public function set_minimum_amount( $amount ) { |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Set the maximum spend amount. |
||
| 562 | * @since 2.7.0 |
||
| 563 | * @param float $amount |
||
| 564 | */ |
||
| 565 | public function set_maximum_amount( $amount ) { |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Set email restrictions. |
||
| 571 | * @since 2.7.0 |
||
| 572 | * @param array $emails |
||
| 573 | */ |
||
| 574 | public function set_email_restrictions( $emails ) { |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Set which users have used this coupon. |
||
| 580 | * @since 2.7.0 |
||
| 581 | * @param array $used_by |
||
| 582 | */ |
||
| 583 | public function set_used_by( $used_by ) { |
||
| 586 | |||
| 587 | /* |
||
| 588 | |-------------------------------------------------------------------------- |
||
| 589 | | CRUD methods |
||
| 590 | |-------------------------------------------------------------------------- |
||
| 591 | | |
||
| 592 | | Methods which create, read, update and delete coupons from the database. |
||
| 593 | | |
||
| 594 | | A save method is included for convenience (chooses update or create based |
||
| 595 | | on if the order exists yet). |
||
| 596 | | |
||
| 597 | */ |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Reads an coupon from the database and sets its data to the class. |
||
| 601 | * @since 2.7.0 |
||
| 602 | * @param int $id |
||
| 603 | */ |
||
| 604 | public function read( $id ) { |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Create a new coupon. |
||
| 659 | * @since 2.7.0 |
||
| 660 | */ |
||
| 661 | public function create() { |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Updates an existing coupon. |
||
| 681 | * @since 2.7.0 |
||
| 682 | */ |
||
| 683 | public function update() { |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Save data (either create or update depending on if we are working on an existing coupon) |
||
| 700 | * @since 2.7.0 |
||
| 701 | */ |
||
| 702 | public function save() { |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Delete coupon from the database. |
||
| 712 | * @since 2.7.0 |
||
| 713 | */ |
||
| 714 | public function delete() { |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Helper method that updates all the post meta for a coupon based on it's settings in the WC_Coupon class. |
||
| 721 | * @since 2.7.0 |
||
| 722 | * @param int $coupon_id |
||
| 723 | */ |
||
| 724 | private function update_post_meta( $coupon_id ) { |
||
| 743 | |||
| 744 | /** |
||
| 745 | * Developers can programically return coupons. This function will read those values into our WC_Coupon class. |
||
| 746 | * @since 2.7.0 |
||
| 747 | * @param string $code Coupon code |
||
| 748 | * @param array $coupon Array of coupon properties |
||
| 749 | */ |
||
| 750 | public function read_manual_coupon( $code, $coupon ) { |
||
| 783 | |||
| 784 | /* |
||
| 785 | |-------------------------------------------------------------------------- |
||
| 786 | | Other Actions |
||
| 787 | |-------------------------------------------------------------------------- |
||
| 788 | */ |
||
| 789 | |||
| 790 | /** |
||
| 791 | * Increase usage count for current coupon. |
||
| 792 | * |
||
| 793 | * @param string $used_by Either user ID or billing email |
||
| 794 | */ |
||
| 795 | public function inc_usage_count( $used_by = '' ) { |
||
| 805 | |||
| 806 | /** |
||
| 807 | * Decrease usage count for current coupon. |
||
| 808 | * |
||
| 809 | * @param string $used_by Either user ID or billing email |
||
| 810 | */ |
||
| 811 | public function dcr_usage_count( $used_by = '' ) { |
||
| 829 | |||
| 830 | /* |
||
| 831 | |-------------------------------------------------------------------------- |
||
| 832 | | Validation & Error Handling |
||
| 833 | |-------------------------------------------------------------------------- |
||
| 834 | */ |
||
| 835 | |||
| 836 | /** |
||
| 837 | * Returns the error_message string. |
||
| 838 | * |
||
| 839 | * @access public |
||
| 840 | * @return string |
||
| 841 | */ |
||
| 842 | public function get_error_message() { |
||
| 845 | |||
| 846 | /** |
||
| 847 | * Ensure coupon exists or throw exception. |
||
| 848 | * |
||
| 849 | * @throws Exception |
||
| 850 | */ |
||
| 851 | private function validate_exists() { |
||
| 856 | |||
| 857 | /** |
||
| 858 | * Ensure coupon usage limit is valid or throw exception. |
||
| 859 | * |
||
| 860 | * @throws Exception |
||
| 861 | */ |
||
| 862 | private function validate_usage_limit() { |
||
| 867 | |||
| 868 | /** |
||
| 869 | * Ensure coupon user usage limit is valid or throw exception. |
||
| 870 | * |
||
| 871 | * Per user usage limit - check here if user is logged in (against user IDs). |
||
| 872 | * Checked again for emails later on in WC_Cart::check_customer_coupons(). |
||
| 873 | * |
||
| 874 | * @param int $user_id |
||
| 875 | * @throws Exception |
||
| 876 | */ |
||
| 877 | private function validate_user_usage_limit( $user_id = 0 ) { |
||
| 890 | |||
| 891 | /** |
||
| 892 | * Ensure coupon date is valid or throw exception. |
||
| 893 | * |
||
| 894 | * @throws Exception |
||
| 895 | */ |
||
| 896 | private function validate_expiry_date() { |
||
| 901 | |||
| 902 | /** |
||
| 903 | * Ensure coupon amount is valid or throw exception. |
||
| 904 | * |
||
| 905 | * @throws Exception |
||
| 906 | */ |
||
| 907 | private function validate_minimum_amount() { |
||
| 912 | |||
| 913 | /** |
||
| 914 | * Ensure coupon amount is valid or throw exception. |
||
| 915 | * |
||
| 916 | * @throws Exception |
||
| 917 | */ |
||
| 918 | private function validate_maximum_amount() { |
||
| 923 | |||
| 924 | /** |
||
| 925 | * Ensure coupon is valid for products in the cart is valid or throw exception. |
||
| 926 | * |
||
| 927 | * @throws Exception |
||
| 928 | */ |
||
| 929 | View Code Duplication | private function validate_product_ids() { |
|
| 944 | |||
| 945 | /** |
||
| 946 | * Ensure coupon is valid for product categories in the cart is valid or throw exception. |
||
| 947 | * |
||
| 948 | * @throws Exception |
||
| 949 | */ |
||
| 950 | View Code Duplication | private function validate_product_categories() { |
|
| 968 | |||
| 969 | /** |
||
| 970 | * Ensure coupon is valid for sale items in the cart is valid or throw exception. |
||
| 971 | * |
||
| 972 | * @throws Exception |
||
| 973 | */ |
||
| 974 | View Code Duplication | private function validate_sale_items() { |
|
| 995 | |||
| 996 | /** |
||
| 997 | * All exclusion rules must pass at the same time for a product coupon to be valid. |
||
| 998 | */ |
||
| 999 | private function validate_excluded_items() { |
||
| 1015 | |||
| 1016 | /** |
||
| 1017 | * Cart discounts cannot be added if non-eligble product is found in cart. |
||
| 1018 | */ |
||
| 1019 | private function validate_cart_excluded_items() { |
||
| 1026 | |||
| 1027 | /** |
||
| 1028 | * Exclude products from cart. |
||
| 1029 | * |
||
| 1030 | * @throws Exception |
||
| 1031 | */ |
||
| 1032 | View Code Duplication | private function validate_cart_excluded_product_ids() { |
|
| 1048 | |||
| 1049 | /** |
||
| 1050 | * Exclude categories from cart. |
||
| 1051 | * |
||
| 1052 | * @throws Exception |
||
| 1053 | */ |
||
| 1054 | View Code Duplication | private function validate_cart_excluded_product_categories() { |
|
| 1070 | |||
| 1071 | /** |
||
| 1072 | * Exclude sale items from cart. |
||
| 1073 | * |
||
| 1074 | * @throws Exception |
||
| 1075 | */ |
||
| 1076 | View Code Duplication | private function validate_cart_excluded_sale_items() { |
|
| 1096 | |||
| 1097 | /** |
||
| 1098 | * Check if a coupon is valid. |
||
| 1099 | * |
||
| 1100 | * @return boolean validity |
||
| 1101 | * @throws Exception |
||
| 1102 | */ |
||
| 1103 | public function is_valid() { |
||
| 1127 | |||
| 1128 | /** |
||
| 1129 | * Check if a coupon is valid. |
||
| 1130 | * |
||
| 1131 | * @return bool |
||
| 1132 | */ |
||
| 1133 | public function is_valid_for_cart() { |
||
| 1136 | |||
| 1137 | /** |
||
| 1138 | * Check if a coupon is valid for a product. |
||
| 1139 | * |
||
| 1140 | * @param WC_Product $product |
||
| 1141 | * @return boolean |
||
| 1142 | */ |
||
| 1143 | public function is_valid_for_product( $product, $values = array() ) { |
||
| 1192 | |||
| 1193 | /** |
||
| 1194 | * Converts one of the WC_Coupon message/error codes to a message string and. |
||
| 1195 | * displays the message/error. |
||
| 1196 | * |
||
| 1197 | * @param int $msg_code Message/error code. |
||
| 1198 | */ |
||
| 1199 | public function add_coupon_message( $msg_code ) { |
||
| 1212 | |||
| 1213 | /** |
||
| 1214 | * Map one of the WC_Coupon message codes to a message string. |
||
| 1215 | * |
||
| 1216 | * @param integer $msg_code |
||
| 1217 | * @return string| Message/error string |
||
| 1218 | */ |
||
| 1219 | View Code Duplication | public function get_coupon_message( $msg_code ) { |
|
| 1233 | |||
| 1234 | /** |
||
| 1235 | * Map one of the WC_Coupon error codes to a message string. |
||
| 1236 | * |
||
| 1237 | * @param int $err_code Message/error code. |
||
| 1238 | * @return string| Message/error string |
||
| 1239 | */ |
||
| 1240 | public function get_coupon_error( $err_code ) { |
||
| 1316 | |||
| 1317 | /** |
||
| 1318 | * Map one of the WC_Coupon error codes to an error string. |
||
| 1319 | * No coupon instance will be available where a coupon does not exist, |
||
| 1320 | * so this static method exists. |
||
| 1321 | * |
||
| 1322 | * @param int $err_code Error code |
||
| 1323 | * @return string| Error string |
||
| 1324 | */ |
||
| 1325 | View Code Duplication | public static function get_generic_coupon_error( $err_code ) { |
|
| 1340 | } |
||
| 1341 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: