fix: adding line items to cart forwards cart_id to the pricing context #10376
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.
Problem description
When calling
[POST] /store/carts/{id}/line-items
to add an item into the cart, basically two major things happen:lineItemService.generate(...)
is called to generate the line itemtxCartService.addOrUpdateLineItems(...)
is called to add the line item to the respective cartWhen there is a custom
PriceSelectionStrategy
in place, which relies on thecart_id
in the context to actually calculate a price, the first method calllineItemService.generate(...)
will fail, as thecart
is not added to the context, thusCannot generate line item for variant <id> without a price
is thrown.Solution
This PR should fix the issue, by passing the cart context forward to the nested method calls in
addOrUpdateLineItem
such that when we callgetProductVariantsPricing
the cart context is available when we call thePriceSelectionStrategy
.See that the current implementation of
CartService.addOrUpdateLineItems(...)
does this already correct as we can see here:Unfortunately I don't really know how it influences orders / swaps, etc. and there is no test added yet, as the test setup needed seems to be quite complex, as currently in the tests for the
LineItemService
the price calculation strategy is completely mocked and independent of any input values.However, I'm open for discussing how and if we can bring this fix into the
v1.x
branch and hopefully into a newv1
release, as currently we are relying on a dirty fix which avoids this issue.