This episode is for members only

Sign up to access "Build an E-Commerce Platform" right now.

Get started
Already a member? Sign in to continue
Playing
19. Creating a cart session

Episodes

0%
Your progress
  • Total: 8h 42m
  • Played: 0m
  • Remaining: 8h 42m
Join or sign in to track your progress
01. Introduction and demo
9m 42s
0%
02. Installing Laravel, Breeze and Livewire
4m 47s
0%
03. Creating categories
9m 4s
0%
04. Recursively displaying categories
8m 21s
0%
05. Product model and migration
2m 50s
0%
06. Showing a product
7m 50s
0%
07. Product variation setup
10m 26s
0%
08. Creating the product selector
10m 34s
0%
09. Loading child variation dropdowns
4m 28s
0%
10. Faking adding the final variation
9m 33s
0%
11. Setting up product stock
4m 34s
0%
12. Calculating variation stock levels
9m 1s
0%
13. Adding product images with MediaLibrary
8m 50s
0%
14. Creating the product gallery
7m 28s
0%
15. Providing a fallback image
2m 56s
0%
16. Adding media to product variations
3m 37s
0%
17. Creating the cart model
3m 37s
0%
18. Registering the cart service
6m 34s
0%
19. Creating a cart session
11m 4s
0%
20. Showing the cart in the navigation
9m 19s
0%
21. Caching the cart instance
3m 2s
0%
22. Adding items to the cart
14m 1s
0%
23. Creating the notification component
8m 5s
0%
24. Showing the user's cart
6m 50s
0%
25. Outputting cart items
4m 50s
0%
26. Showing variation specifics
8m 8s
0%
27. Updating item quantity
8m 2s
0%
28. Removing an item from the cart
6m 1s
0%
29. Calculating the cart summary
8m 7s
0%
30. Showing the category products page
5m 1s
0%
31. Indexing products in Meilisearch
8m 32s
0%
32. Hooking up products to categories
4m 15s
0%
33. Building the product browser
13m 32s
0%
34. Showing child categories
1m 51s
0%
35. Indexing product variations for filtering
8m
0%
36. Outputting variations for filtering
12m 52s
0%
37. Hooking up product filters with Livewire
7m 48s
0%
38. Filtering products
12m 24s
0%
39. Filtering by price
9m 50s
0%
40. Adding global navigation search
7m 50s
0%
41. Handling products that are not live
3m 22s
0%
42. Price range category fix
1m 18s
0%
43. Scaffolding the checkout page
8m 6s
0%
44. Listing shipping options
9m 3s
0%
45. Calculating the cart totals
2m 52s
0%
46. Validating the account form
9m 10s
0%
47. Validating the shipping form
5m 47s
0%
48. Saving the shipping address
8m 4s
0%
49. Selecting a saved shipping address
6m 39s
0%
50. Fix shipping address error for non authenticated users
1m 7s
0%
51. Redirecting if the cart is empty
2m 42s
0%
52. Checking for quantity changes
7m 11s
0%
53. Syncing if quantities have changed
11m 59s
0%
54. Flashing a message when quantities have changed
5m 48s
0%
55. Setting up for orders
5m 17s
0%
56. Creating an order
13m 24s
0%
57. Attaching variations to order
6m 13s
0%
58. Reducing stock after ordering
2m 56s
0%
59. Meilisearch filter query fix
1m 33s
0%
60. Showing the order confirmation page
7m 39s
0%
61. Attaching orders for registering guest users
5m 17s
0%
62. Scaffolding the orders page
5m 30s
0%
63. Filling in order variation details
5m 24s
0%
64. Returning the order status
4m 39s
0%
65. Detecting order status changes
10m 49s
0%
66. Sending the order status change email
5m 12s
0%
67. Sending an order confirmation email
2m 47s
0%
68. Handling deleted cart records
4m 44s
0%
69. Transferring the guest cart
2m 44s
0%
70. Creating a presenter for the order status
4m 31s
0%
71. Setting up Stripe
3m 43s
0%
72. Creating and updating a PaymentIntent
16m 21s
0%
73. The Stripe card form
3m 35s
0%
74. Validating before payment
5m 34s
0%
75. Submitting a payment
6m 40s
0%
76. Checking for a successful payment
5m 47s
0%
77. Handling Stripe client errors
3m 11s
0%
78. Entangling Stripe customer data
2m 18s
0%

