> ## Documentation Index
> Fetch the complete documentation index at: https://docs.stackfront.digital/llms.txt
> Use this file to discover all available pages before exploring further.

# Cart Overview

# Cart Overview

The SDK manages the shopping cart lifecycle through the Shopify Storefront GraphQL API. The cart ID is persisted in AsyncStorage so it survives app restarts.

## How It Works

1. **First add** — calling `addLines` when no cart exists automatically creates one
2. **Subsequent operations** — the stored cart ID is used for all mutations
3. **State sync** — each mutation returns the updated cart and updates local state
4. **Clear** — removes the cart ID from storage; next add creates a fresh cart

## Cart Service vs Hook

|                               | Service                | Hook                       |
| ----------------------------- | ---------------------- | -------------------------- |
| Access                        | `useStackfront().cart` | `useCart()`                |
| State                         | None (imperative)      | `cart`, `loading`, `error` |
| Auto-fetch                    | No                     | Yes (fetches on `ready`)   |
| Mutations return updated cart | Yes                    | Yes + updates state        |

## Cart Shape

The cart response follows Shopify's Storefront API cart type:

```ts theme={null}
{
  id: string;
  checkoutUrl: string;
  lines: { edges: [{ node: { id, quantity, merchandise } }] };
  cost: { subtotalAmount, totalAmount, totalTaxAmount };
  buyerIdentity: { countryCode, customer, email, phone };
}
```

See [Types Reference](/docs/types-reference) for the full schema.
