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.
Fixes a couple of problems with order line item quantity validation.
First of all, the
$qty
variable is being calculated only one time per LineItem object instance because Yii creates all validators when you request them the first time and then reuses them inside the same model object for all subsequent validations. And although the quantity validation functions will be called each time the validation runs, the actual quantities of purchasables will not be updated, they will be stored in the closure and you will always validate the first set of purchasable quantities each time you run a validation. I understand that this calculation had been put outside of the validator to optimize and minimize the code, but it will fail in case when you:Also, I believe the current version of the code contains a typo:
I think there should be the
$item
variable used instead of$lineItem
. Because in case the current line item is new, only it's quantity will be used, ignoring all other line items in the order with the same purchasableId.My fix encloses the quantity calculation into a closure, allowing to re-calculate the quantities each time properly while keeping the code minimal. And although I had to get rid of the optimization, I don't think it will have any noticeable impact on the performance.