Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 28 ยท Projects

Project: Shopping Cart and Product Quick View

Build a shopping cart with HTMX and Alpine.js: add to cart with out-of-band badge updates, quantity steppers, server-computed totals and a quick-view modal.

A shopping cart is a good test of the HTMX and Alpine split, because one click changes several places at once: the badge in the header, the list of items, the totals. It also has a trap that JavaScript-heavy apps fall into: it is tempting to compute prices in the browser. Here the server keeps the cart and computes every price, discount and total, so what you see is always what the server will charge. HTMX moves the new HTML into the page. Alpine.js opens and closes the cart drawer and the quick-view modal.

The key technique is the out-of-band swap, so be comfortable with it first (Targets and Swap Strategies). Modal ideas come from Using HTMX and Alpine.js Together.

One click, three updates

Badge The item count in the header.
Cart list Rows with quantity steppers.
Totals Subtotal, shipping and total.

All three arrive in a single response.

1 / 2

The design: swap nothing, update everything

When the shopper presses Add to cart, nothing near the button needs to change. So the button uses hx-swap="none": HTMX sends the request and ignores the main content of the response. What it does still process are the out-of-band elements, those with hx-swap-oob="true". The server answers with:

  • <span id="badge" hx-swap-oob="true">3</span> to update the header, and
  • <div id="cart" hx-swap-oob="true">...</div> to replace the cart contents.
sequenceDiagram
    participant U as Shopper
    participant H as HTMX
    participant S as Server
    U->>H: Clicks Add to cart
    H->>S: POST /cart/add (id=2)
    S->>S: Update cart, compute totals
    S-->>H: badge span + cart div (both out of band)
    H-->>U: Badge and cart both update

Because the buttons use hx-swap="none", the whole response is out-of-band content.

Step 1: add to cart and the badge

This first version shows only the product list and the badge. Watch the yellow flash on the badge: HTMX adds the class htmx-added to new content for a moment, and a CSS transition turns that into an animation.

Add to cart with an out-of-band badge

The response has no main content at all, only the out-of-band badge, yet it works: with hx-swap="none" HTMX still processes out-of-band elements.

Step 2: cart drawer, steppers, totals and quick view

The complete shop adds several ideas.

One helper for the update. The server function cartUpdate() returns the badge and the cart panel, both out of band. Every cart route (add, plus, minus) returns it, so every button updates the same places and there is only one template to maintain.

Steppers. Each cart row has - and + buttons that POST /cart/qty with the product id and a delta. The server changes the quantity, removes the line when it reaches zero, and recomputes the totals. Nothing in the browser does arithmetic.

Totals on the server. Prices are stored in cents, so there are no floating-point rounding surprises, and a money() helper formats them. Shipping is free from 50.00 and 4.00 below that. If the rule changes tomorrow, only the server changes.

Alpine for the views. The wrapper holds { cartOpen: false, quick: false }. The cart button toggles the drawer. A quick-view button sets quick = true and loads the product details with hx-get into the modal body, so the dialog opens instantly while the content arrives a moment later. The fragment itself contains an "Add to cart" button that also closes the modal and opens the drawer, because fragments inside the wrapper can use its scope.

Shopping cart with quick view

Check the behavior step by step: add a Notebook twice and a Backpack. The badge shows 3, and the drawer lists both lines with the totals. The subtotal is $48.00 plus $4.00 shipping. Add a Pen set and the subtotal passes $50, so shipping shows "free". Press - on the Pen set and shipping returns. Open a quick view, press Add to cart inside it: the modal closes, the drawer opens and the cart updates.

Things worth noticing

  • The initial cart load. The #cart placeholder uses hx-trigger="load" with hx-swap="none". The response replaces it out of band. The replacement has no hx-trigger, so it does not load again.
  • Buttons inside swapped HTML. The cart's steppers are created by the server, yet they work immediately, because HTMX processes every hx- attribute in new content.
  • Disabled states from the server. The + button gets disabled at quantity 9. The rule lives in one place: the same MAX_QTY constant the server enforces. Always enforce limits on the server, too: a hand-made request with delta=50 must not break your rules.
  • Accessibility. The modal has role="dialog" and aria-modal, Escape closes it, and the cart button exposes aria-expanded. A production version would also move focus into the dialog and return it on close.

Never trust prices that come from the browser. Notice that no request in this project sends a price: only a product id and a quantity change. The server looks up the real price every time.

Ideas to extend it

  1. Add a coupon code field (hx-post="/cart/coupon") and show the discount as another line in cartPanel().
  2. Show an inline error with an htmx:responseError listener when the server rejects an item that went out of stock.
  3. Persist the cart in a session or database. The HTML does not change: only the cart object on the server does.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why do the Add buttons use hx-swap="none"?

    hx-swap="none" skips the main swap, but HTMX still processes hx-swap-oob elements, which update the badge and the cart.
  2. 2. Where should cart totals be computed?

    Computing money in the browser risks showing numbers that differ from what the server charges. The server is the single source of truth.
  3. 3. What makes the quick-view modal open instantly while its content loads afterwards?

    Alpine handles visibility with no waiting, and HTMX loads the fragment into the modal body. The two jobs run side by side.
  4. 4. The cart placeholder uses hx-trigger="load". Why does the cart not reload in a loop after being replaced?

    The out-of-band replacement is a new element without hx-trigger="load", so nothing triggers another request.

Summary

  • The server owns the cart and computes every price and total; requests carry only ids and quantities.
  • hx-swap="none" plus hx-swap-oob lets one response update the badge, the cart list and the totals.
  • A single cartUpdate() helper keeps every cart route consistent.
  • Alpine opens and closes the drawer and the modal; HTMX fills the modal from the server.
  • Elements created by the server (steppers, the modal's Add button) work at once because HTMX and Alpine process new content.
  • Validate quantities and limits on the server as well.

Next project: Dashboard with Polling, Tabs and Toasts.