Skip to content

chore: sync storefront and checkout locale - #3210

Merged
hellofanny merged 7 commits into
feat/multilanguagefrom
chore/send-checkout-locale
Feb 19, 2026
Merged

hellofanny merged 7 commits into
feat/multilanguagefrom
chore/send-checkout-locale

Conversation

@hellofanny

@hellofanny hellofanny commented Feb 16, 2026 •

Copy link
Copy Markdown
Contributor

What's the purpose of this pull request?

Includes the locale as client preferences information to the orderForm, so can be retrieved in checkout and enable the translation.

How it works?

storepreferencesdata ref:
This object contains data from the store's configuration. This is already updated with the session salesChannel/store info.

clientPreferencesData ref:
Object containing preferences from the client who placed the order. Pass the current session locale to enable translation in checkout.

How to test it?

We will simulate the checkout update, run the project locally, enable localization and run the binding setup.

  1. Using the current locale en-US / sc=1
  2. Change locale to pt-BR (sc=2)
  3. Add a new item in the cart
  4. Check if storePreferencesData and clientPreferencesData.locale fields form orderForm has been updated.
locale_checkout_only_when_update_cart.mov

References

Jira Task

Summary by CodeRabbit

  • New Features
    • Session locale now automatically syncs with the shopping cart for consistent language.
    • You can save client preferences (locale and newsletter opt-in) to the cart/order form so language and subscription choices persist across sessions.
  • Improvements
    • Locale synchronization runs as an additional validation step to keep cart data up to date.

@coderabbitai

coderabbitai Bot commented Feb 16, 2026 •

Copy link
Copy Markdown
Contributor

Walkthrough

Adds a new checkout.clientPreferencesData method to the VTEX commerce client, extracts client preferences into a ClientPreferencesData interface, and updates validateCart to conditionally POST clientPreferencesData (locale) to the OrderForm when session and OrderForm locales differ.

Changes

