Fix cart drawer desync on partial inventory 422#3939
Open
trippyogi wants to merge 1 commit into
Open
Conversation
When /cart/add returns 422 after partially adding available quantity, refresh the cart drawer and bubble via section_id fetches so the UI matches server cart state while keeping the product-form error visible. Fixes Shopify#3921
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
PR Summary
When a customer tries to add more of a product than is available, the cart drawer now shows what was actually added to the cart instead of staying empty, while the product page still displays the availability warning.
Why are these changes introduced?
Fixes #3921
When
/cart/addreturns 422 after partially adding available quantity, the server cart updates but Dawn's error branch did not refresh cart drawer UI. The drawer could open empty or stay stale even though/cart.jshad items and the product form showed the availability error.What approach did you take?
In
assets/product-form.js, whenresponse.status == 422, keep existing error handling (handleErrorMessage, pubsubcartError,CartErrorEvent) unchanged, then call a newrefreshCartUIAfterAvailabilityError()method for cart drawer mode./cart/addresponse already includessectionsanditem_count > 0, call existingcart-drawer.renderContents(response)(same as successful add)./cart.jsto confirmitem_count > 0, then use the Section Rendering API:?section_id=cart-drawerto replace.drawer__inner(removes stale empty-cart markup)?section_id=cart-icon-bubbleto refresh the header bubbleCart notification mode is unchanged. Its
renderContentsdepends on add-response line itemkey, so this helper is scoped toCART-DRAWERonly.Other considerations
sectionsHTML is returned on cart POST endpoints (/cart/add,/cart/change, etc.), not onGET /cart.js?sections=. The initial fix attempt using that GET pattern did not work./cart/change) is a separate code path inassets/cart.jsand is out of scope for this PR.Decision log
?section_id=cart-drawerand?section_id=cart-icon-bubbleGET /cart.js?sections=...;cart-drawer-items.onCartUpdate()onlyonCartUpdatedoes not remove empty-drawer markup or refresh the bubble.GET /cart.js?sections=does not return bundled section HTML.sectionsresponse.status == 422anditem_count > 0response.statuserrorsitem_countcheck via add response or/cart.jscart-notification.renderContentsrequires add-responsekey; applying same logic there needs separate validationVisual impact on existing themes
Merchants using cart drawer mode will see the drawer populate correctly when a customer over-requests quantity and Shopify partially adds available stock. The inline availability error on the product form remains visible. No change for merchants using cart page or cart notification modes. Normal successful add-to-cart behavior is unchanged.
Testing steps/scenarios
/cart/addreturns 422 with availability description./cart.jsreportsitem_count: 1.Demo links
Replace theme IDs with your development theme if sharing with reviewers.
Checklist