Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adding line items to cart forwards cart_id to the pricing context #10376

Open
wants to merge 1 commit into
base: v1.x
Choose a base branch
from

Conversation

ajdani
Copy link

@ajdani ajdani commented Nov 30, 2024

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 item
  • txCartService.addOrUpdateLineItems(...) is called to add the line item to the respective cart

When there is a custom PriceSelectionStrategy in place, which relies on the cart_id in the context to actually calculate a price, the first method call lineItemService.generate(...) will fail, as the cart is not added to the context, thus Cannot 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 call getProductVariantsPricing the cart context is available when we call the PriceSelectionStrategy.

See that the current implementation of CartService.addOrUpdateLineItems(...) does this already correct as we can see here:

          if (currentItem) {
            const variantsPricing = await this.pricingService_
              .withTransaction(transactionManager)
              .getProductVariantsPricing(
                [
                  {
                    variantId: item.variant_id!,
                    quantity: item.quantity,
                  },
                ],
                {
                  cart_id: cart.id,
                  region_id: cart.region_id,
                  customer_id: cart.customer_id,
                  include_discount_prices: true,
                }
              )

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 new v1 release, as currently we are relying on a dirty fix which avoids this issue.

@ajdani ajdani requested a review from a team as a code owner November 30, 2024 05:12
Copy link

changeset-bot bot commented Nov 30, 2024

⚠️ No Changeset found

Latest commit: 1cf25cb

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Nov 30, 2024

@drainer-cloudflight is attempting to deploy a commit to the medusajs Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants