Skip to content

Apply discounts on guest carts when max_uses_per_user is set#2470

Draft
glennjacobs wants to merge 1 commit into
1.xfrom
fix/coupon-max-uses-per-user-no-effect-2442
Draft

Apply discounts on guest carts when max_uses_per_user is set#2470
glennjacobs wants to merge 1 commit into
1.xfrom
fix/coupon-max-uses-per-user-no-effect-2442

Conversation

@glennjacobs
Copy link
Copy Markdown
Contributor

Summary

  • AbstractDiscountType::checkDiscountConditions() now only runs the per-user usage check when the cart actually has a user. Guest carts with max_uses_per_user set no longer silently fail the condition.
  • Added three tests covering guest cart, under-limit user, at-limit user.

Why

The old condition was:

if ($validMaxUses && $this->discount->max_uses_per_user) {
    $validMaxUses = $cart->user && ($this->usesByUser($cart->user) < $this->discount->max_uses_per_user);
}

If the cart had no user attached (guest checkout, or anonymous cart pre-login), $cart->user was null, so $validMaxUses became false and the discount was filtered out — but only at calculation time, after the coupon code had already been accepted onto the cart. From the customer's point of view the coupon "worked" but their total never changed. This is exactly what #2442 reported.

A per-user limit only makes sense when a user is known, so the new condition skips the check entirely for guest carts and preserves whatever $validMaxUses was already set to:

if ($validMaxUses && $this->discount->max_uses_per_user && $cart->user) {
    $validMaxUses = $this->usesByUser($cart->user) < $this->discount->max_uses_per_user;
}

Logged-in users still hit the limit correctly — the third test asserts that an attached usage row blocks reuse.

Fixes #2442.

Test plan

  • vendor/bin/pest --testsuite=core — 506 passing
  • Manual: discount with max_uses_per_user = 1 on a guest cart → discount applies to total
  • Manual: same discount on a logged-in user who has already used it → total unchanged

checkDiscountConditions() short-circuited validMaxUses to false on any
cart without a user the moment max_uses_per_user was set, which meant
guest carts saw the coupon "applied" but got zero discount. The
per-user limit can only be enforced when a user is known, so only run
the check in that case and otherwise leave the existing validMaxUses
value alone.

Fixes #2442
@ryanmitchell
Copy link
Copy Markdown
Contributor

Does this mean that the discount can be used indefinitely by a non-logged in user?
If so would it not be better to reject the discount code if there is no logged in user.

@glennjacobs glennjacobs marked this pull request as draft May 19, 2026 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

Coupon Applies but there is no effect on prices/calculations

2 participants