| @@ -10,8 +10,9 @@ | ||
| 10 | 10 | namespace Disco\App; |
| 11 | 11 | |
| 12 | 12 | use Disco\App\Calc\CalcFactory; |
| 13 | 13 | use Disco\App\Features\UserLimit; |
| 14 | +use Disco\App\Utility\Config; | |
| 14 | 15 | use Disco\App\Utility\Settings; |
| 15 | 16 | |
| 16 | 17 | /** |
| 17 | 18 | * Class Disco |
| @@ -71,18 +72,9 @@ | ||
| 71 | 72 | * Get a discount limit form campaign. |
| 72 | 73 | * |
| 73 | 74 | * Compare with total product meta and apply discount |
| 74 | 75 | */ |
| 75 | - $discount_limit = $intent->campaign->discount_max_user; | |
| 76 | - $total_applied_campaign = ( new UserLimit )->disco_get_total_applied_campaign( $intent->campaign->id ); | |
| 77 | - | |
| 78 | - if ( | |
| 79 | - ! empty( $discount_limit ) | |
| 80 | - && ( | |
| 81 | - $discount_limit >= 0 | |
| 82 | - && $total_applied_campaign >= $discount_limit | |
| 83 | - ) | |
| 84 | - ) { | |
| 76 | + if ( ( new UserLimit )->disco_is_limit_reached( $intent->campaign ) ) { | |
| 85 | 77 | continue; |
| 86 | 78 | } |
| 87 | 79 | |
| 88 | 80 | $discount = $intent->get_discounts( (float) $price, $product ); |
| @@ -90,10 +82,30 @@ | ||
| 90 | 82 | if ( empty( $discount ) ) { |
| 91 | 83 | continue; |
| 92 | 84 | } |
| 93 | 85 | |
| 94 | - // Store discount keyed by campaign id so the winning amount maps back to the right campaign. | |
| 95 | - $discounts[ $intent->campaign->id ] = $discount; | |
| 86 | + /** | |
| 87 | + * Normalize each campaign's amount before the amounts compete. | |
| 88 | + * | |
| 89 | + * A percent amount is derived from the product price, which a | |
| 90 | + * currency switcher has already converted, so it arrives in the | |
| 91 | + * active currency. A fixed amount is the rule value as typed, so | |
| 92 | + * it arrives in the store's base currency. Comparing the two | |
| 93 | + * directly picks a winner by exchange rate rather than by size: | |
| 94 | + * percent scales with the rate and fixed does not, so `max` would | |
| 95 | + * select percent on almost every product and `min` would select | |
| 96 | + * fixed. Converting here, with each campaign's own discount type, | |
| 97 | + * puts every amount in the same currency before min/max runs. | |
| 98 | + * | |
| 99 | + * Filtering per campaign also matches what CartIntent and the | |
| 100 | + * Calc classes already do, so every path now compares like for | |
| 101 | + * like. | |
| 102 | + */ | |
| 103 | + $discounts[ $intent->campaign->id ] = (float) apply_filters( | |
| 104 | + 'disco_final_discounted_amount', | |
| 105 | + (float) $discount, | |
| 106 | + $this->campaign_discount_type( $intent->campaign ) | |
| 107 | + ); | |
| 96 | 108 | } |
| 97 | 109 | |
| 98 | 110 | // If no discount is applied, return the original price. |
| 99 | 111 | if ( empty( $discounts ) ) { |
| @@ -99,20 +111,37 @@ | ||
| 99 | 111 | if ( empty( $discounts ) ) { |
| 100 | 112 | return $price; |
| 101 | 113 | } |
| 102 | 114 | |
| 103 | - $discount_type = $intent->campaign->discount_rules[0]['discount_type']; | |
| 115 | + /** | |
| 116 | + * Only amounts the product can actually carry take part in the choice. | |
| 117 | + * | |
| 118 | + * A flat amount larger than the price cannot be applied, and it used | |
| 119 | + * to win the min/max selection anyway and then be rejected by the | |
| 120 | + * guard below, leaving the product at full price even though another | |
| 121 | + * campaign offered a discount that would have fitted. With a 70% | |
| 122 | + * campaign and a 50.00 campaign, everything priced under 50.00 was | |
| 123 | + * discounted by nothing at all while a 50.00 product went free. | |
| 124 | + * | |
| 125 | + * Dropping the ones that do not fit first means `max` picks the | |
| 126 | + * largest discount that can be given rather than the largest that was | |
| 127 | + * offered, which is what asking for the maximum discount means. | |
| 128 | + */ | |
| 129 | + $applicable = array_filter( | |
| 130 | + $discounts, | |
| 131 | + static function ( $amount ) use ( $price ) { | |
| 132 | + return (float) $amount <= (float) $price; | |
| 133 | + } | |
| 134 | + ); | |
| 104 | 135 | |
| 136 | + if ( empty( $applicable ) ) { | |
| 137 | + return $price; | |
| 138 | + } | |
| 139 | + | |
| 105 | 140 | // Pick the winning discount amount, then map it back to the campaign that produced it. |
| 106 | - $base_discount = $this->min_max_average( array_values( $discounts ) ); | |
| 107 | - $this->applied_campaign_id = (int) array_search( $base_discount, $discounts, true ); | |
| 141 | + $discounted_amount = $this->min_max_average( array_values( $applicable ) ); | |
| 142 | + $this->applied_campaign_id = (int) array_search( $discounted_amount, $applicable, true ); | |
| 108 | 143 | |
| 109 | - /** | |
| 110 | - * Get the min or max discount amount according to plugin settings. | |
| 111 | - * Apply the filter to modify final discounted amount based on discount types. | |
| 112 | - */ | |
| 113 | - $discounted_amount = apply_filters( 'disco_final_discounted_amount', $base_discount, $discount_type ); | |
| 114 | - | |
| 115 | 144 | if ( $discounted_amount <= $price ) { |
| 116 | 145 | // Get an applied campaign from DiscountLimit class. |
| 117 | 146 | ( new UserLimit )->disco_start_session_on_checkout( $this->applied_campaign_id ); |
| 118 | 147 | |
| @@ -149,18 +178,9 @@ | ||
| 149 | 178 | * Get a discount limit form campaign. |
| 150 | 179 | * |
| 151 | 180 | * Compare with total product meta and apply discount |
| 152 | 181 | */ |
| 153 | - $discount_limit = $intent->campaign->discount_max_user; | |
| 154 | - $total_applied_campaign = ( new UserLimit )->disco_get_total_applied_campaign( $intent->campaign->id ); | |
| 155 | - | |
| 156 | - if ( | |
| 157 | - ! empty( $discount_limit ) | |
| 158 | - && ( | |
| 159 | - $discount_limit >= 0 | |
| 160 | - && $total_applied_campaign >= $discount_limit | |
| 161 | - ) | |
| 162 | - ) { | |
| 182 | + if ( ( new UserLimit )->disco_is_limit_reached( $intent->campaign ) ) { | |
| 163 | 183 | continue; |
| 164 | 184 | } |
| 165 | 185 | |
| 166 | 186 | $items = $this->get_items_for_discount( $cart, $intent->campaign ); |
| @@ -275,21 +295,21 @@ | ||
| 275 | 295 | * @param \WC_Cart $cart WooCommerce cart object to calculate discounts for. |
| 276 | 296 | * @return array|false|\WC_Cart Cart object with applied discounts or false if no discounts are applicable. |
| 277 | 297 | */ |
| 278 | 298 | public function get_cart_items_discount_for_bogo( $cart ) {//phpcs:ignore |
| 279 | - if ( ! $this->cart_is_valid() ) { | |
| 280 | - return $cart; | |
| 281 | - } | |
| 299 | + if ( ! $this->cart_is_valid() ) { | |
| 300 | + return $cart; | |
| 301 | + } | |
| 282 | 302 | |
| 283 | - if ( ! defined( 'DOING_AJAX' ) && is_admin() ) { | |
| 284 | - return $cart; | |
| 285 | - } | |
| 303 | + if ( ! defined( 'DOING_AJAX' ) && is_admin() ) { | |
| 304 | + return $cart; | |
| 305 | + } | |
| 286 | 306 | |
| 287 | - // Init Cart - Based Intents except Product & Shipping Intent. | |
| 288 | - $this->intents = $this->prepare_intents( array( 'BOGO' ) ); | |
| 307 | + // Init Cart - Based Intents except Product & Shipping Intent. | |
| 308 | + $this->intents = $this->prepare_intents( array( 'BOGO' ) ); | |
| 289 | 309 | |
| 290 | 310 | return $this->prepare_item_discounts_bogo_free( $this->intents, $cart ); |
| 291 | - } | |
| 311 | + } | |
| 292 | 312 | |
| 293 | 313 | /** |
| 294 | 314 | * Apply the discount to shipping. |
| 295 | 315 | * |
| @@ -329,16 +349,10 @@ | ||
| 329 | 349 | * Get a discount limit from campaign. |
| 330 | 350 | * |
| 331 | 351 | * Compare with total applied count and skip if the user limit is hit. |
| 332 | 352 | */ |
| 333 | - $discount_limit = $intent->campaign->discount_max_user; | |
| 334 | - | |
| 335 | - if ( ! empty( $discount_limit ) ) { | |
| 336 | - $total_applied_campaign = ( new UserLimit )->disco_get_total_applied_campaign( $intent->campaign->id ); | |
| 337 | - | |
| 338 | - if ( $total_applied_campaign >= $discount_limit ) { | |
| 339 | - continue; | |
| 340 | - } | |
| 353 | + if ( ( new UserLimit )->disco_is_limit_reached( $intent->campaign ) ) { | |
| 354 | + continue; | |
| 341 | 355 | } |
| 342 | 356 | |
| 343 | 357 | $items = $this->get_items_for_discount( $cart, $intent->campaign ); |
| 344 | 358 | |
| @@ -465,7 +479,35 @@ | ||
| 465 | 479 | return false; |
| 466 | 480 | } |
| 467 | 481 | |
| 468 | 482 | return in_array( $page_name, $pages, true ); |
| 483 | + } | |
| 484 | + | |
| 485 | + /** | |
| 486 | + * Resolve a campaign's discount type. | |
| 487 | + * | |
| 488 | + * Read through get_discount_rules() rather than the raw config property. | |
| 489 | + * That accessor is what the intents use, so the type reported here is the | |
| 490 | + * one the discount was actually calculated with: it decodes a rules payload | |
| 491 | + * stored as JSON and fills in the defaults, neither of which the magic | |
| 492 | + * property does. The property is also undeclared, so PHPStan cannot see it. | |
| 493 | + * | |
| 494 | + * @param \Disco\App\Utility\Config $campaign Campaign config. | |
| 495 | + * @return string Discount type, or an empty string when it cannot be determined. | |
| 496 | + */ | |
| 497 | + private function campaign_discount_type( Config $campaign ): string { | |
| 498 | + $rules = $campaign->get_discount_rules(); | |
| 499 | + | |
| 500 | + if ( ! is_array( $rules ) || ! isset( $rules[0] ) || ! is_object( $rules[0] ) ) { | |
| 501 | + return ''; | |
| 502 | + } | |
| 503 | + | |
| 504 | + $discount_type = $rules[0]->discount_type ?? ''; | |
| 505 | + | |
| 506 | + if ( ! is_scalar( $discount_type ) ) { | |
| 507 | + return ''; | |
| 508 | + } | |
| 509 | + | |
| 510 | + return (string) $discount_type; | |
| 469 | 511 | } |
| 470 | 512 | |
| 471 | 513 | } |