# Shopify draft orders: discounts that vanish, stale checkouts, and a cart that has to match

> 💡
> **Summary.** A "lost discount" on a HubSpot draft order wasn't a bug in our cart-link flow: the discount was a one-time code that had expired a week earlier. Shopify keeps a draft's old totals until something changes the draft, then silently drops expired codes on recalculation. Editing a draft while a customer has its checkout open never leads to a wrong charge, but it does show a confusing "free order" screen. The draft cart view now shows discounts using the same markup as the real cart.
> 
## Context

Container One's sales reps build orders in HubSpot. A partner agency's integration (J2) pushes them into Shopify as **draft orders**. Our hub gives each draft a **cart link**: the customer opens the storefront cart page, can add Pay on Delivery (3%) or Expedited Shipping, and is sent to the draft's own checkout, so paying completes the draft and no duplicate order is created.

When the customer ticks an add-on and presses Checkout, the hub rewrites the draft's line items (`draftOrderUpdate`). That rewrite turned out to be central to every finding below.

## Finding 1: the "lost" second discount was an expired code

J2 reported a draft with two discounts (a code for −$200 and "Same Trailer Discount …" for −$100) where checkout only applied one.

| Theory | How it was tested | Verdict |
| --- | --- | --- |
| Hub drops the draft's custom discount when it rewrites lines | Test draft with a code + a custom discount; add-ons toggled 5 times through the hub | Rejected: both kept every time, checkout charged the right total |
| Theme or checkout extension edits the draft | Code search across theme, app extensions, PHP app | Rejected: theme only calls the hub; PHP only creates new drafts; extensions never write drafts |
| Something else rewrote the draft | Hub's `draft_orders/update` webhook log used as a history (Shopify keeps none) | Partly: later rewrites came from a rep editing in HubSpot |
| The second discount was a discount code that had expired | Searched discounts; dry-run `draftOrderCalculate` with both codes | Confirmed: one-time code created with the draft, ended a week before the customer clicked; Shopify silently dropped it |
> ⚠️
> **Shopify behaviour worth knowing.** A draft order keeps its stored totals, including discounts from codes that have since expired, until something recalculates it. Any update (ours, the integration's, or a manual edit in admin) re-validates the codes, and an expired code is dropped without an error or warning.
> 
**Recommendations**

- Per-order codes should have no end date; their single-use limit already protects them.
- Proposed hub safeguard: before saving add-ons, dry-run the update and stop with "please contact your rep" if any discount would disappear, rather than charging the customer more.
## Finding 2: reps editing a draft after the link is sent

Reps need to keep editing drafts from HubSpot at any time. What we established:

- The cart link reads the draft live on every page load, so the same link always shows the current draft. Re-registering the link is optional and returns the same URL.
- If the integration's re-sync drops the Pay on Delivery / Expedited lines, the customer's choices are lost silently. The integration should keep add-on lines (matched by variant ID).
- Open decision: let the hub recalculate the Pay on Delivery amount whenever the integration re-sends a draft, so the 3% is always based on the current lines.
## Finding 3: paying from a stale checkout

Test on staging: open a draft's checkout ($4,600), then change the draft underneath it ($2,224, as a rep edit would).

| Step | What the open checkout showed |
| --- | --- |
| Before entering details | Old total, $4,600; no automatic refresh |
| After entering the address | Subtotal $0.00, "Shipping not available", "Your order is free. No payment is required." |
| Pressing "Complete order" | No order created; checkout emptied ("Your cart is empty") |
| Reopening the link | Correct draft, $2,224, card form |
> ✅
> No wrong charge is possible: Shopify never charged the old total or $0. The cost is a confusing screen; the fix is to reopen the link, so reps should tell customers to do that after editing a sent draft.
> 
## Finding 4: making the draft view match the real cart

The draft view showed a discounted Total but no Discount row, so the numbers didn't add up.

- **Hub:** returns `order_discounts` — order-level codes (`platformDiscounts` with `presentationLevel = order_level`) plus the draft's custom discount. Verified on staging that the two don't overlap, and on production that the API version accepts the fields and the literal is `order_level`.
- **Theme:** the savings line and code tag moved into shared snippets used by both the real cart and the draft view, so they can't drift. The draft view adds the Discount row, "You're saving" line and read-only code tags.
- **Deliberate differences:** no code input and no remove (✕) on draft tags, since customers can't change a rep's discounts.
- **Noticed, not changed:** the real cart shows an already-discounted Subtotal (Subtotal $2,250, Discount −$200, Total $2,250); the draft view's math adds up.
## Techniques that helped

- **Webhook log as history.** Shopify keeps no edit history for drafts; the hub's stored `draft_orders/update` payloads reconstructed who changed what and when.
- `**draftOrderCalculate**`** as a dry run.** Answers "what would Shopify do with these lines and codes?" without creating or changing anything.
- **Visual check before the backend merge.** Re-rendering the page with a patched `fetch` injected the new hub field, so the theme could be compared side by side with the real cart before the hub change shipped.
## Open items

- [ ] Integration: keep add-on lines on re-sync; no end dates on per-order codes
- [ ] Decide: hub recalculates Pay on Delivery when a draft is re-sent
- [ ] Build: hub safeguard against silently dropped discounts
- [ ] Fix: checkout block shows Pay on Delivery as 3% after discounts while the line charges 3% before
- [ ] Phone-width check of the draft cart view


_Research conducted with Claude Code._