Skip to content

fix: Clear cart (get new orderForm) if there is no checkout cookie present - #3178

Merged
eduardoformiga merged 2 commits into
devfrom
fix/clear-cart-after-order-placed-SFS-2969
Jan 21, 2026
Merged

eduardoformiga merged 2 commits into
devfrom
fix/clear-cart-after-order-placed-SFS-2969

Conversation

@eduardoformiga

@eduardoformiga eduardoformiga commented Jan 16, 2026 •

Copy link
Copy Markdown
Member

What's the purpose of this pull request?

This PR aims to prioritize the checkout cookie in the validateCart mutation, and clear the cart after an order is placed in FastCheckout.

How it works?

  • If this cookie is present, we should use it.
  • If this cookie is not present, we should get a new orderForm (and a new cookie) with an empty cart.

Previously, when the cookie was absent, we were retrieving the old indexedDB cart value from the orderForm, which caused the cart to retain items after an order is placed in FastCheckout.

see this thread and video
https://vtex.slack.com/archives/C08QH7W3C2Y/p1768601252009199?thread_ts=1756491477.629329&cid=C08QH7W3C2Y

- Made the `id` parameter optional in the `orderForm` method.
- Adjusted URL construction to conditionally include `refreshOutdatedData` based on the presence of `id`.
- Removed the `getOrderForm` helper function and integrated its logic directly into the `validateCart` resolver for improved clarity and efficiency.
@eduardoformiga
eduardoformiga requested a review from a team as a code owner January 16, 2026 20:52
@eduardoformiga
eduardoformiga requested review from emersonlaurentino and renatomaurovtex and removed request for a team January 16, 2026 20:52
@coderabbitai

coderabbitai Bot commented Jan 16, 2026 •

Copy link
Copy Markdown
Contributor

Walkthrough

The VTEX commerce client makes orderForm.id optional and builds URLs, query parameters, and request bodies conditionally; the validateCart resolver now fetches the orderForm directly using the cookie-derived id and derives orderNumber from that fetched orderForm.

Changes

Cohort / File(s) Summary
VTEX Commerce API
packages/api/src/platforms/vtex/clients/commerce/index.ts
orderForm parameter id changed to optional. URL now uses /orderForm/{id} when id present or /orderForm when absent. refreshOutdatedData query param included only when id is present; sc always included. Request body: send {} when creating (no id), omit body when updating (id present).
VTEX Cart Validation
packages/api/src/platforms/vtex/resolvers/validateCart.ts
Removed helper getOrderForm usage; resolver performs a direct fetch of the orderForm using cookie-derived id and channel, then derives orderNumber from the fetched orderFormId. Adjusted message-clearing logic and order payload usage to rely on fetched result.
Tests & Mocks
packages/api/test/integration/mutations.test.ts, packages/api/test/mocks/ValidateCartMutation.ts
Integration test sets a real-like checkout cookie value for requests. Mocks updated to reflect swapped query-parameter ordering in several VTEX info endpoints (e.g., sc=1&refreshOutdatedData=true, sc=1&allowOutdatedData=paymentData).

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant Resolver as validateCart Resolver
  participant ClientLib as VtexCommerce Client
  participant VTEX as VTEX API

  Client->>Resolver: validateCart request (cookies incl. orderFormId)
  Resolver->>ClientLib: orderForm({ id? = orderFormIdFromCookie, channel })
  alt id present
    ClientLib->>VTEX: GET /orderForm/{id}?sc=...&refreshOutdatedData=...
    VTEX-->>ClientLib: orderForm (with orderFormId, orderNumber)
  else id absent
    ClientLib->>VTEX: POST /orderForm?sc=...
    VTEX-->>ClientLib: new orderForm (orderFormId, orderNumber)
  end
  ClientLib-->>Resolver: orderForm result
  Resolver->>Resolver: derive orderNumber, adjust payloads, clear messages
  Resolver-->>Client: validation response
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🧭 Optional id, a fork in the road,
Fetching orderForm from cookie's code.
Queries reordered, tests aligned,
Cleaner flow and types refined. ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: making the orderForm id optional to enable fetching a new orderForm when the checkout cookie is absent, which clears the cart.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

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

