| @@ -14,8 +14,9 @@ | ||
| 14 | 14 | use Disco\App\Campaign; |
| 15 | 15 | use Disco\App\Features\UserLimit; |
| 16 | 16 | use Disco\App\Utility\Config; |
| 17 | 17 | use Disco\App\Utility\Helper; |
| 18 | +use Disco\App\Utility\QuantityCounter; | |
| 18 | 19 | use Disco\App\Utility\Settings; |
| 19 | 20 | |
| 20 | 21 | /** |
| 21 | 22 | * This Class contains all the common function for cart related intents, |
| @@ -379,24 +380,58 @@ | ||
| 379 | 380 | if ( empty( $items ) ) { |
| 380 | 381 | return $discounts; |
| 381 | 382 | } |
| 382 | 383 | |
| 384 | + if ( ! is_array( $rules ) || empty( $rules ) ) { | |
| 385 | + return $discounts; | |
| 386 | + } | |
| 387 | + | |
| 383 | 388 | /** |
| 389 | + * BuyXGetY tiers are mutually exclusive: only one rule applies per | |
| 390 | + * campaign, selected by the buy quantity. Without this, tiers rewarding | |
| 391 | + * different get-products (e.g. rule 1 → product A, rule 2 → product B) | |
| 392 | + * would all fire and stack. Every other intent already collapses | |
| 393 | + * overlapping rules per product via min_max_average. | |
| 394 | + * | |
| 395 | + * Exception: a free products BOGO in `separate` mode is evaluated | |
| 396 | + * per-product (each cart product uses the tier(s) its own quantity | |
| 397 | + * matches), so all rules are kept and the reward is summed downstream. | |
| 398 | + */ | |
| 399 | + if ( 'BuyXGetY' === $campaign->get_discount_intent() && ! $this->is_per_product_free_bogo( $campaign, $rules ) ) { | |
| 400 | + $rules = $this->select_bogo_tier_rule( $campaign, $rules ); | |
| 401 | + | |
| 402 | + if ( empty( $rules ) ) { | |
| 403 | + return $discounts; | |
| 404 | + } | |
| 405 | + } | |
| 406 | + | |
| 407 | + /** | |
| 384 | 408 | * Combined qualifying quantity for recursive BOGO, filtered by the |
| 385 | 409 | * conditions and few-products filters and excluding Disco free items. |
| 386 | 410 | */ |
| 387 | 411 | $total_applicable_qty = $this->get_total_applicable_quantity( $cart, $campaign ); |
| 388 | 412 | |
| 413 | + /** | |
| 414 | + * "Count Quantity As" combined / variations modes pool quantities across | |
| 415 | + * cart line items before deciding eligibility, so they take a dedicated | |
| 416 | + * path. The default `separate` mode keeps the original per-line behaviour. | |
| 417 | + * | |
| 418 | + * BOGO is excluded — count modes only apply to Bundle / Bulk: | |
| 419 | + * - BuyXGetX matches purely on each line's own quantity, so it always | |
| 420 | + * behaves as `separate` regardless of the count mode. | |
| 421 | + * - BuyXGetY rewards the Y (get) product, not the counted X products, so | |
| 422 | + * it stays on the per-item path and only its buy-X gate is pooled | |
| 423 | + * (see {@see self::verify_xproduct_cart_rule()}). | |
| 424 | + */ | |
| 425 | + if ( | |
| 426 | + ! in_array( $campaign->get_discount_intent(), array( 'BuyXGetX', 'BuyXGetY' ), true ) | |
| 427 | + && in_array( $campaign->get_count_quantity_as(), array( 'combined', 'variations' ), true ) | |
| 428 | + ) { | |
| 429 | + return $this->get_grouped_item_discounts( $campaign, $cart, $rules, $total_applicable_qty ); | |
| 430 | + } | |
| 431 | + | |
| 389 | 432 | // Loop through the cart items. |
| 390 | 433 | foreach ( $items as $item ) { |
| 391 | - // Get the item object. | |
| 392 | - $item_objet = $item['data']; | |
| 393 | - | |
| 394 | - // Check if the campaign has rules. | |
| 395 | - if ( ! is_array( $rules ) || empty( $rules ) ) { | |
| 396 | - continue; | |
| 397 | - } | |
| 398 | - | |
| 399 | 434 | // Loop through the discount rules. |
| 400 | 435 | foreach ( $rules as $rule ) { |
| 401 | 436 | if ( is_object( $rule ) ) { |
| 402 | 437 | $rule = (array) $rule; |
| @@ -406,61 +441,225 @@ | ||
| 406 | 441 | if ( ! $this->verify_rules( $campaign, $item, $rule ) ) { |
| 407 | 442 | continue; |
| 408 | 443 | } |
| 409 | 444 | |
| 410 | - $r = md5( microtime() . wp_rand() ); | |
| 411 | - $id = $this->get_cart_item_id( $item ); | |
| 445 | + $this->apply_rule_to_discount( $discounts, $campaign, $rule, $item, $cart, $total_applicable_qty ); | |
| 446 | + }//end foreach | |
| 447 | + }//end foreach | |
| 412 | 448 | |
| 413 | - // Set the discount array. | |
| 414 | - $discounts[ $id ]['original_price'] = $item_objet->get_price(); | |
| 415 | - $discounts[ $id ]['cart_item_key'] = $item['key']; | |
| 449 | + return $discounts; | |
| 450 | + } | |
| 416 | 451 | |
| 417 | - // Set discount applies to | |
| 418 | - $discounts[ $id ]['discount_applies_to'][ $r ] = CalcFactory::discount_applies_to( $rule, $campaign ); | |
| 452 | + /** | |
| 453 | + * Prepare discounts for the `combined` / `variations` counting modes. | |
| 454 | + * | |
| 455 | + * Eligibility is resolved by {@see QuantityCounter}: quantities are pooled | |
| 456 | + * per group (all applicable lines for `combined`, per parent product for | |
| 457 | + * `variations`), range-capped, and threaded into the Calc layer through the | |
| 458 | + * `disco_forced_qty` item key. | |
| 459 | + * | |
| 460 | + * How many units are actually discounted depends on the rule: | |
| 461 | + * - Bundle / Bulk (no `get_quantity`) → every qualifying unit is discounted. | |
| 462 | + * - BOGO (`get_quantity` set) → only the reward units are discounted, | |
| 463 | + * i.e. `get_quantity × bundles`, where `bundles = floor(pool / min)` | |
| 464 | + * (1 for a non-recursive rule). Otherwise a "buy 2 get 1" in combined mode | |
| 465 | + * would wrongly discount the 2 bought units instead of the 1 reward unit. | |
| 466 | + * Reward units are handed out across the eligible lines in cart order. | |
| 467 | + * | |
| 468 | + * @param \Disco\App\Utility\Config $campaign Campaign Config. | |
| 469 | + * @param \WC_Cart $cart Cart Object. | |
| 470 | + * @param array $rules Discount rules. | |
| 471 | + * @param int $total_applicable_qty Combined qualifying quantity. | |
| 472 | + */ | |
| 473 | + private function get_grouped_item_discounts( Config $campaign, \WC_Cart $cart, array $rules, int $total_applicable_qty ): array {//phpcs:ignore | |
| 474 | + $discounts = array(); | |
| 475 | + $quantity_counter = new QuantityCounter; | |
| 419 | 476 | |
| 420 | - // Set Discounts. | |
| 421 | - if ( $rule['discount_type'] === 'free' ) { | |
| 422 | - $discounted_amount = array( | |
| 423 | - 'price' => $item_objet->get_price(), | |
| 424 | - 'discount' => 0, | |
| 425 | - 'line_subtotal' => $item_objet->get_price() * $item['quantity'], | |
| 426 | - 'discounted_quantities' => 0, | |
| 427 | - ); | |
| 428 | - $discounts[ $id ]['free'] = true; | |
| 429 | - $discounts[ $id ]['get_ids'] = $rule['get_ids']; | |
| 430 | - $discounts[ $id ]['get_qty'] = CalcFactory::get_free_quantity( $rule, $item ); | |
| 477 | + foreach ( $rules as $rule ) { | |
| 478 | + if ( is_object( $rule ) ) { | |
| 479 | + $rule = (array) $rule; | |
| 480 | + } | |
| 431 | 481 | |
| 482 | + $eligible_lines = $quantity_counter->get_eligible_units( $cart, $campaign, $rule ); | |
| 483 | + | |
| 484 | + if ( empty( $eligible_lines ) ) { | |
| 485 | + continue; | |
| 486 | + } | |
| 487 | + | |
| 488 | + // Total qualifying units across the pooled lines. | |
| 489 | + $total_qualifying_units = 0; | |
| 490 | + | |
| 491 | + foreach ( $eligible_lines as $eligible_line ) { | |
| 492 | + $total_qualifying_units += $eligible_line['eligible_qty']; | |
| 493 | + } | |
| 494 | + | |
| 495 | + $discountable_units = $this->grouped_discount_budget( $rule, $total_qualifying_units ); | |
| 496 | + | |
| 497 | + /** | |
| 498 | + * A flat amount is charged for the pooled group as a whole. | |
| 499 | + * | |
| 500 | + * Pooling exists so the cart together can reach the tier minimum, so | |
| 501 | + * the rule qualifies for the group rather than for each line that | |
| 502 | + * contributed quantity. The Calc layer returns the rule value whole | |
| 503 | + * for every line it is handed, because a flat amount does not scale | |
| 504 | + * with units, so spreading it across lines multiplied the discount by | |
| 505 | + * the line count: a flat 10 came out as 20 over two lines. | |
| 506 | + * | |
| 507 | + * It is applied to the first eligible line carrying the entire pooled | |
| 508 | + * budget, which is what lets a recursive rule still charge once per | |
| 509 | + * bundle: the Calc layer derives the bundle count from that quantity. | |
| 510 | + * Per unit types keep every line, since their amount is meant to | |
| 511 | + * scale with the units. | |
| 512 | + */ | |
| 513 | + if ( in_array( $rule['discount_type'], array( 'fixed', 'fixed_price' ), true ) ) { | |
| 514 | + $first_line = reset( $eligible_lines ); | |
| 515 | + | |
| 516 | + if ( ! empty( $first_line['item'] ) && $discountable_units > 0 ) { | |
| 517 | + $item = $first_line['item']; | |
| 518 | + $item['disco_forced_qty'] = $discountable_units; | |
| 519 | + | |
| 520 | + $this->apply_rule_to_discount( $discounts, $campaign, $rule, $item, $cart, $total_applicable_qty ); | |
| 521 | + } | |
| 522 | + | |
| 523 | + continue; | |
| 524 | + } | |
| 525 | + | |
| 526 | + // Hand out the discountable units across the eligible lines in cart order. | |
| 527 | + foreach ( $eligible_lines as $eligible_line ) { | |
| 528 | + if ( $discountable_units <= 0 ) { | |
| 529 | + break; | |
| 530 | + } | |
| 531 | + | |
| 532 | + $line_units = (int) min( $eligible_line['eligible_qty'], $discountable_units ); | |
| 533 | + | |
| 534 | + if ( $line_units <= 0 ) { | |
| 535 | + continue; | |
| 536 | + } | |
| 537 | + | |
| 538 | + $discountable_units -= $line_units; | |
| 539 | + | |
| 540 | + $item = $eligible_line['item']; | |
| 541 | + $item['disco_forced_qty'] = $line_units; | |
| 542 | + | |
| 543 | + $this->apply_rule_to_discount( $discounts, $campaign, $rule, $item, $cart, $total_applicable_qty ); | |
| 544 | + } | |
| 545 | + } | |
| 546 | + | |
| 547 | + return $discounts; | |
| 548 | + } | |
| 549 | + | |
| 550 | + /** | |
| 551 | + * Number of units a pooled rule should actually discount. | |
| 552 | + * | |
| 553 | + * BOGO rules (with `get_quantity`) reward `get_quantity × bundles`; every | |
| 554 | + * other rule discounts the whole qualifying pool. | |
| 555 | + * | |
| 556 | + * @param array $rule Discount rule. | |
| 557 | + * @param int $total_qualifying Pooled qualifying quantity. | |
| 558 | + */ | |
| 559 | + private function grouped_discount_budget( array $rule, int $total_qualifying ): int { | |
| 560 | + $reward_quantity = ! empty( $rule['get_quantity'] ) ? (int) $rule['get_quantity'] : 0; // phpcs:ignore | |
| 561 | + | |
| 562 | + if ( $reward_quantity <= 0 ) { | |
| 563 | + // Bundle / Bulk: discount every qualifying unit. | |
| 564 | + return $total_qualifying; | |
| 565 | + } | |
| 566 | + | |
| 567 | + $minimum_quantity = isset( $rule['min'] ) ? absint( $rule['min'] ) : 0; // phpcs:ignore | |
| 568 | + $bundle_count = $minimum_quantity > 0 ? (int) floor( $total_qualifying / $minimum_quantity ) : 1; // phpcs:ignore | |
| 569 | + | |
| 570 | + if ( $bundle_count < 1 ) { | |
| 571 | + $bundle_count = 1; | |
| 572 | + } | |
| 573 | + | |
| 574 | + // BOGO reward: get_quantity per qualifying bundle, never more than the pool. | |
| 575 | + return (int) min( $reward_quantity * $bundle_count, $total_qualifying ); | |
| 576 | + } | |
| 577 | + | |
| 578 | + /** | |
| 579 | + * Populate the discount array for a single cart line + rule pair. | |
| 580 | + * | |
| 581 | + * Shared by both the per-line (`separate`) path and the grouped | |
| 582 | + * (`combined` / `variations`) path so the emitted discount structure is | |
| 583 | + * identical regardless of counting mode. | |
| 584 | + * | |
| 585 | + * @param array $discounts Discount array, by reference. | |
| 586 | + * @param \Disco\App\Utility\Config $campaign Campaign Config. | |
| 587 | + * @param array $rule Single discount rule. | |
| 588 | + * @param array $item Cart item (may carry `disco_forced_qty`). | |
| 589 | + * @param \WC_Cart $cart Cart Object. | |
| 590 | + * @param int $total_applicable_qty Combined qualifying quantity. | |
| 591 | + */ | |
| 592 | + private function apply_rule_to_discount( array &$discounts, Config $campaign, array $rule, array $item, \WC_Cart $cart, int $total_applicable_qty ): void {//phpcs:ignore | |
| 593 | + $product = $item['data']; | |
| 594 | + $rule_key = md5( microtime() . wp_rand() ); | |
| 595 | + $effective_product_id = $this->get_cart_item_id( $item ); | |
| 596 | + | |
| 597 | + // Set the discount array. | |
| 598 | + $discounts[ $effective_product_id ]['original_price'] = $product->get_price(); | |
| 599 | + $discounts[ $effective_product_id ]['cart_item_key'] = $item['key']; | |
| 600 | + | |
| 601 | + // Set discount applies to | |
| 602 | + $discounts[ $effective_product_id ]['discount_applies_to'][ $rule_key ] = CalcFactory::discount_applies_to( $rule, $campaign ); | |
| 603 | + | |
| 604 | + // Set Discounts. | |
| 605 | + if ( $rule['discount_type'] === 'free' ) { | |
| 606 | + $discounted_amount = array( | |
| 607 | + 'price' => $product->get_price(), | |
| 608 | + 'discount' => 0, | |
| 609 | + 'line_subtotal' => $product->get_price() * $item['quantity'], | |
| 610 | + 'discounted_quantities' => 0, | |
| 611 | + ); | |
| 612 | + $discounts[ $effective_product_id ]['free'] = true; | |
| 613 | + $discounts[ $effective_product_id ]['get_ids'] = $rule['get_ids']; | |
| 614 | + | |
| 615 | + if ( 'products' === $campaign->get_bogo_type() ) { | |
| 616 | + if ( in_array( $campaign->get_count_quantity_as(), array( 'combined', 'variations' ), true ) ) { | |
| 432 | 617 | /** |
| 433 | - * Recursive BOGO (any bogo type): base the free quantity on the | |
| 434 | - * combined qualifying quantity instead of this single line, so a | |
| 435 | - * cart whose qualifying quantity totals 4 grants 4 free items. | |
| 618 | + * Combined / variations: the buy-X quantity is pooled across | |
| 619 | + * the cart, so the reward is one bundle (non-recursive) or one | |
| 620 | + * per bundle (recursive) of the pooled total — not per line. | |
| 436 | 621 | */ |
| 437 | - if ( 'yes' === $rule['recursive'] && (int) $rule['min'] > 0 ) { | |
| 438 | - $discounts[ $id ]['get_qty'] = (int) floor( $total_applicable_qty / (int) $rule['min'] ) * (int) $rule['get_quantity']; | |
| 622 | + $minimum_quantity = isset( $rule['min'] ) ? absint( $rule['min'] ) : 0; // phpcs:ignore | |
| 623 | + $reward_quantity = ! empty( $rule['get_quantity'] ) ? (int) $rule['get_quantity'] : 0; // phpcs:ignore | |
| 624 | + | |
| 625 | + if ( 'yes' === $rule['recursive'] && $minimum_quantity > 0 ) { | |
| 626 | + $discounts[ $effective_product_id ]['get_qty'] = (int) floor( $total_applicable_qty / $minimum_quantity ) * $reward_quantity; | |
| 627 | + } else { | |
| 628 | + $discounts[ $effective_product_id ]['get_qty'] = $reward_quantity; | |
| 439 | 629 | } |
| 440 | 630 | } else { |
| 441 | - $discounted_amount = CalcFactory::get_discount( $rule, $item, $cart, $campaign ); | |
| 442 | - $discounts[ $id ]['free'] = false; | |
| 443 | - $discounts[ $id ]['get_ids'] = array(); | |
| 444 | - $discounts[ $id ]['get_qty'] = 0; | |
| 631 | + /** | |
| 632 | + * Separate: X = all products. Every cart product grants the | |
| 633 | + * reward of each tier its own quantity matches (summed), so | |
| 634 | + * two products matching different tiers each contribute. | |
| 635 | + */ | |
| 636 | + $discounts[ $effective_product_id ]['get_qty'] = $this->get_products_bogo_free_quantity( $campaign, $cart ); | |
| 445 | 637 | } |
| 638 | + } else { | |
| 639 | + /** | |
| 640 | + * BuyXGetX ('all'): per line, recursive → floor( line qty / min ) × | |
| 641 | + * get_quantity. Category BOGO never reaches this — its free items are | |
| 642 | + * owned by {@see \Disco\App\Intents\CategoryBogo\CategoryBogo}. | |
| 643 | + */ | |
| 644 | + $discounts[ $effective_product_id ]['get_qty'] = CalcFactory::get_free_quantity( $rule, $item ); | |
| 645 | + } | |
| 646 | + } else { | |
| 647 | + $discounted_amount = CalcFactory::get_discount( $rule, $item, $cart, $campaign ); | |
| 648 | + $discounts[ $effective_product_id ]['free'] = false; | |
| 649 | + $discounts[ $effective_product_id ]['get_ids'] = array(); | |
| 650 | + $discounts[ $effective_product_id ]['get_qty'] = 0; | |
| 651 | + } | |
| 446 | 652 | |
| 447 | - // $discounts[ $id ]['get_ids'] = array_values( $rule['get_ids'] ); | |
| 653 | + $discounts[ $effective_product_id ]['discount_types'][ $rule_key ] = $rule['discount_type']; | |
| 654 | + $discounts[ $effective_product_id ]['intent'][ $rule_key ] = $campaign->get_discount_intent(); | |
| 655 | + $discounts[ $effective_product_id ]['prices'][ $rule_key ] = $discounted_amount['price']; | |
| 656 | + $discounts[ $effective_product_id ]['discounts'][ $rule_key ] = $discounted_amount['discount']; | |
| 657 | + $discounts[ $effective_product_id ]['subtotals'][ $rule_key ] = $discounted_amount['line_subtotal']; | |
| 658 | + $discounts[ $effective_product_id ]['discounted_quantities'][ $rule_key ] = $discounted_amount['discounted_quantities']; | |
| 659 | + $discounts[ $effective_product_id ]['quantities'][ $rule_key ] = $item['quantity']; | |
| 448 | 660 | |
| 449 | - $discounts[ $id ]['discount_types'][ $r ] = $rule['discount_type']; | |
| 450 | - $discounts[ $id ]['intent'][ $r ] = $campaign->get_discount_intent(); | |
| 451 | - $discounts[ $id ]['prices'][ $r ] = $discounted_amount['price']; | |
| 452 | - $discounts[ $id ]['discounts'][ $r ] = $discounted_amount['discount']; | |
| 453 | - $discounts[ $id ]['subtotals'][ $r ] = $discounted_amount['line_subtotal']; | |
| 454 | - $discounts[ $id ]['discounted_quantities'][ $r ] = $discounted_amount['discounted_quantities']; | |
| 455 | - $discounts[ $id ]['quantities'][ $r ] = $item['quantity']; | |
| 456 | - $offers = $this->get_item_offers( $rule ); | |
| 457 | - | |
| 458 | - $discounts[ $id ]['offers'][ $r ] = $offers; | |
| 459 | - }//end foreach | |
| 460 | - }//end foreach | |
| 461 | - | |
| 462 | - return $discounts; | |
| 661 | + $discounts[ $effective_product_id ]['offers'][ $rule_key ] = $this->get_item_offers( $rule ); | |
| 463 | 662 | } |
| 464 | 663 | |
| 465 | 664 | /** |
| 466 | 665 | * Prepare discounts for cart items. |
| @@ -482,12 +681,9 @@ | ||
| 482 | 681 | * Get a discount limit form campaign. |
| 483 | 682 | * |
| 484 | 683 | * Compare with total product meta and apply discount |
| 485 | 684 | */ |
| 486 | - $discount_limit = $intent->campaign->discount_max_user; | |
| 487 | - $total_applied_campaign = ( new UserLimit )->disco_get_total_applied_campaign( $intent->campaign->id ); | |
| 488 | - | |
| 489 | - if ( ! empty( $discount_limit ) && ( $discount_limit >= 0 && $total_applied_campaign >= $discount_limit ) ) { | |
| 685 | + if ( ( new UserLimit )->disco_is_limit_reached( $intent->campaign ) ) { | |
| 490 | 686 | continue; |
| 491 | 687 | } |
| 492 | 688 | |
| 493 | 689 | $items = $this->get_items_for_discount( $cart, $intent->campaign ); |
| @@ -496,13 +692,19 @@ | ||
| 496 | 692 | if ( empty( $get_discounts ) ) { |
| 497 | 693 | continue; |
| 498 | 694 | } |
| 499 | 695 | |
| 500 | - // Only stage campaigns whose discount actually applied to the cart. | |
| 501 | - ( new UserLimit )->disco_start_session_on_checkout( $intent->campaign->id ); | |
| 502 | - | |
| 503 | 696 | foreach ( $get_discounts as $item_id => $discount ) { |
| 697 | + /** | |
| 698 | + * Keep the campaign alongside its amount at the same index. | |
| 699 | + * | |
| 700 | + * Staging cannot happen yet: several campaigns can offer an amount | |
| 701 | + * for the same item and only one of them survives the min/max | |
| 702 | + * reduction below. Crediting every campaign here would consume the | |
| 703 | + * usage limit of campaigns whose discount never reached the cart. | |
| 704 | + */ | |
| 504 | 705 | $discounts[ $item_id ]['discounts'][] = max( $discount['discounts'] ); |
| 706 | + $discounts[ $item_id ]['campaigns'][] = (int) $intent->campaign->id; | |
| 505 | 707 | } |
| 506 | 708 | } |
| 507 | 709 | |
| 508 | 710 | // Check if the discounts are empty. |
| @@ -509,13 +711,27 @@ | ||
| 509 | 711 | if ( empty( $discounts ) ) { |
| 510 | 712 | return false; |
| 511 | 713 | } |
| 512 | 714 | |
| 715 | + $applied_campaign_ids = array(); | |
| 716 | + | |
| 513 | 717 | // Get the min or max discount amount for each item. |
| 514 | 718 | foreach ( $discounts as $item_id => $discount ) { |
| 515 | - $discounts[ $item_id ] = $this->min_max_average( $discount['discounts'] ); | |
| 719 | + $winning_amount = $this->min_max_average( $discount['discounts'] ); | |
| 720 | + $winning_index = array_search( $winning_amount, $discount['discounts'], true ); | |
| 721 | + | |
| 722 | + if ( false !== $winning_index && isset( $discount['campaigns'][ $winning_index ] ) ) { | |
| 723 | + $applied_campaign_ids[ $discount['campaigns'][ $winning_index ] ] = true; | |
| 724 | + } | |
| 725 | + | |
| 726 | + $discounts[ $item_id ] = $winning_amount; | |
| 516 | 727 | } |
| 517 | 728 | |
| 729 | + // Stage only the campaigns that actually won an item. | |
| 730 | + foreach ( array_keys( $applied_campaign_ids ) as $applied_campaign_id ) { | |
| 731 | + ( new UserLimit )->disco_start_session_on_checkout( $applied_campaign_id ); | |
| 732 | + } | |
| 733 | + | |
| 518 | 734 | return $discounts; |
| 519 | 735 | } |
| 520 | 736 | |
| 521 | 737 | /** |
| @@ -523,11 +739,11 @@ | ||
| 523 | 739 | * |
| 524 | 740 | * @param array $intents Intents. |
| 525 | 741 | * @param \WC_Cart $cart Cart. |
| 526 | 742 | * @param array $discounts Discounts. |
| 527 | - * @return int|null | |
| 743 | + * @return int|string|null | |
| 528 | 744 | */ |
| 529 | - public function get_valid_bogo_category_item_id( array $intents, \WC_Cart $cart, array $discounts ) { | |
| 745 | + public function get_valid_bogo_category_item_id( array $intents, \WC_Cart $cart, array $discounts ) {//phpcs:ignore | |
| 530 | 746 | foreach ( $intents as $intent ) { |
| 531 | 747 | if ( |
| 532 | 748 | $intent->campaign->get_discount_intent() !== 'BuyXGetY' || |
| 533 | 749 | $intent->campaign->get_bogo_type() !== 'categories' |
| @@ -534,15 +750,25 @@ | ||
| 534 | 750 | ) { |
| 535 | 751 | continue; |
| 536 | 752 | } |
| 537 | 753 | |
| 754 | + // Collect every eligible category item (in cart order) with its price, | |
| 755 | + // then pick the reward item per the campaign's selection strategy. | |
| 756 | + $candidates = array(); | |
| 757 | + | |
| 538 | 758 | foreach ( $cart->get_cart() as $item ) { |
| 539 | - $item_id = $this->get_cart_item_id( $item ); | |
| 759 | + $cart_item_id = $this->get_cart_item_id( $item ); | |
| 540 | 760 | |
| 541 | - if ( isset( $discounts[ $item_id ] ) && $discounts[ $item_id ] > 0 ) { | |
| 542 | - return $item_id; | |
| 761 | + if ( isset( $discounts[ $cart_item_id ] ) && $discounts[ $cart_item_id ] > 0 ) { // phpcs:ignore | |
| 762 | + $candidates[ $cart_item_id ] = CalcFactory::get_price( $item ); | |
| 543 | 763 | } |
| 544 | 764 | } |
| 765 | + | |
| 766 | + if ( empty( $candidates ) ) { | |
| 767 | + continue; | |
| 768 | + } | |
| 769 | + | |
| 770 | + return $this->pick_reward_item( $candidates, $intent->campaign->get_free_item_selection() ); | |
| 545 | 771 | } |
| 546 | 772 | |
| 547 | 773 | return null; |
| 548 | 774 | } |
| @@ -547,8 +773,34 @@ | ||
| 547 | 773 | return null; |
| 548 | 774 | } |
| 549 | 775 | |
| 550 | 776 | /** |
| 777 | + * Pick the reward cart-item id from eligible candidates. | |
| 778 | + * | |
| 779 | + * Honours the "Free Item Selection" strategy when more than one product | |
| 780 | + * from the reward category is in the cart: | |
| 781 | + * - `cart_order` (default) → the first eligible item in cart order. | |
| 782 | + * - `lowest` → the lowest-priced eligible item. | |
| 783 | + * - `highest` → the highest-priced eligible item. | |
| 784 | + * Ties keep cart order (first match wins). | |
| 785 | + * | |
| 786 | + * @param array<int|string, float> $candidates Map of item id => price, in cart order. | |
| 787 | + * @param string $selection Selection strategy. | |
| 788 | + * @return int|string|null | |
| 789 | + */ | |
| 790 | + private function pick_reward_item( array $candidates, string $selection ) { | |
| 791 | + if ( 'lowest' === $selection ) { | |
| 792 | + return array_keys( $candidates, min( $candidates ), true )[0]; | |
| 793 | + } | |
| 794 | + | |
| 795 | + if ( 'highest' === $selection ) { | |
| 796 | + return array_keys( $candidates, max( $candidates ), true )[0]; | |
| 797 | + } | |
| 798 | + | |
| 799 | + return array_key_first( $candidates ); | |
| 800 | + } | |
| 801 | + | |
| 802 | + /** | |
| 551 | 803 | * Prepare discounts for cart items. |
| 552 | 804 | * |
| 553 | 805 | * @param array $intents Intents. |
| 554 | 806 | * @param \WC_Cart $cart Cart. |
| @@ -558,9 +810,10 @@ | ||
| 558 | 810 | if ( empty( $intents ) ) { |
| 559 | 811 | return false; |
| 560 | 812 | } |
| 561 | 813 | |
| 562 | - $discounts = array(); | |
| 814 | + $discounts = array(); | |
| 815 | + $candidate_campaign_ids = array(); | |
| 563 | 816 | |
| 564 | 817 | // Loop through the intents. |
| 565 | 818 | foreach ( $intents as $intent ) { |
| 566 | 819 | /** |
| @@ -567,12 +820,9 @@ | ||
| 567 | 820 | * Get a discount limit form campaign. |
| 568 | 821 | * |
| 569 | 822 | * Compare with total product meta and apply discount |
| 570 | 823 | */ |
| 571 | - $discount_limit = $intent->campaign->discount_max_user; | |
| 572 | - $total_applied_campaign = ( new UserLimit )->disco_get_total_applied_campaign( $intent->campaign->id ); | |
| 573 | - | |
| 574 | - if ( ! empty( $discount_limit ) && ( $discount_limit >= 0 && $total_applied_campaign >= $discount_limit ) ) { | |
| 824 | + if ( ( new UserLimit )->disco_is_limit_reached( $intent->campaign ) ) { | |
| 575 | 825 | continue; |
| 576 | 826 | } |
| 577 | 827 | |
| 578 | 828 | $items = $this->get_items_for_discount( $cart, $intent->campaign ); |
| @@ -581,17 +831,26 @@ | ||
| 581 | 831 | if ( empty( $get_discounts ) ) { |
| 582 | 832 | continue; |
| 583 | 833 | } |
| 584 | 834 | |
| 585 | - // Only stage campaigns whose discount actually applied to the cart. | |
| 586 | - ( new UserLimit )->disco_start_session_on_checkout( $intent->campaign->id ); | |
| 835 | + /** | |
| 836 | + * Collect the campaign now, stage it later. | |
| 837 | + * | |
| 838 | + * What this method finally grants is decided well below: the rewards | |
| 839 | + * are unioned across campaigns and a BuyXGetY (products) campaign can | |
| 840 | + * then override the whole result authoritatively. Crediting a campaign | |
| 841 | + * here would consume its usage limit even when the final result grants | |
| 842 | + * it nothing. | |
| 843 | + */ | |
| 844 | + $candidate_campaign_ids[] = (int) $intent->campaign->id; | |
| 587 | 845 | |
| 588 | 846 | foreach ( $get_discounts as $item_id => $discount ) { |
| 589 | - $discounts[ $item_id ]['discounts'][] = max( $discount['discounts'] ); | |
| 590 | - $discounts[ $item_id ]['free'] = $discount['free']; | |
| 591 | - $discounts[ $item_id ]['get_ids'] = $discount['get_ids']; | |
| 592 | - $discounts[ $item_id ]['get_qty'] = $discount['get_qty']; | |
| 593 | - $discounts[ $item_id ]['bogo_type'] = $intent->campaign->get_bogo_type(); | |
| 847 | + $discounts[ $item_id ]['discounts'][] = max( $discount['discounts'] ); // phpcs:ignore | |
| 848 | + $discounts[ $item_id ]['free'] = $discount['free']; // phpcs:ignore | |
| 849 | + $discounts[ $item_id ]['get_ids'] = $discount['get_ids']; // phpcs:ignore | |
| 850 | + $discounts[ $item_id ]['get_qty'] = $discount['get_qty']; // phpcs:ignore | |
| 851 | + $discounts[ $item_id ]['bogo_type'] = $intent->campaign->get_bogo_type(); // phpcs:ignore | |
| 852 | + $discounts[ $item_id ]['free_item_selection'] = $intent->campaign->get_free_item_selection(); // phpcs:ignore | |
| 594 | 853 | } |
| 595 | 854 | } |
| 596 | 855 | |
| 597 | 856 | // Check if the discounts are empty. |
| @@ -598,26 +857,309 @@ | ||
| 598 | 857 | if ( empty( $discounts ) ) { |
| 599 | 858 | return false; |
| 600 | 859 | } |
| 601 | 860 | |
| 602 | - // Get the min or max discount amount for each item. | |
| 861 | + /** | |
| 862 | + * Aggregate the per-item free rewards into the flat shape the cart hook | |
| 863 | + * consumes. Union every qualifying reward id (previously this overwrote | |
| 864 | + * on each pass, so only the last cart item was ever granted its free | |
| 865 | + * product) and keep a per-id quantity map for products whose free counts | |
| 866 | + * differ (e.g. recursive BuyXGetX). | |
| 867 | + */ | |
| 868 | + $reward_product_ids = array(); | |
| 869 | + $reward_quantity_map = array(); | |
| 870 | + $has_free_items = false; | |
| 871 | + $bogo_type = 'products'; | |
| 872 | + $free_item_selection = 'cart_order'; | |
| 873 | + | |
| 603 | 874 | foreach ( $discounts as $item_id => $discount ) { |
| 604 | - $discounts['discount'] = $this->min_max_average( $discount['discounts'] ); | |
| 605 | - $discounts['free'] = $discount['free']; | |
| 606 | - $discounts['get_ids'] = $discount['get_ids'] ? array_column( $discount['get_ids'], 'id' ) : array( $item_id ); //@phpcs:ignore | |
| 607 | - $discounts['get_qty'] = $discount['get_qty']; | |
| 608 | - $discounts['bogo_type'] = $discount['bogo_type'] ?? 'products'; | |
| 875 | + $has_free_items = $discount['free']; | |
| 876 | + $bogo_type = $discount['bogo_type'] ?? 'products'; | |
| 877 | + $free_item_selection = $discount['free_item_selection'] ?? 'cart_order'; | |
| 878 | + | |
| 879 | + $rule_reward_ids = ! empty( $discount['get_ids'] ) ? array_column( $discount['get_ids'], 'id' ) : array( $item_id ); // phpcs:ignore | |
| 880 | + | |
| 881 | + foreach ( $rule_reward_ids as $reward_product_id ) { | |
| 882 | + $reward_product_id = (int) $reward_product_id; // phpcs:ignore | |
| 883 | + $reward_product_ids[] = $reward_product_id; // phpcs:ignore | |
| 884 | + $reward_quantity_map[ $reward_product_id ] = (int) $discount['get_qty']; // phpcs:ignore | |
| 885 | + } | |
| 609 | 886 | } |
| 610 | 887 | |
| 888 | + $reward_product_ids = array_values( array_unique( $reward_product_ids ) ); | |
| 889 | + | |
| 890 | + $discounts['discount'] = 0.0; | |
| 891 | + $discounts['free'] = $has_free_items; | |
| 892 | + $discounts['get_ids'] = $reward_product_ids; | |
| 893 | + $discounts['get_qty'] = empty( $reward_quantity_map ) ? 0 : max( $reward_quantity_map ); // phpcs:ignore | |
| 894 | + $discounts['get_qty_map'] = $reward_quantity_map; | |
| 895 | + $discounts['bogo_type'] = $bogo_type; | |
| 896 | + $discounts['free_item_selection'] = $free_item_selection; | |
| 897 | + | |
| 898 | + /** | |
| 899 | + * BuyXGetY (products): replace the flat aggregation with an authoritative | |
| 900 | + * reward map keyed by the tier's own get product, so each cart product | |
| 901 | + * that qualifies grants its tier's reward. | |
| 902 | + * | |
| 903 | + * Category BOGO is not handled here — {@see \Disco\App\Intents\CategoryBogo\CategoryBogo} | |
| 904 | + * owns its reward selection, buy reservation and cart reconciliation. | |
| 905 | + */ | |
| 906 | + foreach ( $intents as $intent ) { | |
| 907 | + $bogo_type = $intent->campaign->get_bogo_type(); | |
| 908 | + | |
| 909 | + if ( 'BuyXGetY' !== $intent->campaign->get_discount_intent() || 'products' !== $bogo_type ) { | |
| 910 | + continue; | |
| 911 | + } | |
| 912 | + | |
| 913 | + // Only free-type rules grant free items; a percent/fixed BOGO must not | |
| 914 | + // fabricate free products here. | |
| 915 | + $rules = $intent->campaign->get_discount_rules(); | |
| 916 | + $first_rule = ( is_array( $rules ) && isset( $rules[0] ) ) ? ( is_object( $rules[0] ) ? (array) $rules[0] : $rules[0] ) : array(); // phpcs:ignore | |
| 917 | + | |
| 918 | + if ( ! isset( $first_rule['discount_type'] ) || 'free' !== $first_rule['discount_type'] ) { | |
| 919 | + continue; | |
| 920 | + } | |
| 921 | + | |
| 922 | + $reward_map = $this->compute_bogo_products_free_map( $intent->campaign, $cart ); | |
| 923 | + | |
| 924 | + // Always authoritative: an empty map means no qualifying (non-reward) | |
| 925 | + // purchase, so no free items — do not fall back to the flat count. | |
| 926 | + $discounts['get_ids'] = array_keys( $reward_map ); // phpcs:ignore | |
| 927 | + $discounts['get_qty_map'] = $reward_map; // phpcs:ignore | |
| 928 | + // Each get-product is freed its own quantity. | |
| 929 | + $discounts['get_qty'] = empty( $reward_map ) ? 0 : max( $reward_map ); // phpcs:ignore | |
| 930 | + $discounts['free'] = ! empty( $reward_map ); // phpcs:ignore | |
| 931 | + $discounts['bogo_type'] = $bogo_type; | |
| 932 | + $discounts['free_item_selection'] = $intent->campaign->get_free_item_selection(); | |
| 933 | + | |
| 934 | + /** | |
| 935 | + * This branch is authoritative for the whole result, so the campaign | |
| 936 | + * that owns it is the only one that can be credited. | |
| 937 | + */ | |
| 938 | + $candidate_campaign_ids = array( (int) $intent->campaign->id ); | |
| 939 | + } | |
| 940 | + | |
| 941 | + /** | |
| 942 | + * Stage the campaigns only once the result is final, and only when it | |
| 943 | + * actually grants free items. | |
| 944 | + */ | |
| 945 | + if ( ! empty( $discounts['free'] ) ) { | |
| 946 | + foreach ( array_unique( $candidate_campaign_ids ) as $applied_campaign_id ) { | |
| 947 | + ( new UserLimit )->disco_start_session_on_checkout( $applied_campaign_id ); | |
| 948 | + } | |
| 949 | + } | |
| 950 | + | |
| 611 | 951 | return $discounts; |
| 612 | 952 | } |
| 613 | 953 | |
| 614 | 954 | /** |
| 955 | + * Authoritative free Y map for a BuyXGetY (products) campaign. | |
| 956 | + * | |
| 957 | + * Returns get-product id => free quantity, attributing each tier's reward to | |
| 958 | + * that tier's own get-products: | |
| 959 | + * - `separate` → each cart line credits every tier whose range | |
| 960 | + * its own quantity matches ([min,max]); rewards | |
| 961 | + * sum, and each tier credits its own get-products. | |
| 962 | + * - `combined` → one pool over every applicable line; the highest | |
| 963 | + * tier it reaches credits its get-products once. | |
| 964 | + * - `variations` → one pool per parent product (all variations of | |
| 965 | + * the same variable count together, and a simple | |
| 966 | + * product is its own pool); every pool that | |
| 967 | + * reaches a tier credits that tier's get-products, | |
| 968 | + * so two qualifying parents grant two rewards. | |
| 969 | + * Recursive tiers multiply by floor(qty/min) (line qty for separate, pool qty | |
| 970 | + * for combined / variations). | |
| 971 | + * | |
| 972 | + * @param \Disco\App\Utility\Config $campaign Campaign Config. | |
| 973 | + * @param \WC_Cart $cart Cart Object. | |
| 974 | + * @return array<int, int> | |
| 975 | + */ | |
| 976 | + private function compute_bogo_products_free_map( Config $campaign, \WC_Cart $cart ): array {//phpcs:ignore | |
| 977 | + $rules = $campaign->get_discount_rules(); | |
| 978 | + | |
| 979 | + if ( ! is_array( $rules ) || empty( $rules ) ) { | |
| 980 | + return array(); | |
| 981 | + } | |
| 982 | + | |
| 983 | + $reward_map = array(); | |
| 984 | + | |
| 985 | + if ( in_array( $campaign->get_count_quantity_as(), array( 'combined', 'variations' ), true ) ) { | |
| 986 | + foreach ( $this->get_bogo_buy_pool_quantities( $cart, $campaign ) as $pool_quantity ) { | |
| 987 | + $this->credit_bogo_pool_reward( $reward_map, $rules, $pool_quantity ); | |
| 988 | + } | |
| 989 | + | |
| 990 | + return $reward_map; | |
| 991 | + } | |
| 992 | + | |
| 993 | + /** | |
| 994 | + * Separate: build the buy quantity per product, then each product earns | |
| 995 | + * the highest tier its quantity qualifies for (min <= qty). The upper | |
| 996 | + * `max` marks where the next tier begins, not a cut-off, so a quantity | |
| 997 | + * above the top tier still earns it and a gap earns the tier below. | |
| 998 | + * Free lines never count. | |
| 999 | + */ | |
| 1000 | + $buy_quantity_per_product = array(); | |
| 1001 | + | |
| 1002 | + foreach ( $cart->get_cart() as $item ) { | |
| 1003 | + if ( ! empty( $item['is_free_product'] ) ) { | |
| 1004 | + continue; | |
| 1005 | + } | |
| 1006 | + | |
| 1007 | + $effective_product_id = $item['product_id']; | |
| 1008 | + | |
| 1009 | + if ( ! empty( $item['variation_id'] ) ) { | |
| 1010 | + $effective_product_id = $item['variation_id']; | |
| 1011 | + } | |
| 1012 | + | |
| 1013 | + if ( ! $campaign->product_is_applicable( $effective_product_id ) ) { | |
| 1014 | + continue; | |
| 1015 | + } | |
| 1016 | + | |
| 1017 | + $product = wc_get_product( $effective_product_id ); | |
| 1018 | + | |
| 1019 | + if ( ! Helper::is_filter_passed( $campaign, array( 'product' => $product ) ) ) { | |
| 1020 | + continue; | |
| 1021 | + } | |
| 1022 | + | |
| 1023 | + $buy_quantity_per_product[ (int) $effective_product_id ] = ( $buy_quantity_per_product[ (int) $effective_product_id ] ?? 0 ) + (int) $item['quantity']; | |
| 1024 | + } | |
| 1025 | + | |
| 1026 | + foreach ( $buy_quantity_per_product as $quantity ) { | |
| 1027 | + $qualifying_rule = null; | |
| 1028 | + $qualifying_minimum = -1; | |
| 1029 | + | |
| 1030 | + foreach ( $rules as $rule ) { | |
| 1031 | + $rule = is_object( $rule ) ? (array) $rule : $rule; // phpcs:ignore | |
| 1032 | + $minimum_quantity = isset( $rule['min'] ) ? absint( $rule['min'] ) : 0; // phpcs:ignore | |
| 1033 | + $reward_quantity = ! empty( $rule['get_quantity'] ) ? (int) $rule['get_quantity'] : 0; // phpcs:ignore | |
| 1034 | + | |
| 1035 | + if ( $minimum_quantity <= 0 || $reward_quantity <= 0 || empty( $rule['get_ids'] ) ) { | |
| 1036 | + continue; | |
| 1037 | + } | |
| 1038 | + | |
| 1039 | + if ( $quantity >= $minimum_quantity && $minimum_quantity > $qualifying_minimum ) { // phpcs:ignore | |
| 1040 | + $qualifying_rule = $rule; | |
| 1041 | + $qualifying_minimum = $minimum_quantity; | |
| 1042 | + } | |
| 1043 | + } | |
| 1044 | + | |
| 1045 | + if ( null === $qualifying_rule ) { | |
| 1046 | + continue; | |
| 1047 | + } | |
| 1048 | + | |
| 1049 | + $minimum_quantity = absint( $qualifying_rule['min'] ); | |
| 1050 | + $reward_quantity = (int) $qualifying_rule['get_quantity']; | |
| 1051 | + $is_recursive = ! empty( $qualifying_rule['recursive'] ) && 'yes' === $qualifying_rule['recursive']; // phpcs:ignore | |
| 1052 | + $reward_total = ( $is_recursive && $minimum_quantity > 0 ) ? ( (int) floor( $quantity / $minimum_quantity ) * $reward_quantity ) : $reward_quantity; // phpcs:ignore | |
| 1053 | + | |
| 1054 | + if ( $reward_total > 0 ) { // phpcs:ignore | |
| 1055 | + foreach ( array_column( $qualifying_rule['get_ids'], 'id' ) as $reward_product_id ) { | |
| 1056 | + $reward_map[ (int) $reward_product_id ] = ( $reward_map[ (int) $reward_product_id ] ?? 0 ) + $reward_total; | |
| 1057 | + } | |
| 1058 | + } | |
| 1059 | + } | |
| 1060 | + | |
| 1061 | + return $reward_map; | |
| 1062 | + } | |
| 1063 | + | |
| 1064 | + /** | |
| 1065 | + * Pooled buy quantities for combined / variations, excluding free lines. | |
| 1066 | + * | |
| 1067 | + * `combined` returns a single pool over every applicable line. `variations` | |
| 1068 | + * returns one pool per parent product, so variations of the same variable | |
| 1069 | + * product count together while different parents stay apart — the same | |
| 1070 | + * grouping {@see QuantityCounter} applies to the buy-X gate. | |
| 1071 | + * | |
| 1072 | + * @param \WC_Cart $cart Cart Object. | |
| 1073 | + * @param \Disco\App\Utility\Config $campaign Campaign Config. | |
| 1074 | + * @return array<string, int> Pool key => pooled quantity. | |
| 1075 | + */ | |
| 1076 | + private function get_bogo_buy_pool_quantities( \WC_Cart $cart, Config $campaign ): array {//phpcs:ignore | |
| 1077 | + $pool_quantities = array(); | |
| 1078 | + $pool_per_parent = 'variations' === $campaign->get_count_quantity_as(); | |
| 1079 | + $cart_items = $cart->get_cart(); | |
| 1080 | + | |
| 1081 | + if ( ! is_array( $cart_items ) ) { | |
| 1082 | + return $pool_quantities; | |
| 1083 | + } | |
| 1084 | + | |
| 1085 | + foreach ( $cart_items as $item ) { | |
| 1086 | + if ( ! empty( $item['is_free_product'] ) ) { | |
| 1087 | + continue; | |
| 1088 | + } | |
| 1089 | + | |
| 1090 | + $effective_product_id = $item['product_id']; | |
| 1091 | + | |
| 1092 | + if ( ! empty( $item['variation_id'] ) ) { | |
| 1093 | + $effective_product_id = $item['variation_id']; | |
| 1094 | + } | |
| 1095 | + | |
| 1096 | + if ( ! $campaign->product_is_applicable( $effective_product_id ) ) { | |
| 1097 | + continue; | |
| 1098 | + } | |
| 1099 | + | |
| 1100 | + $product = wc_get_product( $effective_product_id ); | |
| 1101 | + | |
| 1102 | + if ( ! Helper::is_filter_passed( $campaign, array( 'product' => $product ) ) ) { | |
| 1103 | + continue; | |
| 1104 | + } | |
| 1105 | + | |
| 1106 | + $pool_key = $pool_per_parent ? 'parent_' . (int) $item['product_id'] : 'all'; // phpcs:ignore | |
| 1107 | + | |
| 1108 | + $pool_quantities[ $pool_key ] = ( $pool_quantities[ $pool_key ] ?? 0 ) + (int) $item['quantity']; | |
| 1109 | + } | |
| 1110 | + | |
| 1111 | + return $pool_quantities; | |
| 1112 | + } | |
| 1113 | + | |
| 1114 | + /** | |
| 1115 | + * Credit one pool's reward to the get-products of the tier it qualifies for. | |
| 1116 | + * | |
| 1117 | + * The winning tier is the one with the highest `min` the pool reaches; a | |
| 1118 | + * recursive tier multiplies its reward by the number of complete sets. | |
| 1119 | + * | |
| 1120 | + * @param array $reward_map Reward map (get-product id => quantity), by reference. | |
| 1121 | + * @param array $rules Discount rules. | |
| 1122 | + * @param int $pool_quantity Pooled buy quantity. | |
| 1123 | + */ | |
| 1124 | + private function credit_bogo_pool_reward( array &$reward_map, array $rules, int $pool_quantity ): void {//phpcs:ignore | |
| 1125 | + $qualifying_rule = null; | |
| 1126 | + $qualifying_minimum = -1; | |
| 1127 | + | |
| 1128 | + foreach ( $rules as $rule ) { | |
| 1129 | + $rule = is_object( $rule ) ? (array) $rule : $rule; // phpcs:ignore | |
| 1130 | + $minimum_quantity = isset( $rule['min'] ) ? absint( $rule['min'] ) : 0; // phpcs:ignore | |
| 1131 | + | |
| 1132 | + if ( $minimum_quantity > 0 && $pool_quantity >= $minimum_quantity && $minimum_quantity > $qualifying_minimum ) { // phpcs:ignore | |
| 1133 | + $qualifying_rule = $rule; | |
| 1134 | + $qualifying_minimum = $minimum_quantity; | |
| 1135 | + } | |
| 1136 | + } | |
| 1137 | + | |
| 1138 | + if ( null === $qualifying_rule || empty( $qualifying_rule['get_ids'] ) ) { | |
| 1139 | + return; | |
| 1140 | + } | |
| 1141 | + | |
| 1142 | + $minimum_quantity = absint( $qualifying_rule['min'] ); | |
| 1143 | + $reward_quantity = ! empty( $qualifying_rule['get_quantity'] ) ? (int) $qualifying_rule['get_quantity'] : 0; // phpcs:ignore | |
| 1144 | + $is_recursive = ! empty( $qualifying_rule['recursive'] ) && 'yes' === $qualifying_rule['recursive']; // phpcs:ignore | |
| 1145 | + $quantity = ( $is_recursive && $minimum_quantity > 0 ) ? (int) floor( $pool_quantity / $minimum_quantity ) * $reward_quantity : $reward_quantity; // phpcs:ignore | |
| 1146 | + | |
| 1147 | + if ( $quantity <= 0 ) { | |
| 1148 | + return; | |
| 1149 | + } | |
| 1150 | + | |
| 1151 | + foreach ( array_column( $qualifying_rule['get_ids'], 'id' ) as $reward_product_id ) { | |
| 1152 | + $reward_map[ (int) $reward_product_id ] = ( $reward_map[ (int) $reward_product_id ] ?? 0 ) + $quantity; | |
| 1153 | + } | |
| 1154 | + } | |
| 1155 | + | |
| 1156 | + /** | |
| 615 | 1157 | * Is it a bogo campaign? |
| 616 | 1158 | * |
| 617 | 1159 | * @param \Disco\App\Utility\Config $campaign Campaign Config. |
| 618 | 1160 | */ |
| 619 | - public function is_bogo( Config $campaign ): bool { | |
| 1161 | + public function is_bogo( Config $campaign ): bool { // phpcs:ignore | |
| 620 | 1162 | return in_array( $campaign->get_discount_intent(), array( 'BuyXGetX', 'BuyXGetY' ), true ); |
| 621 | 1163 | } |
| 622 | 1164 | |
| 623 | 1165 | /** |
| @@ -797,8 +1339,97 @@ | ||
| 797 | 1339 | return $total; |
| 798 | 1340 | } |
| 799 | 1341 | |
| 800 | 1342 | /** |
| 1343 | + * Whether a campaign is a free products BOGO evaluated per product. | |
| 1344 | + * | |
| 1345 | + * @param \Disco\App\Utility\Config $campaign Campaign Config. | |
| 1346 | + * @param array $rules Discount rules. | |
| 1347 | + */ | |
| 1348 | + private function is_per_product_free_bogo( Config $campaign, array $rules ): bool { | |
| 1349 | + if ( 'products' !== $campaign->get_bogo_type() ) { | |
| 1350 | + return false; | |
| 1351 | + } | |
| 1352 | + | |
| 1353 | + if ( in_array( $campaign->get_count_quantity_as(), array( 'combined', 'variations' ), true ) ) { | |
| 1354 | + return false; | |
| 1355 | + } | |
| 1356 | + | |
| 1357 | + $first_rule = isset( $rules[0] ) ? ( is_object( $rules[0] ) ? (array) $rules[0] : $rules[0] ) : array(); // phpcs:ignore | |
| 1358 | + | |
| 1359 | + return isset( $first_rule['discount_type'] ) && 'free' === $first_rule['discount_type']; | |
| 1360 | + } | |
| 1361 | + | |
| 1362 | + /** | |
| 1363 | + * Total free Y quantity for a products BuyXGetY campaign in `separate` mode. | |
| 1364 | + * | |
| 1365 | + * X is every applicable product. Each cart line grants the reward of every | |
| 1366 | + * rule (tier) whose range its own quantity falls in — `qty` within | |
| 1367 | + * `[min, max]` (no max = min-only) — summed across matching tiers, and × | |
| 1368 | + * floor(qty/min) when the tier is recursive. The Y product counts as an X | |
| 1369 | + * line too. Summing across all qualifying lines gives the free Y count, so | |
| 1370 | + * two products each matching a different tier grant each tier's reward. | |
| 1371 | + * | |
| 1372 | + * @param \Disco\App\Utility\Config $campaign Campaign Config. | |
| 1373 | + * @param \WC_Cart $cart Cart Object. | |
| 1374 | + */ | |
| 1375 | + private function get_products_bogo_free_quantity( Config $campaign, \WC_Cart $cart ): int {//phpcs:ignore | |
| 1376 | + $rules = $campaign->get_discount_rules(); | |
| 1377 | + $cart_items = $cart->get_cart(); | |
| 1378 | + | |
| 1379 | + if ( ! is_array( $rules ) || ! is_array( $cart_items ) ) { | |
| 1380 | + return 0; | |
| 1381 | + } | |
| 1382 | + | |
| 1383 | + $free_quantity_total = 0; | |
| 1384 | + | |
| 1385 | + foreach ( $cart_items as $item ) { | |
| 1386 | + if ( ! empty( $item['is_free_product'] ) ) { | |
| 1387 | + continue; | |
| 1388 | + } | |
| 1389 | + | |
| 1390 | + $effective_product_id = $item['product_id']; | |
| 1391 | + | |
| 1392 | + if ( ! empty( $item['variation_id'] ) ) { | |
| 1393 | + $effective_product_id = $item['variation_id']; | |
| 1394 | + } | |
| 1395 | + | |
| 1396 | + if ( ! $campaign->product_is_applicable( $effective_product_id ) ) { | |
| 1397 | + continue; | |
| 1398 | + } | |
| 1399 | + | |
| 1400 | + $product = wc_get_product( $effective_product_id ); | |
| 1401 | + | |
| 1402 | + if ( ! Helper::is_filter_passed( $campaign, array( 'product' => $product ) ) ) { | |
| 1403 | + continue; | |
| 1404 | + } | |
| 1405 | + | |
| 1406 | + $line_quantity = (int) $item['quantity']; | |
| 1407 | + | |
| 1408 | + foreach ( $rules as $rule ) { | |
| 1409 | + $rule = is_object( $rule ) ? (array) $rule : $rule; // phpcs:ignore | |
| 1410 | + $minimum_quantity = isset( $rule['min'] ) ? absint( $rule['min'] ) : 0; // phpcs:ignore | |
| 1411 | + $maximum_quantity = ! empty( $rule['max'] ) ? absint( $rule['max'] ) : 0; // phpcs:ignore | |
| 1412 | + $get_quantity = ! empty( $rule['get_quantity'] ) ? (int) $rule['get_quantity'] : 0; // phpcs:ignore | |
| 1413 | + $recursive = ! empty( $rule['recursive'] ) && 'yes' === $rule['recursive']; // phpcs:ignore | |
| 1414 | + | |
| 1415 | + if ( $minimum_quantity <= 0 || $get_quantity <= 0 || $line_quantity < $minimum_quantity ) { | |
| 1416 | + continue; | |
| 1417 | + } | |
| 1418 | + | |
| 1419 | + // Respect the tier's upper bound so each product matches its tier. | |
| 1420 | + if ( $maximum_quantity > 0 && $line_quantity > $maximum_quantity && 'yes' !== $rule['recursive'] ) { | |
| 1421 | + continue; | |
| 1422 | + } | |
| 1423 | + | |
| 1424 | + $free_quantity_total += $recursive ? ( (int) floor( $line_quantity / $minimum_quantity ) * $get_quantity ) : $get_quantity; // phpcs:ignore | |
| 1425 | + } | |
| 1426 | + } | |
| 1427 | + | |
| 1428 | + return $free_quantity_total; | |
| 1429 | + } | |
| 1430 | + | |
| 1431 | + /** | |
| 801 | 1432 | * Get Offer Label. |
| 802 | 1433 | * |
| 803 | 1434 | * @param array $rule Discount Rule. |
| 804 | 1435 | */ |
| @@ -948,9 +1579,56 @@ | ||
| 948 | 1579 | * |
| 949 | 1580 | * @param \Disco\App\Utility\Config $campaign Campaign Config. |
| 950 | 1581 | * @param array $rule Discount Rules. |
| 951 | 1582 | */ |
| 1583 | + /** | |
| 1584 | + * Pick the single BuyXGetY tier that applies for the current cart. | |
| 1585 | + * | |
| 1586 | + * Tiers are ranked by `min`: the winner is the qualifying rule with the | |
| 1587 | + * highest `min` that the buy quantity still meets (the broken upper `max` is | |
| 1588 | + * ignored — a rule qualifies on `qty >= min`, mirroring the gate). On a tie, | |
| 1589 | + * the later rule wins. Returns a list with 0 or 1 rule. | |
| 1590 | + * | |
| 1591 | + * @param \Disco\App\Utility\Config $campaign Campaign Config. | |
| 1592 | + * @param array $rules All discount rules. | |
| 1593 | + */ | |
| 1594 | + private function select_bogo_tier_rule( Config $campaign, array $rules ): array { | |
| 1595 | + $qualifying_rule = null; | |
| 1596 | + $qualifying_minimum = -1; | |
| 1597 | + | |
| 1598 | + foreach ( $rules as $rule ) { | |
| 1599 | + $rule_array = is_object( $rule ) ? (array) $rule : $rule; // phpcs:ignore | |
| 1600 | + | |
| 1601 | + // Rule only counts if the cart meets its buy-X threshold. | |
| 1602 | + if ( ! $this->verify_xproduct_cart_rule( $campaign, $rule_array ) ) { | |
| 1603 | + continue; | |
| 1604 | + } | |
| 1605 | + | |
| 1606 | + $minimum_quantity = isset( $rule_array['min'] ) ? absint( $rule_array['min'] ) : 0; // phpcs:ignore | |
| 1607 | + | |
| 1608 | + if ( $minimum_quantity >= $qualifying_minimum ) { // phpcs:ignore | |
| 1609 | + $qualifying_minimum = $minimum_quantity; | |
| 1610 | + $qualifying_rule = $rule; | |
| 1611 | + } | |
| 1612 | + } | |
| 1613 | + | |
| 1614 | + return null === $qualifying_rule ? array() : array( $qualifying_rule ); // phpcs:ignore | |
| 1615 | + } | |
| 1616 | + | |
| 952 | 1617 | private function verify_xproduct_cart_rule( Config $campaign, array $rule ): bool { //phpcs:ignore |
| 1618 | + /** | |
| 1619 | + * "Count Quantity As" combined / variations: pool the buy-X quantity | |
| 1620 | + * across cart lines instead of requiring a single line to reach `min`, | |
| 1621 | + * so a mixed cart (e.g. 1 + 1 of different products) can satisfy a | |
| 1622 | + * "buy 2" threshold. QuantityCounter honours the same min / max / | |
| 1623 | + * recursive semantics used elsewhere. | |
| 1624 | + */ | |
| 1625 | + if ( in_array( $campaign->get_count_quantity_as(), array( 'combined', 'variations' ), true ) ) { // phpcs:ignore | |
| 1626 | + $quantity_counter = new QuantityCounter; | |
| 1627 | + | |
| 1628 | + return ! empty( $quantity_counter->get_eligible_units( WC()->cart, $campaign, $rule ) ); | |
| 1629 | + } | |
| 1630 | + | |
| 953 | 1631 | $cart = WC()->cart->get_cart(); |
| 954 | 1632 | |
| 955 | 1633 | foreach ( $cart as $item ) { |
| 956 | 1634 | // Skip free products - they must not count towards the BOGO buy quantity, |