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 exportor theoutput: '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
- Create a Cart Context
Createcontext/CartContext.js. Use React’screateContextanduseReducerto manage cart items. The reducer should handleADD_ITEM,REMOVE_ITEM,UPDATE_QTY, andCLEAR. Store items as objects withid,name,price,qty, and optionalimage. - Provide the Cart to Your App
In_app.js, wrap your app withCartProvider. Export auseCarthook that returns the cart state and dispatch functions. - Add “Add to Cart” Buttons
On your product pages, importuseCartand calldispatch({ type: 'ADD_ITEM', payload: product }). The button should be disabled if the item is already in the cart, or you can increment quantity. - 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. - 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 usingencodeURIComponentand openhttps://wa.me/<number>?text=<encodedMessage>usingwindow.openor a link. - Handle Cart Persistence (Optional)
If you want the cart to survive page reloads, uselocalStorage. In the reducer’s initial state, read from localStorage, and in auseEffectin the provider, write the cart to localStorage whenever it changes. - Test the Flow
Runnpm run buildandnpm run exportto generate the static site. Serve it locally (e.g., withnpx 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
useMemoto compute the total price so it updates efficiently. - Add a badge on the cart icon showing the total item count.
- Consider a
maxQtyper 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
- Fix msmpeg2vdec.dll Errors in Windows
- How to Update Sonos App and Speakers
- Unlock iPad Without Apple ID: Is It Possible?
- iOS 18 WhatsApp Notifications Not Working? Fix
- Fix Windows Cannot Locate Suitable Printer Driver
- How to Group Text and Pictures in Word