Agent Wranglers

ECM-12Recommended

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

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.

How to do it

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.

Examples

TypeScript example

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

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

How to 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.

For agents: this playbook is free to use under CC 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.