feat(medusa): add inventory_quantity support for cart items variants in store API#13945
feat(medusa): add inventory_quantity support for cart items variants in store API#13945bqst wants to merge 6 commits intomedusajs:developfrom
Conversation
🦋 Changeset detectedLatest commit: 3a5563c The changes in this PR will be included in the next version bump. This PR includes changesets to release 74 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Someone is attempting to deploy a commit to the medusajs Team on Vercel. A member of the Team first needs to authorize it. |
olivermrbl
left a comment
There was a problem hiding this comment.
thanks for the contribution! wonder if we should support this across all cart routes for the sake of consistency?
| const withInventoryQuantity = | ||
| req.queryConfig.fields.some((field) => field.includes("items.variant")) && | ||
| req.queryConfig.fields.some((field) => field.includes("items.variant.inventory_quantity")) |
There was a problem hiding this comment.
nit: we don't need both checks here
| const withInventoryQuantity = | |
| req.queryConfig.fields.some((field) => field.includes("items.variant")) && | |
| req.queryConfig.fields.some((field) => field.includes("items.variant.inventory_quantity")) | |
| const withInventoryQuantity = | |
| req.queryConfig.fields.some((field) => field.includes("items.variant.inventory_quantity")) |
There was a problem hiding this comment.
I removed the double check, but this causes an issue. When using fields=+items.variant.inventory_quantity, the helper filters out items.variant.inventory_quantity but doesn't ensure items.variant is fetched, resulting in cart.items[x].variant being undefined and causing this error:
TypeError: Cannot read properties of undefined (reading 'id')
at wrapVariantsWithInventoryQuantityForSalesChannel
Should we ensure that fields=+items.variant.inventory_quantity automatically adds items.variant ?
| const withInventoryQuantity = | ||
| req.queryConfig.fields.some((field) => field.includes("items.variant")) && | ||
| req.queryConfig.fields.some((field) => field.includes("items.variant.inventory_quantity")) |
There was a problem hiding this comment.
| const withInventoryQuantity = | |
| req.queryConfig.fields.some((field) => field.includes("items.variant")) && | |
| req.queryConfig.fields.some((field) => field.includes("items.variant.inventory_quantity")) | |
| const withInventoryQuantity = | |
| req.queryConfig.fields.some((field) => field.includes("items.variant.inventory_quantity")) |
| if (withInventoryQuantity && cart.items?.length && req) { | ||
| await wrapVariantsWithInventoryQuantityForSalesChannel( | ||
| req, | ||
| cart.items.map((item) => item.variant) |
There was a problem hiding this comment.
Bug: Ensure items.variant Included When Fetching Inventory Quantity
The code filters out items.variant.inventory_quantity from the requested fields but doesn't ensure that items.variant itself is included in the query. The comment on line 21 states "and ensure items.variant is included if not already presents" but this isn't implemented. When inventory_quantity is requested and the code tries to map over cart.items.map((item) => item.variant) on line 46, it will fail with a runtime error if item.variant is undefined because the base variant object wasn't fetched. This can happen if a user requests custom fields that exclude items.variant but include items.variant.inventory_quantity, or if defaults don't include items.variant in some configurations.
The fix should ensure items.variant is added to fieldsToFetch when withInventoryQuantity is true, or add a check before accessing item.variant on line 46.
|
I've implemented the feature consistently across all store cart routes as requested. |
|
@olivermrbl this will be ready for a second round of review |
|
This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days. |
|
unstale |
|
This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days. |
|
unstale |
|
This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days. |
|
This PR was closed because it has been stalled for 5 days with no activity. |
|
Thank you for your contribution! 🎉 After an initial review, this PR looks good to us. Here's a summary: ✅ PR template is complete Notes:
Potential Bugs:
variant.inventory_quantity = availability[variant.id].availabilityIf A team member will do a final review before this is merged. We appreciate your patience! |
Summary
What — What changes are introduced in this PR?
This PR adds support for
inventory_quantityon cart item variants in the/store/carts/:idendpoint by applying the existingvariants.inventory_quantitymiddleware from the products endpoint to the cart routes.Why — Why are these changes relevant or necessary?
/store/productsendpointHow — How have these changes been implemented?
variants.inventory_quantitydecorator that:manage_inventory: trueTesting — How have these changes been tested, or how can the reviewer test the feature?
Added comprehensive integration tests in cart.spec.ts (
GET /store/carts/[id]>with inventory itemssection):Tests mirror the existing product endpoint tests for consistency.
Examples
Provide examples or code snippets that demonstrate how this feature works, or how it can be used in practice.
This helps with documentation and ensures maintainers can quickly understand and verify the change.
Before this PR:
After this PR:
Checklist
Please ensure the following before requesting a review:
yarn changesetand follow the promptsAdditional Context
inventory_quantityfor cart line item variants via fields param #12884inventory_quantitywas available on cart items/store/productsendpoint, ensuring consistent behavior across the APINote
Adds sales-channel-aware
inventory_quantityto cart item variants when requested viafields, refactors cart routes to pass request context, and adds integration tests.items.variant.inventory_quantity(when selected viafields) by enhancingrefetchCartto strip the computed field from queries and wrap fetched variants with availability./store/carts,/store/carts/:id, line-items add/update/delete, promotions add/remove, shipping-methods, taxes, complete) to useMedusaStoreRequest/AuthenticatedMedusaRequestand passreqtorefetchCart.wrapVariantsWithInventoryQuantityForSalesChanneland sales-channel validation to accept both authenticated/store requests, handle missing contexts safely, and require a single sales channel for availability.inventory_quantityfor managed variants, absence for unmanaged, multi-item and multi-location aggregation, and sales-channel behavior.@medusajs/medusaandintegration-tests-http.Written by Cursor Bugbot for commit 3a5563c. This will update automatically on new commits. Configure here.