---
title: "Merge the guest cart into the account cart · Agent Wranglers"
url: https://agentwranglers.se/playbook/ecommerce/carts-and-checkout/cart-merge
id: ECM-12
level: Recommended
updated: 2026-10-22
tokens: 828
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Merge the guest cart into the account cart on sign-in

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Carts and checkout](https://agentwranglers.se/playbook/ecommerce/carts-and-checkout.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers.md). Updated 2026-10-22.

- ID: ECM-12
- Level: Recommended
- Chapter: [Carts and checkout](https://agentwranglers.se/playbook/ecommerce/carts-and-checkout.md)
- Rule: When a guest signs in, merge their guest cart into the account's saved cart in one transaction, then delete the guest cart. For a variant in both, keep the larger quantity, capped at the per-product limit.
- Check: A guest cart holds 2 of variant A. The account cart holds 1 of A and 1 of B. Then the guest signs in. The cart holds 2 of A and 1 of B, the guest cart is gone, and the old cart token opens nothing.
- How: A buyer who fills a cart on the phone, then signs in on the laptop, expects one cart. Adding the two quantities doubles items people already bought once in their head. Taking the larger one is the common choice. Drop lines whose variant is archived or no longer sold to the buyer's country, and tell the buyer which ones. Issue a new cart token after sign-in, so the old guest token cannot reach the account cart. Cookie settings are in [Lock down session cookies](https://agentwranglers.se/playbook/adversarial-testing/access-and-accounts/session-cookies.md).

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### TypeScript example

```ts
export function mergeCarts(guest: Map<string, number>, saved: Map<string, number>, limitOf: (id: string) => number) {
  const merged = new Map(saved);
  for (const [variantId, quantity] of guest) {
    merged.set(variantId, Math.min(Math.max(quantity, saved.get(variantId) ?? 0), limitOf(variantId)));
  }
  return merged;
}
```

### Python example

```python
def merge_carts(guest: dict[str, int], saved: dict[str, int], limit_of) -> dict[str, int]:
    merged = dict(saved)
    for variant_id, quantity in guest.items():
        merged[variant_id] = min(max(quantity, saved.get(variant_id, 0)), limit_of(variant_id))
    return merged
```

## Related rules

- SEC-22 [Lock down session cookies](https://agentwranglers.se/playbook/adversarial-testing/access-and-accounts/session-cookies.md) in Adversarial testing

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/ecommerce/carts-and-checkout/cart-merge">Agent Wranglers e-commerce and fulfilment playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Let guests check out without an account](https://agentwranglers.se/playbook/ecommerce/carts-and-checkout/guest-checkout.md)
- Next: [Recheck prices and stock when the order is placed](https://agentwranglers.se/playbook/ecommerce/carts-and-checkout/recheck-at-checkout.md)
- [Carts and checkout](https://agentwranglers.se/playbook/ecommerce/carts-and-checkout.md)