Cohort / File(s) Summary
Type Extraction
packages/api/src/platforms/vtex/clients/commerce/types/OrderForm.ts
Replace inline clientPreferencesData object type with exported ClientPreferencesData interface (locale: string, optional `optinNewsLetter?: boolean
Client API Enhancement
packages/api/src/platforms/vtex/clients/commerce/index.ts
Add checkout.clientPreferencesData({ id, clientPreferencesData }) that POSTs serialized preferences to /api/checkout/pub/orderForm/{id}/attachments/clientPreferencesData, constructing headers (including store cookies) and returning the updated OrderForm.
Cart Validation Integration
packages/api/src/platforms/vtex/resolvers/validateCart.ts
Insert conditional pipeline step after marketingData update to call checkout.clientPreferencesData when session locale exists and differs from orderForm.clientPreferencesData?.locale, then continue with etag and joinItems flow; orderForm is updated accordingly.

Sequence Diagram(s)

sequenceDiagram
    participant Resolver as Client/Resolver
    participant Commerce as VtexCommerceClient
    participant Cookies as StoreCookies
    participant VTEX as VTEX API

    Resolver->>Commerce: GET OrderForm (validateCart)
    Commerce->>VTEX: GET /api/checkout/pub/orderForm/{id}
    VTEX-->>Commerce: OrderForm (includes clientPreferencesData.locale)
    Resolver->>Resolver: compare session.locale vs orderForm.clientPreferencesData.locale

    alt locales differ
        Resolver->>Commerce: checkout.clientPreferencesData({ id, clientPreferencesData })
        Commerce->>Cookies: read cookies -> build headers
        Cookies-->>Commerce: cookie & auth headers
        Commerce->>VTEX: POST /api/checkout/pub/orderForm/{id}/attachments/clientPreferencesData
        VTEX-->>Commerce: Updated OrderForm
        Commerce-->>Resolver: return Updated OrderForm
    else locales match
        Resolver-->>Resolver: continue with existing OrderForm
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

✨ A locale whisper sent across the cart,

Types aligned and headers set to start,
Cookies guide the tiny preference plea,
OrderForm and session now agree,
A small sync done — checkout hums with heart.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: syncing storefront and checkout locale via clientPreferencesData, which is the core objective across all modified files.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch chore/send-checkout-locale

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codesandbox-ci

codesandbox-ci Bot commented Feb 16, 2026 •

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@hellofanny
hellofanny marked this pull request as ready for review February 16, 2026 19:46
@hellofanny
hellofanny requested a review from a team as a code owner February 16, 2026 19:46
@hellofanny
hellofanny requested review from emersonlaurentino and renatomaurovtex and removed request for a team February 16, 2026 19:46
@hellofanny hellofanny self-assigned this Feb 16, 2026
@hellofanny hellofanny added the enhancement New feature or request label Feb 16, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
Before applying any fix, first verify the finding against the current code and
decide whether a code change is actually needed. If the finding is not valid or
no change is required, do not modify code for that item and briefly explain why
it was skipped.

In `@packages/api/src/platforms/vtex/resolvers/validateCart.ts`:
- Around line 379-386: The TypeScript error occurs because ClientPreferencesData
requires both locale and optinNewsLetter but the call to
commerce.checkout.clientPreferencesData only supplies locale; update the payload
to include the existing optinNewsLetter (or other existing clientPreferencesData
fields) when calling commerce.checkout.clientPreferencesData so the shape
matches the ClientPreferencesData interface—i.e., merge/spread
orderForm.clientPreferencesData (or explicitly pass optinNewsLetter:
orderForm.clientPreferencesData?.optinNewsLetter) alongside locale in the call
that updates orderForm, and alternatively consider making optinNewsLetter
optional in the ClientPreferencesData type if the API truly accepts partial
updates.
- Around line 378-386: The locale sync call inside validateCart should be made
non-blocking: wrap the call to commerce.checkout.clientPreferencesData (the
block that assigns orderForm when locale &&
orderForm.clientPreferencesData?.locale !== locale) in a try-catch so transient
errors don't bubble up and break the mutation; on failure catch the error, log a
warning (or processLogger.warn) including the error and the locale, and continue
using the existing orderForm so cart validation proceeds normally.
🧹 Nitpick comments (1)
🤖 Fix all nitpicks with AI agents
Before applying any fix, first verify the finding against the current code and
decide whether a code change is actually needed. If the finding is not valid or
no change is required, do not modify code for that item and briefly explain why
it was skipped.

In `@packages/api/src/platforms/vtex/resolvers/validateCart.ts`:
- Around line 378-386: The locale sync call inside validateCart should be made
non-blocking: wrap the call to commerce.checkout.clientPreferencesData (the
block that assigns orderForm when locale &&
orderForm.clientPreferencesData?.locale !== locale) in a try-catch so transient
errors don't bubble up and break the mutation; on failure catch the error, log a
warning (or processLogger.warn) including the error and the locale, and continue
using the existing orderForm so cart validation proceeds normally.
packages/api/src/platforms/vtex/resolvers/validateCart.ts (1)

378-386: Consider wrapping locale sync in a try-catch to avoid blocking cart validation.

If the clientPreferencesData call fails (e.g., transient network error), the entire validateCart mutation will throw, preventing the user from interacting with their cart. Locale sync is a non-critical enhancement — it shouldn't block the core cart flow.

Proposed defensive handling
   // Sync session locale to checkout so OrderForm has the same clientPreferencesData.locale
   if (locale && orderForm.clientPreferencesData?.locale !== locale) {
-    orderForm = await commerce.checkout.clientPreferencesData({
-      id: orderForm.orderFormId,
-      clientPreferencesData: {
-        locale,
-      },
-    })
+    try {
+      orderForm = await commerce.checkout.clientPreferencesData({
+        id: orderForm.orderFormId,
+        clientPreferencesData: {
+          locale,
+        },
+      })
+    } catch (err) {
+      console.error('Failed to sync locale to orderForm clientPreferencesData', err)
+    }
   }
🤖 Prompt for AI Agents
Before applying any fix, first verify the finding against the current code and
decide whether a code change is actually needed. If the finding is not valid or
no change is required, do not modify code for that item and briefly explain why
it was skipped.
In `@packages/api/src/platforms/vtex/resolvers/validateCart.ts` around lines 378 -
386, The locale sync call inside validateCart should be made non-blocking: wrap
the call to commerce.checkout.clientPreferencesData (the block that assigns
orderForm when locale && orderForm.clientPreferencesData?.locale !== locale) in
a try-catch so transient errors don't bubble up and break the mutation; on
failure catch the error, log a warning (or processLogger.warn) including the
error and the locale, and continue using the existing orderForm so cart
validation proceeds normally.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Fix all issues with AI agents
Before applying any fix, first verify the finding against the current code and
decide whether a code change is actually needed. If the finding is not valid or
no change is required, do not modify code for that item and briefly explain why
it was skipped.

In `@packages/api/src/platforms/vtex/clients/commerce/types/OrderForm.ts`:
- Around line 156-159: Change the optinNewsLetter property on the
ClientPreferencesData interface from "any | null" to "boolean | null" so it
matches the VTEX Checkout API and improves type safety; update the declaration
in the ClientPreferencesData interface (symbol: ClientPreferencesData, property:
optinNewsLetter) accordingly and run type checks to ensure no downstream type
errors.
🧹 Nitpick comments (1)
🤖 Fix all nitpicks with AI agents
Before applying any fix, first verify the finding against the current code and
decide whether a code change is actually needed. If the finding is not valid or
no change is required, do not modify code for that item and briefly explain why
it was skipped.

In `@packages/api/src/platforms/vtex/clients/commerce/types/OrderForm.ts`:
- Around line 156-159: Change the optinNewsLetter property on the
ClientPreferencesData interface from "any | null" to "boolean | null" so it
matches the VTEX Checkout API and improves type safety; update the declaration
in the ClientPreferencesData interface (symbol: ClientPreferencesData, property:
optinNewsLetter) accordingly and run type checks to ensure no downstream type
errors.
packages/api/src/platforms/vtex/clients/commerce/types/OrderForm.ts (1)

156-159: Use boolean | null instead of any | null.

The any type already subsumes null and every other value in TypeScript, making the union redundant. More importantly, the VTEX Checkout API defines optinNewsLetter as a boolean, so tightening this improves type safety and aligns with the actual API contract per the coding guidelines.

♻️ Suggested diff
 export interface ClientPreferencesData {
   locale: string
-  optinNewsLetter?: any | null
+  optinNewsLetter?: boolean | null
 }
🤖 Prompt for AI Agents
Before applying any fix, first verify the finding against the current code and
decide whether a code change is actually needed. If the finding is not valid or
no change is required, do not modify code for that item and briefly explain why
it was skipped.
In `@packages/api/src/platforms/vtex/clients/commerce/types/OrderForm.ts` around
lines 156 - 159, Change the optinNewsLetter property on the
ClientPreferencesData interface from "any | null" to "boolean | null" so it
matches the VTEX Checkout API and improves type safety; update the declaration
in the ClientPreferencesData interface (symbol: ClientPreferencesData, property:
optinNewsLetter) accordingly and run type checks to ensure no downstream type
errors.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/api/src/platforms/vtex/clients/commerce/types/OrderForm.ts`:
- Line 123: The OrderForm type's clientPreferencesData field is non-nullable but
the VTEX GraphQL schema allows null; change the declaration of
clientPreferencesData in the OrderForm type from ClientPreferencesData to
ClientPreferencesData | null (matching the pattern used for clientProfileData,
shippingData, marketingData) and update any related type imports/usages to
accept null (e.g., places that access form.clientPreferencesData?.locale already
handle it). Ensure the symbol names referenced are clientPreferencesData and
ClientPreferencesData within the OrderForm type so the TypeScript definition
aligns with the API contract.

if (locale && form.clientPreferencesData?.locale !== locale) {
return commerce.checkout.clientPreferencesData({
id: form.orderFormId,
clientPreferencesData: { locale },

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: have you checked if this overrides the whole clientPreferencesData in orderform or just overrides the locale? I ask because for marketingData for example we had to merge old and new info to not override the whole object

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

niceee!
I've tried a few times, and it's only updating the locale (at least when I was trying)
There is only two fields in this objects (locale and optinNewsLetter - I can only see locale changing)

image

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, I've tried using the API to set true to optinNewsLetter, and then update only the locale and is overriding 🙈 Lemme try to fix it!!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

locale-update-only.mov

hellofanny and others added 2 commits February 17, 2026 10:37
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/api/src/platforms/vtex/resolvers/validateCart.ts (1)

468-471: ⚠️ Potential issue | 🔴 Critical

Early return bypasses locale sync when there are no item/shipping changes.

When changes.length === 0 && !updateShipping at line 470, the function returns null before reaching the clientPreferencesData locale sync at lines 516–527. If a user switches locale without modifying the cart or shipping, the orderForm locale is never synced to the session locale.

Add a locale mismatch check to the early-return guard:

Proposed fix
+  const localeNeedsUpdate =
+    locale && orderForm.clientPreferencesData?.locale !== locale
+
   // If there are no item changes and no shipping data updates needed, return null
-  if (changes.length === 0 && !updateShipping) {
+  if (changes.length === 0 && !updateShipping && !localeNeedsUpdate) {
     return null
   }

Reuse localeNeedsUpdate at line 517 to keep the pattern consistent.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/api/src/platforms/vtex/resolvers/validateCart.ts` around lines 468 -
471, The early return checking "changes.length === 0 && !updateShipping" skips
the later clientPreferencesData locale sync; update this guard so it also checks
whether the session/orderForm locale needs updating before returning. Compute or
reuse the existing localeNeedsUpdate (from clientPreferencesData check) and
change the condition to only return early when changes.length === 0 &&
!updateShipping && !localeNeedsUpdate, ensuring the locale sync code in the
validateCart flow (clientPreferencesData / localeNeedsUpdate logic) runs when a
locale mismatch exists.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@packages/api/src/platforms/vtex/resolvers/validateCart.ts`:
- Around line 468-471: The early return checking "changes.length === 0 &&
!updateShipping" skips the later clientPreferencesData locale sync; update this
guard so it also checks whether the session/orderForm locale needs updating
before returning. Compute or reuse the existing localeNeedsUpdate (from
clientPreferencesData check) and change the condition to only return early when
changes.length === 0 && !updateShipping && !localeNeedsUpdate, ensuring the
locale sync code in the validateCart flow (clientPreferencesData /
localeNeedsUpdate logic) runs when a locale mismatch exists.

@hellofanny
hellofanny merged commit 1de9186 into feat/multilanguage Feb 19, 2026
3 checks passed
@hellofanny
hellofanny deleted the chore/send-checkout-locale branch February 19, 2026 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants