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 ) { |
||
| 131 | return ( $this->get_discount_type() == $type || ( is_array( $type ) && in_array( $this->get_discount_type(), $type ) ) ); |
||
| 132 | } |
||
| 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() { |
||
| 158 | return $this->data['description']; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get discount type. |
||
| 163 | * @since 2.7.0 |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public function get_discount_type() { |
||
| 167 | return $this->data['discount_type']; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Get coupon code. |
||
| 172 | * @since 2.7.0 |
||
| 173 | * @return float |
||
| 174 | */ |
||
| 175 | public function get_amount() { |
||
| 176 | return wc_format_decimal( $this->data['amount'] ); |
||
| 177 | } |
||
| 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() { |
||
| 230 | return $this->data['product_ids']; |
||
| 231 | } |
||
| 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() { |
||
| 266 | return $this->data['limit_usage_to_x_items']; |
||
| 267 | } |
||
| 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() { |
||
| 284 | return $this->data['product_categories']; |
||
| 285 | } |
||
| 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() { |
||
| 311 | return wc_format_decimal( $this->data['minimum_amount'] ); |
||
| 312 | } |
||
| 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() { |
||
| 337 | return $this->data['used_by']; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Get discount amount for a cart item. |
||
| 342 | * |
||
| 343 | * @param float $discounting_amount Amount the coupon is being applied to |
||
| 344 | * @param array|null $cart_item Cart item being discounted if applicable |
||
| 345 | * @param boolean $single True if discounting a single qty item, false if its the line |
||
| 346 | * @return float Amount this coupon has discounted |
||
| 347 | */ |
||
| 348 | public function get_discount_amount( $discounting_amount, $cart_item = null, $single = false ) { |
||
| 349 | $discount = 0; |
||
| 350 | $cart_item_qty = is_null( $cart_item ) ? 1 : $cart_item['quantity']; |
||
| 351 | |||
| 352 | if ( $this->is_type( array( 'percent_product', 'percent' ) ) ) { |
||
|
|
|||
| 353 | $discount = $this->get_amount() * ( $discounting_amount / 100 ); |
||
| 354 | } elseif ( $this->is_type( 'fixed_cart' ) && ! is_null( $cart_item ) && WC()->cart->subtotal_ex_tax ) { |
||
| 355 | /** |
||
| 356 | * This is the most complex discount - we need to divide the discount between rows based on their price in. |
||
| 357 | * proportion to the subtotal. This is so rows with different tax rates get a fair discount, and so rows. |
||
| 358 | * with no price (free) don't get discounted. |
||
| 359 | * |
||
| 360 | * Get item discount by dividing item cost by subtotal to get a %. |
||
| 361 | * |
||
| 362 | * Uses price inc tax if prices include tax to work around https://github.com/woothemes/woocommerce/issues/7669 and https://github.com/woothemes/woocommerce/issues/8074. |
||
| 363 | */ |
||
| 364 | if ( wc_prices_include_tax() ) { |
||
| 365 | $discount_percent = ( $cart_item['data']->get_price_including_tax() * $cart_item_qty ) / WC()->cart->subtotal; |
||
| 366 | } else { |
||
| 367 | $discount_percent = ( $cart_item['data']->get_price_excluding_tax() * $cart_item_qty ) / WC()->cart->subtotal_ex_tax; |
||
| 368 | } |
||
| 369 | $discount = ( $this->get_amount() * $discount_percent ) / $cart_item_qty; |
||
| 370 | |||
| 371 | } elseif ( $this->is_type( 'fixed_product' ) ) { |
||
| 372 | $discount = min( $this->get_amount(), $discounting_amount ); |
||
| 373 | $discount = $single ? $discount : $discount * $cart_item_qty; |
||
| 374 | } |
||
| 375 | |||
| 376 | $discount = min( $discount, $discounting_amount ); |
||
| 377 | |||
| 378 | // Handle the limit_usage_to_x_items option |
||
| 379 | if ( $this->is_type( array( 'percent_product', 'fixed_product' ) ) ) { |
||
| 380 | if ( $discounting_amount ) { |
||
| 381 | if ( '' === $this->get_limit_usage_to_x_items() ) { |
||
| 382 | $limit_usage_qty = $cart_item_qty; |
||
| 383 | } else { |
||
| 384 | $limit_usage_qty = min( $this->get_limit_usage_to_x_items(), $cart_item_qty ); |
||
| 385 | $this->set_limit_usage_to_x_items( max( 0, $this->get_limit_usage_to_x_items() - $limit_usage_qty ) ); |
||
| 386 | } |
||
| 387 | if ( $single ) { |
||
| 388 | $discount = ( $discount * $limit_usage_qty ) / $cart_item_qty; |
||
| 389 | } else { |
||
| 390 | $discount = ( $discount / $cart_item_qty ) * $limit_usage_qty; |
||
| 391 | } |
||
| 392 | } |
||
| 393 | } |
||
| 394 | |||
| 395 | $discount = round( $discount, wc_get_rounding_precision() ); |
||
| 396 | |||
| 397 | return apply_filters( 'woocommerce_coupon_get_discount_amount', $discount, $discounting_amount, $cart_item, $single, $this ); |
||
| 398 | } |
||
| 399 | |||
| 400 | /* |
||
| 401 | |-------------------------------------------------------------------------- |
||
| 402 | | Setters |
||
| 403 | |-------------------------------------------------------------------------- |
||
| 404 | | |
||
| 405 | | Functions for setting coupon data. These should not update anything in the |
||
| 406 | | database itself and should only change what is stored in the class |
||
| 407 | | object. |
||
| 408 | | |
||
| 409 | */ |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Set coupon code. |
||
| 413 | * @since 2.7.0 |
||
| 414 | * @param string $code |
||
| 415 | * @throws WC_Data_Exception |
||
| 416 | */ |
||
| 417 | public function set_code( $code ) { |
||
| 418 | $this->data['code'] = apply_filters( 'woocommerce_coupon_code', $code ); |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Set coupon description. |
||
| 423 | * @since 2.7.0 |
||
| 424 | * @param string $description |
||
| 425 | * @throws WC_Data_Exception |
||
| 426 | */ |
||
| 427 | public function set_description( $description ) { |
||
| 428 | $this->data['description'] = $description; |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Set discount type. |
||
| 433 | * @since 2.7.0 |
||
| 434 | * @param string $discount_type |
||
| 435 | * @throws WC_Data_Exception |
||
| 436 | */ |
||
| 437 | public function set_discount_type( $discount_type ) { |
||
| 438 | if ( ! in_array( $discount_type, array_keys( wc_get_coupon_types() ) ) ) { |
||
| 439 | $this->error( 'coupon_invalid_discount_type', __( 'Invalid discount type', 'woocommerce' ) ); |
||
| 440 | } |
||
| 441 | $this->data['discount_type'] = $discount_type; |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Set amount. |
||
| 446 | * @since 2.7.0 |
||
| 447 | * @param float $amount |
||
| 448 | * @throws WC_Data_Exception |
||
| 449 | */ |
||
| 450 | public function set_amount( $amount ) { |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Set expiration date. |
||
| 456 | * @since 2.7.0 |
||
| 457 | * @param string $timestamp Timestamp |
||
| 458 | * @throws WC_Data_Exception |
||
| 459 | */ |
||
| 460 | public function set_date_expires( $timestamp ) { |
||
| 461 | $this->data['date_expires'] = is_numeric( $timestamp ) ? $timestamp : strtotime( $timestamp ); |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Set date_created |
||
| 466 | * @since 2.7.0 |
||
| 467 | * @param string $timestamp Timestamp |
||
| 468 | * @throws WC_Data_Exception |
||
| 469 | */ |
||
| 470 | public function set_date_created( $timestamp ) { |
||
| 471 | $this->data['date_created'] = is_numeric( $timestamp ) ? $timestamp : strtotime( $timestamp ); |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Set date_modified |
||
| 476 | * @since 2.7.0 |
||
| 477 | * @param string $timestamp |
||
| 478 | * @throws WC_Data_Exception |
||
| 479 | */ |
||
| 480 | public function set_date_modified( $timestamp ) { |
||
| 481 | $this->data['date_modified'] = is_numeric( $timestamp ) ? $timestamp : strtotime( $timestamp ); |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Set how many times this coupon has been used. |
||
| 486 | * @since 2.7.0 |
||
| 487 | * @param int $usage_count |
||
| 488 | * @throws WC_Data_Exception |
||
| 489 | */ |
||
| 490 | public function set_usage_count( $usage_count ) { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Set if this coupon can only be used once. |
||
| 496 | * @since 2.7.0 |
||
| 497 | * @param bool $is_individual_use |
||
| 498 | * @throws WC_Data_Exception |
||
| 499 | */ |
||
| 500 | public function set_individual_use( $is_individual_use ) { |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Set the product IDs this coupon can be used with. |
||
| 506 | * @since 2.7.0 |
||
| 507 | * @param array $product_ids |
||
| 508 | * @throws WC_Data_Exception |
||
| 509 | */ |
||
| 510 | public function set_product_ids( $product_ids ) { |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Set the product IDs this coupon cannot be used with. |
||
| 516 | * @since 2.7.0 |
||
| 517 | * @param array $excluded_product_ids |
||
| 518 | * @throws WC_Data_Exception |
||
| 519 | */ |
||
| 520 | public function set_excluded_product_ids( $excluded_product_ids ) { |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Set the amount of times this coupon can be used. |
||
| 526 | * @since 2.7.0 |
||
| 527 | * @param int $usage_limit |
||
| 528 | * @throws WC_Data_Exception |
||
| 529 | */ |
||
| 530 | public function set_usage_limit( $usage_limit ) { |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Set the amount of times this coupon can be used per user. |
||
| 536 | * @since 2.7.0 |
||
| 537 | * @param int $usage_limit |
||
| 538 | * @throws WC_Data_Exception |
||
| 539 | */ |
||
| 540 | public function set_usage_limit_per_user( $usage_limit ) { |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Set usage limit to x number of items. |
||
| 546 | * @since 2.7.0 |
||
| 547 | * @param int $limit_usage_to_x_items |
||
| 548 | * @throws WC_Data_Exception |
||
| 549 | */ |
||
| 550 | public function set_limit_usage_to_x_items( $limit_usage_to_x_items ) { |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Set if this coupon enables free shipping or not. |
||
| 556 | * @since 2.7.0 |
||
| 557 | * @param bool $free_shipping |
||
| 558 | * @throws WC_Data_Exception |
||
| 559 | */ |
||
| 560 | public function set_free_shipping( $free_shipping ) { |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Set the product category IDs this coupon can be used with. |
||
| 566 | * @since 2.7.0 |
||
| 567 | * @param array $product_categories |
||
| 568 | * @throws WC_Data_Exception |
||
| 569 | */ |
||
| 570 | public function set_product_categories( $product_categories ) { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Set the product category IDs this coupon cannot be used with. |
||
| 576 | * @since 2.7.0 |
||
| 577 | * @param array $excluded_product_categories |
||
| 578 | * @throws WC_Data_Exception |
||
| 579 | */ |
||
| 580 | public function set_excluded_product_categories( $excluded_product_categories ) { |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Set if this coupon should excluded sale items or not. |
||
| 586 | * @since 2.7.0 |
||
| 587 | * @param bool $exclude_sale_items |
||
| 588 | * @throws WC_Data_Exception |
||
| 589 | */ |
||
| 590 | public function set_exclude_sale_items( $exclude_sale_items ) { |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Set the minimum spend amount. |
||
| 596 | * @since 2.7.0 |
||
| 597 | * @param float $amount |
||
| 598 | * @throws WC_Data_Exception |
||
| 599 | */ |
||
| 600 | public function set_minimum_amount( $amount ) { |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Set the maximum spend amount. |
||
| 606 | * @since 2.7.0 |
||
| 607 | * @param float $amount |
||
| 608 | * @throws WC_Data_Exception |
||
| 609 | */ |
||
| 610 | public function set_maximum_amount( $amount ) { |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Set email restrictions. |
||
| 616 | * @since 2.7.0 |
||
| 617 | * @param array $emails |
||
| 618 | * @throws WC_Data_Exception |
||
| 619 | */ |
||
| 620 | public function set_email_restrictions( $emails = array() ) { |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Set which users have used this coupon. |
||
| 632 | * @since 2.7.0 |
||
| 633 | * @param array $used_by |
||
| 634 | * @throws WC_Data_Exception |
||
| 635 | */ |
||
| 636 | public function set_used_by( $used_by ) { |
||
| 639 | |||
| 640 | /* |
||
| 641 | |-------------------------------------------------------------------------- |
||
| 642 | | CRUD methods |
||
| 643 | |-------------------------------------------------------------------------- |
||
| 644 | | |
||
| 645 | | Methods which create, read, update and delete coupons from the database. |
||
| 646 | | |
||
| 647 | | A save method is included for convenience (chooses update or create based |
||
| 648 | | on if the order exists yet). |
||
| 649 | | |
||
| 650 | */ |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Reads an coupon from the database and sets its data to the class. |
||
| 654 | * @since 2.7.0 |
||
| 655 | * @param int $coupon_id |
||
| 656 | */ |
||
| 657 | public function read( $coupon_id ) { |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Create a new coupon. |
||
| 696 | * @since 2.7.0 |
||
| 697 | */ |
||
| 698 | public function create() { |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Updates an existing coupon. |
||
| 722 | * @since 2.7.0 |
||
| 723 | */ |
||
| 724 | public function update() { |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Save data (either create or update depending on if we are working on an existing coupon) |
||
| 741 | * @since 2.7.0 |
||
| 742 | */ |
||
| 743 | public function save() { |
||
| 750 | |||
| 751 | /** |
||
| 752 | * Delete coupon from the database. |
||
| 753 | * @since 2.7.0 |
||
| 754 | */ |
||
| 755 | public function delete() { |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Helper method that updates all the post meta for a coupon based on it's settings in the WC_Coupon class. |
||
| 763 | * @since 2.7.0 |
||
| 764 | * @param int $coupon_id |
||
| 765 | */ |
||
| 766 | private function update_post_meta( $coupon_id ) { |
||
| 785 | |||
| 786 | /** |
||
| 787 | * Developers can programically return coupons. This function will read those values into our WC_Coupon class. |
||
| 788 | * @since 2.7.0 |
||
| 789 | * @param string $code Coupon code |
||
| 790 | * @param array $coupon Array of coupon properties |
||
| 791 | */ |
||
| 792 | public function read_manual_coupon( $code, $coupon ) { |
||
| 831 | |||
| 832 | /* |
||
| 833 | |-------------------------------------------------------------------------- |
||
| 834 | | Other Actions |
||
| 835 | |-------------------------------------------------------------------------- |
||
| 836 | */ |
||
| 837 | |||
| 838 | /** |
||
| 839 | * Increase usage count for current coupon. |
||
| 840 | * |
||
| 841 | * @param string $used_by Either user ID or billing email |
||
| 842 | */ |
||
| 843 | public function inc_usage_count( $used_by = '' ) { |
||
| 853 | |||
| 854 | /** |
||
| 855 | * Decrease usage count for current coupon. |
||
| 856 | * |
||
| 857 | * @param string $used_by Either user ID or billing email |
||
| 858 | */ |
||
| 859 | public function dcr_usage_count( $used_by = '' ) { |
||
| 877 | |||
| 878 | /* |
||
| 879 | |-------------------------------------------------------------------------- |
||
| 880 | | Validation & Error Handling |
||
| 881 | |-------------------------------------------------------------------------- |
||
| 882 | */ |
||
| 883 | |||
| 884 | /** |
||
| 885 | * Returns the error_message string. |
||
| 886 | * |
||
| 887 | * @access public |
||
| 888 | * @return string |
||
| 889 | */ |
||
| 890 | public function get_error_message() { |
||
| 893 | |||
| 894 | /** |
||
| 895 | * Ensure coupon exists or throw exception. |
||
| 896 | * |
||
| 897 | * @throws Exception |
||
| 898 | */ |
||
| 899 | private function validate_exists() { |
||
| 904 | |||
| 905 | /** |
||
| 906 | * Ensure coupon usage limit is valid or throw exception. |
||
| 907 | * |
||
| 908 | * @throws Exception |
||
| 909 | */ |
||
| 910 | private function validate_usage_limit() { |
||
| 915 | |||
| 916 | /** |
||
| 917 | * Ensure coupon user usage limit is valid or throw exception. |
||
| 918 | * |
||
| 919 | * Per user usage limit - check here if user is logged in (against user IDs). |
||
| 920 | * Checked again for emails later on in WC_Cart::check_customer_coupons(). |
||
| 921 | * |
||
| 922 | * @param int $user_id |
||
| 923 | * @throws Exception |
||
| 924 | */ |
||
| 925 | private function validate_user_usage_limit( $user_id = 0 ) { |
||
| 938 | |||
| 939 | /** |
||
| 940 | * Ensure coupon date is valid or throw exception. |
||
| 941 | * |
||
| 942 | * @throws Exception |
||
| 943 | */ |
||
| 944 | private function validate_expiry_date() { |
||
| 949 | |||
| 950 | /** |
||
| 951 | * Ensure coupon amount is valid or throw exception. |
||
| 952 | * |
||
| 953 | * @throws Exception |
||
| 954 | */ |
||
| 955 | private function validate_minimum_amount() { |
||
| 960 | |||
| 961 | /** |
||
| 962 | * Ensure coupon amount is valid or throw exception. |
||
| 963 | * |
||
| 964 | * @throws Exception |
||
| 965 | */ |
||
| 966 | private function validate_maximum_amount() { |
||
| 971 | |||
| 972 | /** |
||
| 973 | * Ensure coupon is valid for products in the cart is valid or throw exception. |
||
| 974 | * |
||
| 975 | * @throws Exception |
||
| 976 | */ |
||
| 977 | View Code Duplication | private function validate_product_ids() { |
|
| 992 | |||
| 993 | /** |
||
| 994 | * Ensure coupon is valid for product categories in the cart is valid or throw exception. |
||
| 995 | * |
||
| 996 | * @throws Exception |
||
| 997 | */ |
||
| 998 | View Code Duplication | private function validate_product_categories() { |
|
| 1016 | |||
| 1017 | /** |
||
| 1018 | * Ensure coupon is valid for sale items in the cart is valid or throw exception. |
||
| 1019 | * |
||
| 1020 | * @throws Exception |
||
| 1021 | */ |
||
| 1022 | View Code Duplication | private function validate_sale_items() { |
|
| 1043 | |||
| 1044 | /** |
||
| 1045 | * All exclusion rules must pass at the same time for a product coupon to be valid. |
||
| 1046 | */ |
||
| 1047 | private function validate_excluded_items() { |
||
| 1063 | |||
| 1064 | /** |
||
| 1065 | * Cart discounts cannot be added if non-eligble product is found in cart. |
||
| 1066 | */ |
||
| 1067 | private function validate_cart_excluded_items() { |
||
| 1074 | |||
| 1075 | /** |
||
| 1076 | * Exclude products from cart. |
||
| 1077 | * |
||
| 1078 | * @throws Exception |
||
| 1079 | */ |
||
| 1080 | View Code Duplication | private function validate_cart_excluded_product_ids() { |
|
| 1096 | |||
| 1097 | /** |
||
| 1098 | * Exclude categories from cart. |
||
| 1099 | * |
||
| 1100 | * @throws Exception |
||
| 1101 | */ |
||
| 1102 | View Code Duplication | private function validate_cart_excluded_product_categories() { |
|
| 1118 | |||
| 1119 | /** |
||
| 1120 | * Exclude sale items from cart. |
||
| 1121 | * |
||
| 1122 | * @throws Exception |
||
| 1123 | */ |
||
| 1124 | View Code Duplication | private function validate_cart_excluded_sale_items() { |
|
| 1144 | |||
| 1145 | /** |
||
| 1146 | * Check if a coupon is valid. |
||
| 1147 | * |
||
| 1148 | * @return boolean validity |
||
| 1149 | * @throws Exception |
||
| 1150 | */ |
||
| 1151 | public function is_valid() { |
||
| 1175 | |||
| 1176 | /** |
||
| 1177 | * Check if a coupon is valid. |
||
| 1178 | * |
||
| 1179 | * @return bool |
||
| 1180 | */ |
||
| 1181 | public function is_valid_for_cart() { |
||
| 1184 | |||
| 1185 | /** |
||
| 1186 | * Check if a coupon is valid for a product. |
||
| 1187 | * |
||
| 1188 | * @param WC_Product $product |
||
| 1189 | * @return boolean |
||
| 1190 | */ |
||
| 1191 | public function is_valid_for_product( $product, $values = array() ) { |
||
| 1240 | |||
| 1241 | /** |
||
| 1242 | * Converts one of the WC_Coupon message/error codes to a message string and. |
||
| 1243 | * displays the message/error. |
||
| 1244 | * |
||
| 1245 | * @param int $msg_code Message/error code. |
||
| 1246 | */ |
||
| 1247 | public function add_coupon_message( $msg_code ) { |
||
| 1260 | |||
| 1261 | /** |
||
| 1262 | * Map one of the WC_Coupon message codes to a message string. |
||
| 1263 | * |
||
| 1264 | * @param integer $msg_code |
||
| 1265 | * @return string| Message/error string |
||
| 1266 | */ |
||
| 1267 | View Code Duplication | public function get_coupon_message( $msg_code ) { |
|
| 1281 | |||
| 1282 | /** |
||
| 1283 | * Map one of the WC_Coupon error codes to a message string. |
||
| 1284 | * |
||
| 1285 | * @param int $err_code Message/error code. |
||
| 1286 | * @return string| Message/error string |
||
| 1287 | */ |
||
| 1288 | public function get_coupon_error( $err_code ) { |
||
| 1364 | |||
| 1365 | /** |
||
| 1366 | * Map one of the WC_Coupon error codes to an error string. |
||
| 1367 | * No coupon instance will be available where a coupon does not exist, |
||
| 1368 | * so this static method exists. |
||
| 1369 | * |
||
| 1370 | * @param int $err_code Error code |
||
| 1371 | * @return string| Error string |
||
| 1372 | */ |
||
| 1373 | View Code Duplication | public static function get_generic_coupon_error( $err_code ) { |
|
| 1388 | } |
||
| 1389 |
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: