sodcalculator

10 Tech How-To

WhatsApp Cart for Next.js Static Store

Guide 10-50173 · Updated · Step by step

This guide shows you how to add a cart to a static Next.js store that sends orders via WhatsApp. You’ll build a lightweight cart context, a cart drawer, and a checkout button that opens WhatsApp with a pre-filled order message. No backend or payment gateway is needed—just your existing product data and a WhatsApp number.

What You’ll Need

  • A Next.js project with static export (using next export or the output: 'export' option).
  • Your product data available as static JSON or in the page components.
  • A WhatsApp number (use your own or a business number) in international format, e.g., 15551234567.
  • Basic familiarity with React hooks and Next.js pages.

Step-by-Step

  1. Create a Cart Context
    Create context/CartContext.js. Use React’s createContext and useReducer to manage cart items. The reducer should handle ADD_ITEM, REMOVE_ITEM, UPDATE_QTY, and CLEAR. Store items as objects with id, name, price, qty, and optional image.
  2. Provide the Cart to Your App
    In _app.js, wrap your app with CartProvider. Export a useCart hook that returns the cart state and dispatch functions.
  3. Add “Add to Cart” Buttons
    On your product pages, import useCart and call dispatch({ type: 'ADD_ITEM', payload: product }). The button should be disabled if the item is already in the cart, or you can increment quantity.
  4. Create a Cart Drawer Component
    Build a component that shows the list of cart items, each with a quantity input and a remove button. Use the context to update quantities. Style it as a side drawer that opens on click.
  5. Implement the WhatsApp Checkout Button
    In the cart drawer, add a button that triggers the checkout. On click, build a message string: Hello! I'd like to order: then list each item with name, qty, and price, then total. Encode it using encodeURIComponent and open https://wa.me/<number>?text=<encodedMessage> using window.open or a link.
  6. Handle Cart Persistence (Optional)
    If you want the cart to survive page reloads, use localStorage. In the reducer’s initial state, read from localStorage, and in a useEffect in the provider, write the cart to localStorage whenever it changes.
  7. Test the Flow
    Run npm run build and npm run export to generate the static site. Serve it locally (e.g., with npx serve out) and test adding items, updating quantities, and clicking the WhatsApp button. Ensure the message appears correctly in a new tab.

Common Mistakes

  • Forgetting to encode the message – Spaces and special characters break the URL. Always use encodeURIComponent.
  • Using a number with spaces or dashes – WhatsApp requires the number in international format without any non-digit characters.
  • Not handling quantity updates – If you only allow adding, the cart becomes unusable. Implement quantity controls.
  • Storing the cart in context but not persisting – On a static site, page navigation won’t lose context, but a full reload will. Use localStorage if you need persistence.
  • Assuming the WhatsApp URL works on all devices – On desktop, it opens the web version; on mobile, it opens the app. Test both.

Tips

  • Use a useMemo to compute the total price so it updates efficiently.
  • Add a badge on the cart icon showing the total item count.
  • Consider a maxQty per item to prevent unrealistic orders.
  • Make the WhatsApp message include the product URLs for easy reference.
  • If you have many products, consider using a product ID to look up details in a central data file.

Troubleshooting

The WhatsApp button doesn’t open anything. Check the browser console for errors. Ensure the number is correct and the URL is formed properly. Test with a simple hardcoded message first.

The message is cut off or garbled. You likely forgot to encode the message. Use encodeURIComponent on the entire message string.

Cart resets on page reload. Implement localStorage persistence. In the provider, read the initial state from localStorage and write back on changes.

Cart items don’t update correctly. Check that your reducer is pure and returns a new state object. Use map or filter to create new arrays.

The cart drawer doesn’t close on mobile. Add a click-outside or a close button. Ensure z-index is high enough.

Frequently Asked Questions

Can I use this with a dynamic store? Yes, but the guide assumes static products. For dynamic, you’d fetch products client-side and adapt the cart accordingly.

How do I add shipping costs? You can include a shipping fee as a line item in the message, or ask the user to confirm shipping in WhatsApp. For a static site, it’s easier to calculate manually.

Can I use a WhatsApp Business API instead? Yes, but that requires a backend and approval. The wa.me link is the simplest approach for static sites.

Is it possible to send images in the WhatsApp message? Not via a simple URL. You can include product links, but images require a different API.

How do I change the WhatsApp number? Store it in a config file or environment variable. For static export, you can define it in a constant and import it.

Can I use this with a headless CMS? Yes, as long as you fetch products at build time and pass them to your pages as static data.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →