Home / E-commerce and Business / Shopping Cart with Coupons

Shopping Cart with Coupons in JavaScript, Free with Live Demo

Free shopping cart in plain JavaScript. Add products, change quantities in a cart drawer, apply coupon codes, see free shipping progress, tax and totals, and keep the cart after reload.

Open live demoDownload HTML fileView code on GitHub
Shopping Cart with Coupons JavaScript project: a cart drawer with quantities, coupon codes, free shipping progress and saved items

Runs on: localStorage and Intl.NumberFormat. Works in all modern browsers.

What is the Shopping Cart with Coupons?

A small furniture shop with a slide-out cart. Add products, change quantities, remove items and see the subtotal, discount, shipping, tax and total update.

Try three coupon codes, one with a minimum order. A progress bar shows how much more you need for free shipping, and the cart is saved in the browser.

Good for

  • Small online shops
  • Restaurant and takeaway ordering
  • Event and ticket sales
  • Any site selling a few products

What this project does

How it works

  1. Cart is one small objectItems are stored as product id and quantity. Prices are always read from the product list, so they cannot be edited in the cart.
  2. Recalculate everythingSubtotal, discount, shipping, tax and total are worked out again on every change, in that order.
  3. Save after each changeThe cart object is saved to localStorage, so it survives a reload or coming back tomorrow.

The key JavaScript

This is the heart of the project. The full file has the rest, including the screen layout and error handling.

const cart = JSON.parse(localStorage.getItem("cart") || "{}"); // { productId: qty }
function totals() {
  const sub = Object.entries(cart).reduce((a, [id, q]) => a + products[id].price * q, 0);
  const discount = code === "SAVE10" ? sub * 0.1 : 0;
  const shipping = sub >= 500 || code === "FREESHIP" ? 0 : 25;
  const tax = (sub - discount) * 0.2;
  return { sub, discount, shipping, tax, total: sub - discount + shipping + tax };
}
localStorage.setItem("cart", JSON.stringify(cart));

How to use it

  1. Click Download HTML file above.
  2. Open the file in a code editor, like VS Code.
  3. Run it from a local server with npx serve . so the camera, microphone and AI features are allowed.
  4. Change the text and colors, then upload it to GitHub Pages, Netlify or your own site. It is one file with no build step.

Questions people ask

Is a JavaScript cart safe?

For showing the cart, yes. Always calculate the final price again on your server before taking payment.

How do I connect a payment provider?

Send the cart items to your server, create a checkout session with your provider and redirect to it.

How long is the cart kept?

Until the visitor clears their browser data. You can add a date and empty old carts after a few weeks.

More E-commerce and Business projects