✨ Finishing touches
  • 📝 Generate docstrings

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

@codesandbox-ci

codesandbox-ci Bot commented Jan 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.

…tion tests

- Added order form cookie to the headers in the integration test runner.
- Adjusted URL parameters in mock data to ensure consistent ordering of query parameters for order form fetch requests.
Comment on lines -345 to -346
const orderNumber =
orderFormIdFromCookie !== '' ? orderFormIdFromCookie : order?.orderNumber

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

🪄 the main point of this PR is to remove the order?.orderNumber fallback. Now, if the orderFormIdFromCookie didn't exist, we get a new orderForm (and new checkout cookie).

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.

just a question: is this supposed to be only for fastcheckout?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not actually, it will work for all checkout types.

@eduardoformiga
eduardoformiga merged commit f3f69bc into dev Jan 21, 2026
@eduardoformiga
eduardoformiga deleted the fix/clear-cart-after-order-placed-SFS-2969 branch January 21, 2026 14:42
thiagopereira-vtex pushed a commit that referenced this pull request Mar 10, 2026
…esent (#3178)

## What's the purpose of this pull request?

This PR aims to prioritize the checkout cookie in the `validateCart`
mutation, and clear the cart after an order is placed in FastCheckout.


## How it works?

- If this cookie is present, we should use it.
- If this cookie is not present, we should get a new orderForm (and a
new cookie) with an empty cart.

Previously, when the cookie was absent, we were retrieving the old
`indexedDB` cart value from the `orderForm`, which caused the cart to
retain items after an order is placed in FastCheckout.

see this thread and video

https://vtex.slack.com/archives/C08QH7W3C2Y/p1768601252009199?thread_ts=1756491477.629329&cid=C08QH7W3C2Y
hellofanny added a commit that referenced this pull request Oct 6, 2026
…back

- Anchor the host allowlist on a label boundary (evil-localhost no longer matches)
- Read only the first x-forwarded-host value instead of force-casting the header
- Remove the order.orderNumber fallback in validateCart: it is client input and
  would bring back the stale cart that #3178 fixed

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
hellofanny added a commit that referenced this pull request Oct 6, 2026
…back

- Anchor the host allowlist on a label boundary (evil-localhost no longer matches)
- Read only the first x-forwarded-host value instead of force-casting the header
- Remove the order.orderNumber fallback in validateCart: it is client input and
  would bring back the stale cart that #3178 fixed

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
lokinmodar pushed a commit to lokinmodar/faststore that referenced this pull request Nov 20, 2026
…ts (SFS-3034)

## Root Cause
Two factors combined to create an infinite loop in preview environments (*.preview.vtex.app):

1. Cookie domain normalization in BFF ignored `x-forwarded-host` header: In preview infrastructure, the public hostname is delivered in `x-forwarded-host`, not `request.headers.host`. The cookie normalization logic failed silently, returning cookies with the wrong domain. Browsers rejected them due to domain mismatch (RFC 6265), leaving no session cookie.

2. Fallback to `order?.orderNumber` was removed in PR vtex#3178: Without a cookie, the resolver had no fallback and passed `id: undefined` to create a new orderForm on every request. Each response provided a new orderFormId, triggering an infinite validation loop (~94 requests in 26s).

## Fixes

1. **Header priority fix** (`@faststore/core`): Check `x-forwarded-host` before `request.headers.host` when normalizing cookie domains. This ensures preview environments (where the public hostname is in `x-forwarded-host`) correctly rewrite cookie domains.

2. **Defensive fallback** (`@faststore/api`): Reintroduce fallback to `order?.orderNumber` when `orderFormIdFromCookie` is absent. This protects against cookie delivery issues and provides resilience in environments with cookie restrictions.

## Impact
- Fixes infinite loop in preview/QA environments
- No impact on production (custom domains continue to work)
- Minimal risk: both changes restore defensive patterns that existed in v3.93.x

## References
- Regression introduced by: PR vtex#3178 (Jan 21, 2026)
- Incomplete fix attempt: PR vtex#3190 (Feb 2, 2026 - didn't handle x-forwarded-host)
- Related issue: vtex#3188 (prematurely closed)
- Customer ticket: #1467584 / SO-686
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.

4 participants