Transcript

00:00
Now, we physically want to create a cart record in the database for a user's session. So when they start adding items to their cart, whether they're signed in or not, we can go ahead and attach these variations, which we'll be doing shortly. But how are we going to do this?
00:16
Well, we've already defined our create method, which is going to do exactly that. It's going to go ahead and create a cart instance or a cart record in the database. But how are we going to do this? When are we going to trigger it?
00:29
Well, we're going to create some middleware to do this. So let's just do a little bit of tidying up. And over on the home controller, let's get rid of the reference to creating that cart. And we can get rid of that from there and there as well.
00:40
And let's create out some middleware to do this. So let's say phpArtisan make middleware. And let's just call this cartMiddleware. This is just going to be very simple.
00:51
OK, so let's open up that cartMiddleware. And because we have that interface, we can go ahead and inject that directly into this cartMiddleware. So we can actually do that in the constructor, which we don't have by default.
01:03
So let's create a constructor out here. And we'll go ahead and say protected cart interface and cart. And we can get rid of this block here because we are automatically pulling that into a property. And now down here, we can go ahead and get access to that cart.
01:21
So let's just die dump on this cart, register the middleware, and see this in action. So if we come over to our kernel under HTTP, we're going to come down to the web section. And at the very end, we're going to go ahead and register this cartMiddleware. And if we come over to our app now, sure enough, we're dying and dumping
01:43
on this cart instance with the session manager inside of it. So what we can now do is over in our middleware, we can check if the cart instance exists within our session first of all. And then we can create it.
01:57
Let's just kind of mock this out before we go any further. So we're going to say if this cart exists, and we're going to check if it doesn't exist, then we want to go ahead and create a fresh cart instance. So we don't have the exists method.
02:14
We have the create method, but it doesn't do anything at the moment. So let's fill in the exists method first of all. So if we come over to app and cart, let's create this out and see what this would need to do. And pretty much what it would need to do is check
02:29
if we have an item in our session with a particular key. So all we need to do for this is access our session manager, use the has method, and then pass in some sort of key. And that can be anything.
02:42
It could be something like code course and cart session, whatever you wanted to call it. But we really are going to be referencing this a few times here. So we want to perhaps store this in config. So let's go and just open up our config directory.
02:58
And let's create a cart.php file. Let's go and just return an array from here. And we'll just put session in here. And let's nest this down and say key.
03:08
So I'm just going to call this cart session. Doesn't really matter what you call it as long as you pretty much keep it constant, but have it in here if it does need to change. So now what we can do is over here say config and cart session and key.
03:24
Now, at the moment, this doesn't exist. So if we come over to our middleware, we know that that's going to go ahead and die dump on our session manager because over in our middleware, we're calling create if that doesn't exist.
03:37
And over in our cart, that's going ahead and die dumping on the session. So it's here now that we actually want to create that in the database and then go ahead and put the UUID that we get generated into this key here within our session. So that sounds like a lot, but let's just take this really slowly.
03:57
So the first thing that we're going to do is go ahead and create out a cart instance by not persisting this just yet. Because if the user is signed in, we want the user ID to be automatically associated with that user.
04:10
So we're going to go ahead and create out a cart. So if we just pull in app models, that's going to rename it to models cart for me. Because of course, we're already using the cart namespace here or the cart name here. So that's absolutely fine.
04:22
We're going to say models cart and make. Let's just die dump on that instance, first of all. And let's come over and give that a refresh. There we go.
04:29
So this isn't persisting in the database just yet until we save it. But at least we are making a start on creating that out. Now, if a user is signed in, then we want to go ahead and associate with that user.
04:43
So I think we should actually allow a user to be passed into here. By default, they're null. And we want to make sure we have that as an optional null value. Then if that user does exist, we can then associate them.
04:57
So let's say if user. And in here, what we're going to do is with this cart instance, access a user relationship and associate it with that authenticated user. Now, let's just take this step by step.
05:09
This user, where is it coming in from? Well, it will come in from our cart middleware. So I'm going to say auth in here. Or in this case, I think it would be under request.
05:20
So let's say request and user. And we can kind of mock this out in a minute just to test it. And then we want to also then go ahead and create the relationship on our cart model. So the user gets passed in, potentially null.
05:36
If they are signed in, then we want to go ahead and attach it. So if we just come over to our cart model, let's create the relationship to the user inside of here. Let's create this user relationship.
05:49
And in here, we're going to go ahead and say this belongs to user and class. Great. So let's go and just test this out by dying and dumping down here on that instance. And let's see what we get.
06:07
So the user is not authenticated at the moment. We don't have, well, we do actually have the ability to sign in. So we could try this out. At the moment, there are no relations defined on here.
06:16
Let's go over to the login page. And let's log in as our user. Now, the only trouble is we've got this global middleware. So let's just cancel that off for now.
06:27
There we go. So let's go ahead and sign in with my details. And there we go. So we're signed in.
06:34
Let's go back and revert this to create the car passing in potentially a user. If we just die dump here on request and user, you'll see that we get the user model. So that's me. And if we pass this in and it's not null, remember, over here, it's going to attach
06:51
that and then die dump on that instance. Again, we're not persisting it at this point. OK. So here is our new instance ready to be persisted in the database with the user who's
07:02
currently signed in. So if we're signed in, that cart's automatically going to be associated with us. So that's it. So now what we can do, now that we've got a new instance of our cart model and we've
07:14
potentially attached a user if they are authenticated, we can now go ahead and persist that in the database. Now, let's try it out. I'm going to come over here and give this a refresh.
07:25
And yeah, sure enough, the UUID is not being filled. So there's a couple of ways that we could do this. We could either pass this into make if we wanted to, but probably the best way would be to over in the model automatically generate this UUID.
07:41
There are packages that can do this for you, but I tend to always find myself doing this directly within the model just in case we create a cart instance anywhere else. So we're going to go ahead and create out a booted method. This will be invoked once this particular model is booted.
07:56
And we're going to go ahead and attach an EloquentEventListener when a item is created or creating, really, really important. So when this is being created, before it's persisted in the database, we want to hook in and attach a UUID to this model.
08:14
So in here, we're going to get the model instance, which we could call, let's just call it model for now in case we decide to move this over to any other classes. And we're just going to say model UUID. And we're going to set that to casted string.
08:29
We're going to use the Laravel string helper under illuminate and support. And we're going to use the UUID method. So basically, when this model is booted, set up an EloquentEventListener. So when we are creating a model, e.g. over here, then attach a UUID to this.
08:48
So when we save it, we don't see this error. So let's go and give that a refresh. And there we go. So if we head over to the database now, sure enough, we now have a cart instance in here
08:59
associated to my user. And then we can start to add variations to that by hooking it up to this ID. Let's just test this out really quickly with a user who isn't signed in. So I'm going to log out and refresh the page and come over.
09:13
And there we go. Now, we have an extra couple of records here, even though that's not what we want. We're going to look at that in just a second. But there we go.
09:20
We can at least create a cart for a guest user, which is a really important part of functionality to this. So I'm going to go ahead and just delete these manually from the database. And if we just come over and just give this a refresh a few times, notice that it's creating
09:35
a new cart every time we refresh the page or navigate to another page. That is not good because we don't want all these carts. And really importantly, we'd lose the products that we'd added to the previous cart. So to get around this, of course, if we just look at the logic that we have here, over
09:51
in here, we've got this exists method. And over in our middleware, we're checking if the cart instance doesn't exist in our session. Well, at the moment, we're not putting this key in.
10:01
We're checking for it, but we're not putting it in after we've created the instance. So down here, we're going to go ahead and put the UUID into the session. So next time around, it's not created again. So again, we're going to access our session manager, and we're going to put a value in
10:18
here. And we're going to put that at the key that we defined in our config, so cart session and key. And the value that we want to put in is just the UUID.
10:27
So we're going to say instance UUID. OK, so let's go over. Make sure we've got nothing in our database. Give this a refresh.
10:35
Refresh it a few more times. Come over, and we've just got one instance for that particular session. Now, this means that when this session expires, a new cart will be created. So when that session naturally expires, that cart will be lost.
10:49
And you can create some sort of job to go ahead and clean up old database records. We might cover that at the end. But now we have a cart instance in here. So we're ready to start adding products and hooking this up to this pivot table.
78 episodes8 hrs 42 mins

Overview

Build a robust e-commerce platform with a Laravel and Livewire. Features products with unlimited and flexible variations, a product browser with filters and price range slider, global product search, guest checkout, shipping and payment implementation, order status tracking and more.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!