# The storefront script

> Every call window.Cloud.commerce gives a served Studio page: product and price reads, the shared cart, pickup, coupon, tip and checkout, with a pickup order worked end to end.

## The storefront script

Every page The Cloud serves for a Studio app is given `window.Cloud.commerce`, including on a custom domain. It reads the shop's live products and prices, keeps one shared cart, draws the pickup, coupon and tip fields, and starts payment. You never need the shop's commerce address: the script already knows which shop it belongs to.

Every read answers with a state first. Check it before using anything else, because a product that was unpublished answers `{ state: "unavailable" }` rather than an error. Prices are in minor units, so 450 is 4.50.

## Calls on Cloud.commerce

| Call | What it does | What it answers |
| --- | --- | --- |
| `product(productId, { earliestPickup, location })` | Reads one live product. Pass earliestPickup: true, and optionally a location id, to also learn the first pickup time. | A promise of { state: "product", product, earliestPickup } or { state: "unavailable" }. The product carries its name, price in minor units, currency, images and sizes. |
| `configuration(productId, { variant })` | Reads the sizes, option groups and prices a product is sold with. Pass a variant id to read that size's own options. | A promise of { state: "configuration", configuration, variants } or { state: "unavailable" }. Each variant carries its id, name and priceMinor. |
| `products(playlistId)` | Lists every live product, or the products of one playlist when you pass its id. | A promise of { state: "table", products, fulfillment } or { state: "unavailable" }. |
| `cartToken()` | The one cart token every call on the page shares. Pass it as anonId when a call asks for one. | The cart token string for this site. |
| `pickup(host, anonId, { onChange, onNote, onOptions })` | Draws the pickup picker into host. onChange receives the choice to send as fulfillment: { method: "pickup", locationId, pickupAt } for a pickup time, { method: "pickup", locationId, asap: true } for as soon as possible, or null. | A promise of true when a picker was drawn or nothing needs picking, false when checkout is not available. |
| `coupon(host, anonId, { onChange, onLine })` | Draws the coupon field. onChange receives the code, and the email when the coupon needs one, to send as couponCode and couponEmail. | A promise of true when the shop takes coupons and the field was drawn, false otherwise. |
| `tip(host, anonId, { onChange, onLine })` | Draws the tip row. The tip is saved on the cart when onChange fires, so checkout needs nothing more for it. | A promise of true when the shop takes tips and the row was drawn, false otherwise. |
| `checkout(request, { onNote, onClose })` | Starts payment for the cart. request takes anonId, fulfillment, couponCode, couponEmail, tipMinor, dietary and returnTo. The script picks the payment page the shop is set up for. | A promise of "hosted", "custom", "embedded" or "unavailable". Only "unavailable" needs handling: onNote has already said why. |
| `cart` | The shared cart every surface on the page reads. | The cart calls listed below. |

## Calls on Cloud.commerce.cart

| Call | What it does | What it answers |
| --- | --- | --- |
| `cart.token()` | The same token as cartToken(). | The cart token string. |
| `cart.get()` | The cart as last read, without a request. | { status, cart, problem, count }, with status loading, ok, empty or unavailable. |
| `cart.count()` | What a bag badge shows. | The number of items in the cart. |
| `cart.read()` | Reads the cart from the shop again. | A promise of the cart, { state: "ok", lines, subtotalMinor, ... } or { state: "empty" }. |
| `cart.add({ productId, quantity, variantId, selections, notes })` | Adds a product. Only productId is required; the shop's default options are applied when no selections are sent. | A promise of the cart after the add. |
| `cart.remove(lineKey)` | Removes one line. | A promise of the cart after the removal. |
| `cart.setQuantity(lineKey, quantity)` | Changes one line's quantity. | A promise of the cart after the change. |
| `cart.checkout(request, handlers)` | checkout() with this cart's token filled in, run after any add still in flight. | The same answer as checkout(). |
| `cart.mount(host, handlers)` | Draws the full cart page into host. | Nothing. |
| `cart.open()` | Opens the cart drawer. | Nothing. |
| `cart.close()` | Closes the cart drawer and popover. | Nothing. |

## Worked example: a pickup order from your own page

This page shows one product's live price, adds it to the bag, lets the visitor choose a cafe and a pickup time, and carries both to checkout. The pickup picker's choice is exactly what checkout takes as `fulfillment`, so nothing needs converting. Replace PRODUCT_ID with an id from `listProducts`.

```html
<div id="menu"></div>
<div id="pickup"></div>
<p id="note"></p>
<button id="pay" disabled>Pay</button>

<script>
  const commerce = window.Cloud.commerce;
  let fulfillment = null;

  // 1. Show a live price from the shop.
  commerce.product("PRODUCT_ID").then((res) => {
    if (res.state !== "product") return;
    const price = (res.product.priceMinor / 100).toFixed(2);
    document.getElementById("menu").textContent = res.product.name + " " + price;
  });

  // 2. Put it in the bag, then let the visitor pick a cafe and a time.
  commerce.cart.add({ productId: "PRODUCT_ID", quantity: 1 }).then(() =>
    commerce.pickup(document.getElementById("pickup"), commerce.cartToken(), {
      onChange: (choice) => {
        fulfillment = choice;
        document.getElementById("pay").disabled = !choice;
      },
      onNote: (words) => {
        document.getElementById("note").textContent = words;
      },
    })
  );

  // 3. Hand the location and the time to checkout.
  document.getElementById("pay").addEventListener("click", () => {
    commerce.cart.checkout({ fulfillment, returnTo: location.href }, {
      onNote: (words) => {
        document.getElementById("note").textContent = words;
      },
    });
  });
</script>
```

**If you draw your own time picker.** Send `{ method: "pickup", locationId, pickupAt }` yourself, with pickupAt in milliseconds. The shop checks the time against its own hours and lead time at checkout and refuses one it does not offer, so read the offered times from the `onOptions` handler of `pickup()` rather than guessing them.

## Prove it with a test order

Ask your AI to call `proveCheckoutWithTestOrder` with your app and one live product, and a pickup location and time if the product needs one. It places a test order with the same prices, stock check and pickup check a buyer's cart goes through, pays it with a test card on The Cloud's own test account rather than on this script's payment page, and answers with the order number and a link to it in your shop's orders. No card is charged, no money moves, and any stock it held goes back on the shelf. Then `getGoLiveReadiness` reads checkout as proven. See [Verify it worked](/docs/build-on-the-cloud#verify-it-worked) for the other checks.
