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
53. Syncing if quantities have changed

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
Once we detect that there is quantity no longer available for any product, we're going to go ahead and trigger a sync on our cart which is going to map up the maximum
00:08
available quantity for all of the products if it's out of stock or if the stock has been reduced. So what we're going to do here is go over to our cart model and we're going to create our new method to do this. We're going to take this really slowly because we're going to be doing a lot of mapping through collections to build up an array that we can pass through to
00:28
the sync method on the variations relationship. So let's go ahead and just call this sync available quantities. Once again we're going to make this fairly verbose just so we can kind of see what we're doing and you might come up with some magic to reduce this. So first of all let's look at the syntax that we'll need to actually update this in the database. Now at the moment
00:52
if we come over we've got we come over to our cart variation we've got one product in here with 10. I'm actually going to get rid of all of these others just so we can focus on this one here and this being updated and let's also go ahead and add another product to our cart. So let's go and find that other product and let's go ahead and add. So yeah there's no stock on that so let's
01:17
add some stock first of all. So to keep things sort of simple we'll add 10 stock on rainbow 12. If we look at the parent id there that's that one. There we go. So let's go ahead and add this in. There we go and then let's reduce the stock for that one as well. So we should see both of these change over. So let's choose this one and let's reduce that by two. So we should end up with
01:46
five and eight for these respectively over in our cart. So this one in fact let's just update the quantity over here in our cart and that's not going to work because we are dying and dumping. We'll get there. So we can actually do this manually in the database. Let's update this one to 10 as well. So basically by the end of syncing this because this one's been reduced by
02:10
two in stock and this one's been reduced by five this one should show five and this one should show eight. Hopefully that makes sense. Okay so what we want to do is eventually get to the point where we can grab our cart instance, grab our variations relationship out here and we can call sync. Now to sync something you need to pass an id in and then the pivot information. So let's say
02:34
we had a product variation one and we wanted to update the quantity to say 10. What we're going to have to do is pass another array in here. So we're going to have to array it like this. One is going to be the key that's the variation id and then in here is going to be the pivot information. So let's just say for the variation that we have in there we're reducing that to
02:56
eight for the rainbow size 12 and for the other one we're reducing this to five. This is the kind of array that we want to add a build up. So this is what we need to bear in mind. I'm going to cut this out. I'm going to paste this down here just comment it out so we can reference the structure of this when we build this up here. So let's call this array synced quantities that we can use to
03:20
pass directly into here and the way that we want to do this is again iterate through the variations much like we did here or map through the variations to build this array up based on the available quantity that we have. So what we're going to do is just say this instance variations the collection this time. We're going to map through it with the keys because remember we
03:44
need them keys intact and let's go ahead and create out our closure in here. Of course we're going to get our variation through in here. So now what we need to do is figure out the quantity that we want to return in this array here. We know the variation id so we can return that at the bottom of here but we need to figure out the quantity that needs to be synced too. So let's create a
04:07
variable for this just to make this a little bit clearer. We're going to go ahead and say variation pivot quantity and we're going to check if that's greater than the variation stocks count sum which is what we did in this check here. So effectively we're saying has this stock been reduced and has it been reduced based on this quantity here. Now if that's the case so we'll
04:36
use a ternary here what we want to do is go down to the maximum stock count that we have for that product. Really important. Otherwise what we want to do is go and just go for the quantity that the user has chosen. So let's just return an array from here and just test this out. So let's go ahead and say variation id so we're starting to build up this array here. This is the variation id so
05:05
remember we're mapping with keys and then in here we want an array with the pivot information which is going to be the quantity that we have just calculated based on this here. So there are of course shorter ways to do this but we want to be a little bit more verbose in this just so we can kind of read the code a bit better. Okay so what we're going to do here is
05:24
die dump on the sync quantities so we can see it before we test this out. So over in cart index controller let's go over to here and say cart sync available quantities and let's see what we get. Let's give that a refresh. We get a collection here with three and eight which of course are the variation ids. This one's been reduced to five and this one has been reduced to eight which is great.
05:49
Now we also need to bear in mind that if a particular product has been reduced by so much quantity that it has to come out of our cart that it has to come out of our cart because the quantity has been reduced to zero. We want to kind of get rid of that from this array. So let's just test this out in the database and we'll go over to our stocks. Let's reduce this one by 10. So let's say
06:15
someone buys 10. We've got 10 in our cart. What does that leave us with? Well of course it leaves us with nothing in our cart and zero is not a valid quantity for an item in our cart. So we need to kind of reject this from this list and we can sure enough do that with the reject method on a Laravel collection. So this will just kind of filter this out or reject this from here. So let's go ahead
06:40
and reject this based on this condition and let's go ahead and say synced quantity and we're going to access that synced quantity and we're going to check if the quantity is equal to zero. So this is the condition for rejecting that item from this collection. Give it a refresh. We only end up with this one in here. Now we're not going to do this for now. We're going to test this out by reducing
07:05
this by five first of all so we can see that over in our cart variation this does reduce and then we'll check that this is completely rejected from our cart. So now that we've got our synced quantities with dying and dumping on them we need these to be cast to an array. So let's cast these to an array because of course we need this kind of an array to pass through to this sync method
07:28
and let's go ahead and now pass this in. So synced quantities. Now we will need to do something down here. We'll look at that in just a second but let's just make sure that giving this a refresh updates this for us. So let's give that a refresh and there we go five and eight. So both of them have been updated. Now what you'll notice is on this page now that we've refreshed that the
07:48
exception was thrown we sync to the product. What we really need to do is clear the cache instance or the instance cache from our class so this can be re-plucked out and we can see the correct quantities. So what we're going to do is do something like this clear instance cache which does slow things down a little bit but not by that much. So let's go and implement this method maybe
08:15
just down here let's make this protected and to clear this cache down here let's find it this one we just need to set the instance to null so it can be re-read. So let's say clear instance cache and let's set this instance to null. Now let's move that down because it doesn't really make sense there let's just put this just above the instance method. Okay so let's try this
08:49
again let's manually set the quantity in our cart to 10 and 10 save this out come over here give it a refresh and you can see there we go. So in between that syncing our database we've cleared the instance cache for looking up our cart and the quantities are now showing properly on here otherwise the user would have to refresh the page to see these quantities which isn't great.
09:12
Okay so let's finish off by looking at what happens when we have a quantity of zero in here. Now let's go over and again switch these to 10 and 10 just to start with something really simple and if we come over to our stocks here and reduce this one by 10 like we demonstrated before that should reject that from that array. So let's go over to our sync method
09:37
and let's once again just die dump on synced quantities. So we end up with an array with just the quantity for this variation in here because that's not being reduced to zero we didn't reject it from this array using this reject method. Now what's going to happen is when we pass this through and the variation is missing because we're using the sync method
10:00
and we're not using sync without detaching it's going to go ahead and remove this from the cart effectively meaning that if it's a zero quantity it gets rid of it. So what we've done is reduce the quantity down to zero rejected it and then we're just syncing it and Laravel will take care of the rest or Eloquent will take care of the rest. So let's go over give
10:22
this a refresh and there we go so we've got our Nike Air Force 2 in there but if we head over of course because we reduced that variation and we rejected that it's only gone ahead and included this one which has a positive quantity. Now you could make this a little bit more robust by saying less than or equal to one or zero sorry but let's go ahead and just compare this to zero
10:45
because it's not that likely that this is going to go to minus one or minus two and that's pretty much it checking and syncing the products in our cart. Now we want to include this check over here on other pages as well so let's go ahead and take this and add it to our checkout index controller as well there's probably a bit of a nicer way to do this we could abstract this
11:10
into a method on its own in our cart but let's just leave this like it is for now as long as it's in there that's the main thing and then when we go through and actually process a payment we'll include this within our Livewire component. Okay let's just double check this by clicking on checkout and yeah undefined variable cart let's go and say cart interface and cart and there we go so
11:36
that's going to remove that from there as well we can even test it here we head over to variations or cart variations and bump that to 10 and come over there we go we practically just bumped it down to 8 we didn't see anything happen here but that is now working on our checkout index as well really important because of course we see this before we check out.
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!