#17074·medusa

[Bug]: Signed-out cart customer resolution depends on guest/registered creation order — is this intended?

Author: jkhalliganCreated Sep 30, 2026Updated Sep 30, 2026
Labelstype: bugrequires-team

What happened?

On Medusa 2.18.0, setting an email on a cart with no explicit customer selects the first customer record returned for that email. When an older guest record and a later registered record share the email, the guest wins. Reversing their creation order makes the registered record win instead.

This matters for guest purchase followed by account registration: later signed-out purchases can stay with the older guest record and not appear in the registered account's order history.

Could you clarify whether this creation-order-dependent behavior is intended? This report does not assume signed-out checkout should prove account ownership or automatically transfer earlier guest orders. We are keeping stock behavior while asking for clarification.

Expected behavior / question

A documented policy for which record owns a signed-out cart when a guest and registered customer share the email, with predictable behavior regardless of their creation order. If the intention is to prefer registered records, could the resolver inspect/filter all matches before selecting one? If guest precedence is intentional, should it apply consistently in both creation orders?

Actual behavior

The isolated stock-workflow reproduction passed these three cases:

Records and cart Selected customer
Guest created first; registered second; cart has no explicit customer Older guest
Registered created first; guest second; cart has no explicit customer Older registered
Guest created first; cart explicitly selects registered customer Registered preserved

The third case is a control for an explicitly selected customer. It does not exercise HTTP authentication and is not a claim that signed-in checkout fails.

Related report

#11827 described a logged-in variant and was closed as stale. Its community response mentions intentional guest precedence and explicitly assigning the logged-in customer. This report is specifically about signed-out email resolution and its differing outcome when record creation order changes.

System information

  • Verified September 29, 2026; Medusa/core-flows and test-utils 2.18.0.
  • Node v24.18.0, pnpm 10.11.1.
  • PostgreSQL 17.10 (Homebrew).
  • macOS 27.0 (26A428).
  • Browser: not applicable; server integration test.
  • No customer-resolution override is installed. Tests use module-created customer records (has_account false/true), not a full registration flow. Only synthetic .test email addresses and a disposable integration-runner database were used.
  • This has not been verified against the latest Medusa release.

Relevant installed package.json entries (application-only dependencies omitted):

json
{
  "dependencies": {
    "@medusajs/cli": "2.18.0",
    "@medusajs/framework": "2.18.0",
    "@medusajs/medusa": "2.18.0"
  },
  "devDependencies": {
    "@medusajs/test-utils": "2.18.0",
    "jest": "^29.7.0",
    "@swc/jest": "^0.2.36"
  }
}

Reproduction

Inline, standalone integration-test reproduction below; no public reproduction repository is available. Save it as integration-tests/http/customer-resolution.spec.ts in a Medusa 2.18.0 backend with the standard Jest/test-utils setup and a reachable test PostgreSQL. The runner creates and tears down its own test database.

bash
TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules pnpm exec jest --maxWorkers=1 --forceExit --runTestsByPath integration-tests/http/customer-resolution.spec.ts

Set the integration runner's normal local database credentials if needed. Result: 1 suite passed, 3 tests passed. The first passing assertion reproduces the guest selection; the other two are controls.

typescript
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import { Modules } from "@medusajs/framework/utils"
import { createCartWorkflow, updateCartWorkflow } from "@medusajs/medusa/core-flows"

jest.setTimeout(180000)
medusaIntegrationTestRunner({
  moduleName: `ave832-${Date.now()}`,
  testSuite: ({ getContainer }) => {
    async function runCase(name, registeredFirst, explicitCustomer) {
      const container = getContainer()
      const customers = container.resolve(Modules.CUSTOMER)
      const email = `${name}@ave832.test`
      const first = await customers.createCustomers({ email, has_account: registeredFirst })
      const second = await customers.createCustomers({ email, has_account: !registeredFirst })
      const guest = registeredFirst ? second : first
      const registered = registeredFirst ? first : second
      const matches = await customers.listCustomers({ email })
      const region = await container.resolve(Modules.REGION).createRegions({
        name: `AVE-832 ${name}`, currency_code: 'usd', countries: ['us'],
      })
      const { result: cart } = await createCartWorkflow(container).run({
        input: { region_id: region.id, currency_code: 'usd', ...(explicitCustomer ? { customer_id: registered.id } : {}) },
      })
      await updateCartWorkflow(container).run({ input: { id: cart.id, email } })
      const updated = await container.resolve(Modules.CART).retrieveCart(cart.id)
      const expected = explicitCustomer || registeredFirst ? registered : guest
      console.log('AVE832_RESULT', JSON.stringify({
        case: name, guest_id: guest.id, registered_id: registered.id,
        list_order: matches.map(c => ({ id: c.id, has_account: c.has_account })),
        cart_customer_id: updated.customer_id, cart_email: updated.email,
        selected_registered: updated.customer_id === registered.id,
      }))
      expect(matches).toHaveLength(2)
      expect(matches[0].id).toBe(first.id)
      expect(updated.customer_id).toBe(expected.id)
      expect(updated.email).toBe(email)
    }
    it('reproduces older guest winning over later registered customer on signed-out email update', async () => {
      await runCase('guest-first', false, false)
    })
    it('selects the registered customer when that record predates the guest', async () => {
      await runCase('registered-first', true, false)
    })
    it('keeps the explicitly selected registered customer despite an older guest record', async () => {
      await runCase('explicit-registered', false, true)
    })
  },
})

Relevant stock source: v2.18.0 find-or-create-customer.ts. The resolver destructures the first result and checks only that record's has_account flag.