1. 21
    Create a Checkout Page in Next.js that Displays Cart Contents
    2m 3s

Create a Checkout Page in Next.js that Displays Cart Contents

Shadid Haque
InstructorShadid Haque
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

A cart isn't very useful if we don't have a check out page to actually purchase products at. While we aren't setting up payment process just yet, this lesson will set us up by looping over products in our cart and displaying them in a list.

Shadid Haque: [0:00] When we click on the cart, it should take us to a checkout page. Let's go ahead and build this checkout page. Let's go to our cart.tsx file. Here, we're going to import useContext from React and Context from the context file.

[0:20] Inside the Cart component itself, let's destructure state and dispatch from useContext. Next, we destructure cart from state. Next, we're going to calculate the total amount for the cart. We are going to loop over every element of the cart. For each item in the cart, it's going to be price*qty. Then, we add all of them up to get our total amount.

[0:54] Next, we are going to loop over every item in the cart and render them on the page. We're going to create a container div, give it flex and some padding. Next, we add a ul tag with some basic margin and a divider.

[1:14] We're going to loop over each entry of the cart object and return a list element. I already have some pre-written code for the list. I'm just going to paste this HTML here.

[1:38] Let's go back and add couple more items in the cart again. Now, when we go back to our cart, we can see all the items popping up. Finally, at the bottom, I'm just going to add a Checkout button. You can find all this pre-written HTML with the lesson notes.

egghead
egghead
~ 4 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today