| @@ -251,17 +251,39 @@ | ||
| 251 | 251 | public function calculate_discount( $cost, $discount_type, $discount_value ) { |
| 252 | 252 | $cost = (float) $cost; |
| 253 | 253 | $discount_value = (float) $discount_value; |
| 254 | 254 | |
| 255 | - if ( 'percent' === $discount_type ) { | |
| 256 | - $cost = $cost * $discount_value / 100; | |
| 257 | - } | |
| 255 | + switch ( $discount_type ) { | |
| 256 | + case 'percent': | |
| 257 | + return $cost * $discount_value / 100; | |
| 258 | 258 | |
| 259 | - if ( 'fixed' === $discount_type || 'fixed_per_product' === $discount_type ) { | |
| 260 | - $cost = $discount_value; | |
| 261 | - } | |
| 259 | + case 'fixed': | |
| 260 | + case 'fixed_per_product': | |
| 261 | + return $discount_value; | |
| 262 | 262 | |
| 263 | - return $cost; | |
| 263 | + case 'free': | |
| 264 | + // The whole cost comes off, which is what makes the item free. | |
| 265 | + return $cost; | |
| 266 | + | |
| 267 | + default: | |
| 268 | + /** | |
| 269 | + * Anything else discounts nothing. | |
| 270 | + * | |
| 271 | + * This used to fall through and return $cost untouched, which the | |
| 272 | + * callers then treat as the discount: ProductIntent hands it the | |
| 273 | + * product price and CartIntent the cart subtotal, so an | |
| 274 | + * unrecognised type gave away the product or the entire cart. | |
| 275 | + * | |
| 276 | + * Only Bulk, Bundle and BOGO route through the Calc classes, | |
| 277 | + * where every type has an implementation; Product and Cart come | |
| 278 | + * straight here, so the gap was reachable by any rule carrying a | |
| 279 | + * type this method does not know: a campaign saved while | |
| 280 | + * fixed_price and percent_per_product were still offered in the | |
| 281 | + * builder, a REST write (discount_type inside discount_rules is | |
| 282 | + * not validated), or a hand edited row. | |
| 283 | + */ | |
| 284 | + return 0.0; | |
| 285 | + }//end switch | |
| 264 | 286 | } |
| 265 | 287 | |
| 266 | 288 | /** |
| 267 | 289 | * Get the discounted price. |