| @@ -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 |
| @@ -64,10 +65,8 @@ | ||
| 64 | 65 | } |
| 65 | 66 | |
| 66 | 67 | $discounts = array(); |
| 67 | 68 | |
| 68 | - $prev_discount = PHP_INT_MAX; | |
| 69 | - | |
| 70 | 69 | // Foreach intent, apply the discount. |
| 71 | 70 | foreach ( $this->intents as $intent ) { |
| 72 | 71 | /** |
| 73 | 72 | * Get a discount limit form campaign. |
| @@ -73,18 +72,9 @@ | ||
| 73 | 72 | * Get a discount limit form campaign. |
| 74 | 73 | * |
| 75 | 74 | * Compare with total product meta and apply discount |
| 76 | 75 | */ |
| 77 | - $discount_limit = $intent->campaign->discount_max_user; | |
| 78 | - $total_applied_campaign = ( new UserLimit )->disco_get_total_applied_campaign( $intent->campaign->id ); | |
| 79 | - | |
| 80 | - if ( | |
| 81 | - ! empty( $discount_limit ) | |
| 82 | - && ( | |
| 83 | - $discount_limit >= 0 | |
| 84 | - && $total_applied_campaign >= $discount_limit | |
| 85 | - ) | |
| 86 | - ) { | |
| 76 | + if ( ( new UserLimit )->disco_is_limit_reached( $intent->campaign ) ) { | |
| 87 | 77 | continue; |
| 88 | 78 | } |
| 89 | 79 | |
| 90 | 80 | $discount = $intent->get_discounts( (float) $price, $product ); |
| @@ -92,15 +82,30 @@ | ||
| 92 | 82 | if ( empty( $discount ) ) { |
| 93 | 83 | continue; |
| 94 | 84 | } |
| 95 | 85 | |
| 96 | - // Get applied discount campaign. | |
| 97 | - if ( $discount > 0 && $prev_discount > $discount ) { | |
| 98 | - $this->applied_campaign_id = $intent->campaign->id; | |
| 99 | - $prev_discount = $discount; | |
| 100 | - } | |
| 101 | - | |
| 102 | - $discounts[] = $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 | + ); | |
| 103 | 108 | } |
| 104 | 109 | |
| 105 | 110 | // If no discount is applied, return the original price. |
| 106 | 111 | if ( empty( $discounts ) ) { |
| @@ -106,16 +111,12 @@ | ||
| 106 | 111 | if ( empty( $discounts ) ) { |
| 107 | 112 | return $price; |
| 108 | 113 | } |
| 109 | 114 | |
| 110 | - $discount_type = $intent->campaign->discount_rules[0]['discount_type']; | |
| 115 | + // Pick the winning discount amount, then map it back to the campaign that produced it. | |
| 116 | + $discounted_amount = $this->min_max_average( array_values( $discounts ) ); | |
| 117 | + $this->applied_campaign_id = (int) array_search( $discounted_amount, $discounts, true ); | |
| 111 | 118 | |
| 112 | - /** | |
| 113 | - * Get the min or max discount amount according to plugin settings. | |
| 114 | - * Apply the filter to modify final discounted amount based on discount types. | |
| 115 | - */ | |
| 116 | - $discounted_amount = apply_filters( 'disco_final_discounted_amount', $this->min_max_average( $discounts ), $discount_type ); | |
| 117 | - | |
| 118 | 119 | if ( $discounted_amount <= $price ) { |
| 119 | 120 | // Get an applied campaign from DiscountLimit class. |
| 120 | 121 | ( new UserLimit )->disco_start_session_on_checkout( $this->applied_campaign_id ); |
| 121 | 122 | |
| @@ -152,18 +153,9 @@ | ||
| 152 | 153 | * Get a discount limit form campaign. |
| 153 | 154 | * |
| 154 | 155 | * Compare with total product meta and apply discount |
| 155 | 156 | */ |
| 156 | - $discount_limit = $intent->campaign->discount_max_user; | |
| 157 | - $total_applied_campaign = ( new UserLimit )->disco_get_total_applied_campaign( $intent->campaign->id ); | |
| 158 | - | |
| 159 | - if ( | |
| 160 | - ! empty( $discount_limit ) | |
| 161 | - && ( | |
| 162 | - $discount_limit >= 0 | |
| 163 | - && $total_applied_campaign >= $discount_limit | |
| 164 | - ) | |
| 165 | - ) { | |
| 157 | + if ( ( new UserLimit )->disco_is_limit_reached( $intent->campaign ) ) { | |
| 166 | 158 | continue; |
| 167 | 159 | } |
| 168 | 160 | |
| 169 | 161 | $items = $this->get_items_for_discount( $cart, $intent->campaign ); |
| @@ -203,8 +195,10 @@ | ||
| 203 | 195 | if ( ! empty( $discount_label ) ) { |
| 204 | 196 | $label = $discount_label; |
| 205 | 197 | } |
| 206 | 198 | |
| 199 | + $cart_fee = $this->get_discount_exclude_tax( $cart_fee, $cart ); | |
| 200 | + | |
| 207 | 201 | $cart->add_fee( $label, -$cart_fee ); |
| 208 | 202 | $cart->set_session(); |
| 209 | 203 | } |
| 210 | 204 | } |
| @@ -257,8 +251,10 @@ | ||
| 257 | 251 | $total_discount += $discounts[ $id ]; |
| 258 | 252 | } |
| 259 | 253 | |
| 260 | 254 | if ( $total_discount > 0 ) { |
| 255 | + $total_discount = $this->get_discount_exclude_tax( $total_discount, $cart ); | |
| 256 | + | |
| 261 | 257 | $cart->add_fee( __( 'Discount', 'disco' ), -$total_discount ); |
| 262 | 258 | } |
| 263 | 259 | |
| 264 | 260 | $cart->set_session(); |
| @@ -274,21 +270,21 @@ | ||
| 274 | 270 | * @param \WC_Cart $cart WooCommerce cart object to calculate discounts for. |
| 275 | 271 | * @return array|false|\WC_Cart Cart object with applied discounts or false if no discounts are applicable. |
| 276 | 272 | */ |
| 277 | 273 | public function get_cart_items_discount_for_bogo( $cart ) {//phpcs:ignore |
| 278 | - if ( ! $this->cart_is_valid() ) { | |
| 279 | - return $cart; | |
| 280 | - } | |
| 274 | + if ( ! $this->cart_is_valid() ) { | |
| 275 | + return $cart; | |
| 276 | + } | |
| 281 | 277 | |
| 282 | - if ( ! defined( 'DOING_AJAX' ) && is_admin() ) { | |
| 283 | - return $cart; | |
| 284 | - } | |
| 278 | + if ( ! defined( 'DOING_AJAX' ) && is_admin() ) { | |
| 279 | + return $cart; | |
| 280 | + } | |
| 285 | 281 | |
| 286 | - // Init Cart - Based Intents except Product & Shipping Intent. | |
| 287 | - $this->intents = $this->prepare_intents( array( 'BOGO' ) ); | |
| 282 | + // Init Cart - Based Intents except Product & Shipping Intent. | |
| 283 | + $this->intents = $this->prepare_intents( array( 'BOGO' ) ); | |
| 288 | 284 | |
| 289 | 285 | return $this->prepare_item_discounts_bogo_free( $this->intents, $cart ); |
| 290 | - } | |
| 286 | + } | |
| 291 | 287 | |
| 292 | 288 | /** |
| 293 | 289 | * Apply the discount to shipping. |
| 294 | 290 | * |
| @@ -328,16 +324,10 @@ | ||
| 328 | 324 | * Get a discount limit from campaign. |
| 329 | 325 | * |
| 330 | 326 | * Compare with total applied count and skip if the user limit is hit. |
| 331 | 327 | */ |
| 332 | - $discount_limit = $intent->campaign->discount_max_user; | |
| 333 | - | |
| 334 | - if ( ! empty( $discount_limit ) ) { | |
| 335 | - $total_applied_campaign = ( new UserLimit )->disco_get_total_applied_campaign( $intent->campaign->id ); | |
| 336 | - | |
| 337 | - if ( $total_applied_campaign >= $discount_limit ) { | |
| 338 | - continue; | |
| 339 | - } | |
| 328 | + if ( ( new UserLimit )->disco_is_limit_reached( $intent->campaign ) ) { | |
| 329 | + continue; | |
| 340 | 330 | } |
| 341 | 331 | |
| 342 | 332 | $items = $this->get_items_for_discount( $cart, $intent->campaign ); |
| 343 | 333 | |
| @@ -464,7 +454,35 @@ | ||
| 464 | 454 | return false; |
| 465 | 455 | } |
| 466 | 456 | |
| 467 | 457 | return in_array( $page_name, $pages, true ); |
| 458 | + } | |
| 459 | + | |
| 460 | + /** | |
| 461 | + * Resolve a campaign's discount type. | |
| 462 | + * | |
| 463 | + * Read through get_discount_rules() rather than the raw config property. | |
| 464 | + * That accessor is what the intents use, so the type reported here is the | |
| 465 | + * one the discount was actually calculated with: it decodes a rules payload | |
| 466 | + * stored as JSON and fills in the defaults, neither of which the magic | |
| 467 | + * property does. The property is also undeclared, so PHPStan cannot see it. | |
| 468 | + * | |
| 469 | + * @param \Disco\App\Utility\Config $campaign Campaign config. | |
| 470 | + * @return string Discount type, or an empty string when it cannot be determined. | |
| 471 | + */ | |
| 472 | + private function campaign_discount_type( Config $campaign ): string { | |
| 473 | + $rules = $campaign->get_discount_rules(); | |
| 474 | + | |
| 475 | + if ( ! is_array( $rules ) || ! isset( $rules[0] ) || ! is_object( $rules[0] ) ) { | |
| 476 | + return ''; | |
| 477 | + } | |
| 478 | + | |
| 479 | + $discount_type = $rules[0]->discount_type ?? ''; | |
| 480 | + | |
| 481 | + if ( ! is_scalar( $discount_type ) ) { | |
| 482 | + return ''; | |
| 483 | + } | |
| 484 | + | |
| 485 | + return (string) $discount_type; | |
| 468 | 486 | } |
| 469 | 487 | |
| 470 | 488 | } |