Enforce max_uses_per_user in the standalone CouponValidator#2473
Merged
Conversation
CouponValidator::validate() only checked the global max_uses count, so a coupon validated through Discounts::validateCoupon() (or the ValidCoupon rule) kept passing even after a user had hit their per-user cap. The same coupon applied via cart->update() was rejected correctly, leading to inconsistent behaviour between the two paths. Look up the authenticated user via auth() and count their existing uses on the matched discount when max_uses_per_user is set. Guests still pass since per-user limits cannot be enforced without a user, matching the cart-side behaviour fixed in #2442. Fixes #2253
alecritson
approved these changes
May 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
CouponValidator::validate()now also checksmax_uses_per_useragainst the authenticated user when both are present.Why
The standalone validator only checked the global
max_usescount, while the cart-side path (AbstractDiscountType::checkDiscountConditions()) also enforcesmax_uses_per_user. That left the two entry points in conflict —Discounts::validateCoupon('FOO')or theValidCouponrule would returntruefor a user who had already hit their cap, while applying the same coupon viacart->update(['coupon_code' => 'FOO'])correctly rejected it.The reporter calls this out as an abuse vector — front-end coupon-check endpoints that use the validator give the wrong answer.
Looking up
auth()->user()inside the validator keeps the interface signature unchanged (validate(string $coupon): bool). Guests still pass because there is no per-user state to enforce.Fixes #2253.
Test plan
vendor/bin/pest --testsuite=core— 506 passingDiscounts::validateCoupon($coupon)→ returnsfalse