Fix: Error selecting applicable discounts #1434
Merged
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.
The discount selection currently takes all the discounts with and without coupon code because of this conditional query
when the
$cart?->coupon_code
is true, it executes the first closure in which it selects the discounts having the correct coupon but also the discounts with null coupon code or empty strings. Only the second clousure (executed when$cart?->coupon_code
is false) should selects the discount with null coupons or empty.This issue currently make the query selects all the discounts that has no coupon_code (even the expired ones, because of the OR clause)
Below a simplified result of the ->toSql() of the query.
The PR essentially changes the positive closure. By making use of a where condition with a closure, the query builder creates a logical group around the WHERE conditions and the resulting SQL query will have the affected conditions placed in parentheses, as the simplified example below:
PS: the provided example are simplified because for readability purpose i removed the subqueries relative to channels, customer groups and discount's purchasables.