| @@ -77,43 +77,25 @@ | ||
| 77 | 77 | public function calculate_discount(): float { // phpcs:ignore |
| 78 | 78 | $discount_amount = 0; |
| 79 | 79 | $fixed_discount = $this->rule['discount_value']; |
| 80 | 80 | $quantity = (int) $this->item['quantity']; |
| 81 | - | |
| 82 | - /** | |
| 83 | - * "Count Quantity As" combined / variations: a fixed amount is a flat | |
| 84 | - * line discount, so eligibility (any eligible unit) gates it once rather | |
| 85 | - * than scaling with the eligible unit count. | |
| 86 | - */ | |
| 87 | - if ( isset( $this->item['disco_forced_qty'] ) ) { | |
| 88 | - $forced = max( 0, (int) $this->item['disco_forced_qty'] ); | |
| 89 | - $this->discounted_quantities = $forced; | |
| 90 | - | |
| 91 | - return apply_filters( 'disco_final_discounted_amount', $forced > 0 ? (float) $fixed_discount : 0, 'fixed' ); | |
| 92 | - } | |
| 93 | - | |
| 94 | 81 | $min = $this->rule['min'] ? (int) $this->rule['min'] : 0; // phpcs:ignore |
| 95 | 82 | $max = $this->rule['max'] ? (int) $this->rule['max'] : 0; // phpcs:ignore |
| 96 | 83 | $recursive = isset( $this->rule['recursive'] ) && 'yes' === $this->rule['recursive']; // phpcs:ignore |
| 97 | - $item_id = $this->item['product_id']; | |
| 98 | - $rule_ids = array_column( $this->rule['get_ids'], 'id' ); | |
| 84 | + $item_id = $this->item['product_id']; | |
| 85 | + $rule_ids = array_column( $this->rule['get_ids'], 'id' ); | |
| 99 | 86 | $discount_qty = $this->rule['get_quantity'] ? (int) $this->rule['get_quantity'] : 0; // phpcs:ignore |
| 100 | 87 | |
| 101 | 88 | // Default discount for quantities. |
| 102 | 89 | $discount_for_quantities = $min; |
| 103 | 90 | |
| 104 | - /** | |
| 105 | - * Bulk / range capping — only when NOT recursive. Recursive rules ignore | |
| 106 | - * max: the bundle size is always min, so the qualifying quantity must not | |
| 107 | - * be capped to max here, otherwise floor( qty / base ) would divide by | |
| 108 | - * max instead of min and undercount the bundles. | |
| 109 | - */ | |
| 110 | - if ( ! $recursive && $max && $quantity >= $max ) { | |
| 91 | + // Apply for the Bulk Discount when max is set. | |
| 92 | + if ( $max && $quantity >= $max ) { | |
| 111 | 93 | $discount_for_quantities = $max; |
| 112 | 94 | } |
| 113 | 95 | |
| 114 | 96 | // If quantity between min and max then apply discount. |
| 115 | - if ( ! $recursive && $quantity >= $min && $quantity <= $max ) { | |
| 97 | + if ( $quantity >= $min && $quantity <= $max ) { | |
| 116 | 98 | $discount_for_quantities = $quantity; |
| 117 | 99 | } |
| 118 | 100 | |
| 119 | 101 | // Apply for the Bundle & BOGO Discount when recursive is set. |
| @@ -135,17 +117,13 @@ | ||
| 135 | 117 | $discount_for_quantities = $discount_qty; |
| 136 | 118 | $multiplier = $discount_qty; |
| 137 | 119 | } |
| 138 | 120 | |
| 139 | - if ( $recursive && $min > 0 && $quantity >= $min ) { | |
| 140 | - /** | |
| 141 | - * Recursive: the number of bundles is floor( qty / min ). Divide by | |
| 142 | - * min, never by $discount_for_quantities — for BuyXGetY the latter | |
| 143 | - * holds the reward quantity (get_quantity) and would inflate the | |
| 144 | - * multiplier (e.g. buy 2 get 1 with 2 in cart → 1 bundle, not 2). | |
| 145 | - */ | |
| 146 | - $multiplier = (int) floor( $quantity / $min ); | |
| 147 | - $discount_for_quantities *= $multiplier; | |
| 121 | + if ( $recursive ) { | |
| 122 | + if ( $quantity % $min === 0 || $quantity > $discount_for_quantities ) { | |
| 123 | + $multiplier = floor( $quantity / $discount_for_quantities ); | |
| 124 | + $discount_for_quantities *= $multiplier; | |
| 125 | + } | |
| 148 | 126 | } |
| 149 | 127 | |
| 150 | 128 | // Limit discount to Min or Max quantity. |
| 151 | 129 | if ( $quantity >= $discount_for_quantities ) { |