Repository navigation
fix(storefront): STRF-14923 Persist lang selector with items in cart - #2750
Jwatzkin77 wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit abec23f. Configure here.
| * Updates the active cart's locale before following a language switch link | ||
| */ | ||
| export function cartLocaleSwitcher(cartId, token) { | ||
| if (!cartId) return; |
There was a problem hiding this comment.
Cart locale not updated after add-to-cart
Medium Severity
cartLocaleSwitcher returns immediately when cartId is missing at page load, and it is only invoked from Global.onReady. After an AJAX add-to-cart, currencySelector is re-initialized with the new cart ID, but cartLocaleSwitcher is not, so the cart locale is not persisted if the shopper switches language on the same page.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit abec23f. Configure here.
There was a problem hiding this comment.
Can this situation realistically happen?
| const { href } = event.currentTarget; | ||
| const locale = $(event.currentTarget).data('locale-code'); | ||
|
|
||
| // Navigate regardless of the outcome so a failed update never blocks the switch |
There was a problem hiding this comment.
i like the idea, but it doesnt look like this has neither a timeout nor a loading state? looks like a slow request would just look like a frozen page that would sit indefinitely and with no ux
There was a problem hiding this comment.
@rtalvarez I'm not sure a loading state is really needed, because when you switch the locale via the switcher, the page already navigates/refreshes. This updateCartLocale call just happens in the background so that when the page finishes refreshing, the cart has the correct locale
There was a problem hiding this comment.
🟡 Changes recommended
The language switcher remains uninitialized after the first AJAX cart addition, so switching languages leaves the cart locale unchanged.
1 open issue
What changed in this PR
This PR aims to persist the cart’s locale when shoppers change storefront language with items already in the cart.
Changes:
- Injects the Storefront API token into the page context.
- Initializes a cart locale switcher alongside the existing language selector.
- Attempts a GraphQL locale update before navigation, proceeding even if the request fails.
| File | Description |
|---|---|
templates/layout/base.html |
Exposes the Storefront API token to JavaScript. |
assets/js/theme/global/language-selector.js |
Adds the cart locale mutation and language-link handler. |
assets/js/theme/global.js |
Initializes the switcher with the cart ID and API token. |
Review effort: Balanced
| * Updates the active cart's locale before following a language switch link | ||
| */ | ||
| export function cartLocaleSwitcher(cartId, token) { | ||
| if (!cartId) return; |



What?
Currently the cart locale selector in stencil storefronts does not update the cart locale/language once a single item is in the cart. This is a recommended resolution to fix that behavior
Requirements
[ -
templates/layout/base.html(line 43)gqlTokeninject, right after thecartIdinject on line 42assets/js/theme/global.js(lines 8, 19, 24)cartLocaleSwitcherimport (8)gqlTokenin thethis.contextdestructuring (19)languageSelector()(24)assets/js/theme/global/language-selector.js(lines 34–78)updateCartLocale()(45)cartLocaleSwitcher()(60)a[data-locale-code][data-warning]links, starting at line 63 ] CHANGELOG.md entry added (required for code changes only)Tickets / Documentation
Screenshots (if appropriate)
EN_IT.mov
IT_EN.mov
Note
Medium Risk
Adds client-side GraphQL cart mutations using an injected Storefront API token; failed updates are ignored while navigation still proceeds, which could briefly desync cart locale from the selected language.
Overview
Persists the cart locale when shoppers change language while the cart has items. The layout now injects
gqlTokenso global init can run a newcartLocaleSwitcheralongside the existing language selector.On clicks of language links that carry
data-warning(locale switch warning), the handler calls the Storefront GraphQLupdateCartLocalemutation with the currentcartId, then redirects to the switch URL even if the request fails so the UI never blocks the language change.Reviewed by Cursor Bugbot for commit a1a547e. Bugbot is set up for automated code reviews on this repo. Configure here.