- 00:00:00 like a back everyone it's really
- 00:00:03 summertime over here it's extremely hot
- 00:00:06 but it won't stop me from finishing this
- 00:00:08 shopping cart serious here so where are
- 00:00:11 we currently at we have our shopping
- 00:00:14 cart here or our product page you can
- 00:00:17 add items to the cart and we can then
- 00:00:19 successfully check out while stripe so
- 00:00:23 that's all great but there's one thing
- 00:00:25 that is missing we get this action drop
- 00:00:28 down here but currently it's not doing
- 00:00:29 anything I want to be able to either
- 00:00:32 reduce it by 1 which of course means I
- 00:00:35 want to take just one item off that item
- 00:00:38 group here off the card or I want to
- 00:00:41 reduce all words basically chest remove
- 00:00:44 this whole item leave all other items in
- 00:00:46 the shopping cart if there are any but
- 00:00:48 remove this one and yes of course you
- 00:00:50 could add plenty of our functionalities
- 00:00:53 to this but the core of this series it
- 00:00:56 is called shopping cart after all right
- 00:00:58 we'll be finished after that you'll know
- 00:01:00 how to create a card how to manage it
- 00:01:03 how to check it out and how to store it
- 00:01:06 in the database of checkout so I think
- 00:01:09 that really is a lot but before going
- 00:01:12 into all this
- 00:01:13 finishing talking stuff well let me go
- 00:01:16 back and implement your functionality I
- 00:01:18 was just talking about manage this card
- 00:01:21 back in the project add like I just
- 00:01:25 bring that over here I'll start in my
- 00:01:27 cart PHP file in my card model currently
- 00:01:31 we got a function to add items but since
- 00:01:33 I want to be able to even reduce item by
- 00:01:36 one or remove it entirely
- 00:01:38 I also need functions for well these
- 00:01:41 actions I'll start with the reviews
- 00:01:44 function so I'll go in here and create a
- 00:01:48 new function which I'll call reduce by
- 00:01:51 one something like that
- 00:01:53 and reduce by one we'll take an idea as
- 00:01:56 an input because I need to know the idea
- 00:01:59 of the item which I want to reduce right
- 00:02:00 think that makes sense so in here let's
- 00:02:04 think about what should happen if I
- 00:02:06 reduce an item by one well it's kind of
- 00:02:10 the opposite of the add item here I want
- 00:02:13 to reduce the quantity of that item I
- 00:02:16 want you reduce the aggregated
- 00:02:19 aggregated price of that item group and
- 00:02:22 I also want to remove D total quantity
- 00:02:25 or a reduced the total quantity and
- 00:02:27 reduce the total price so really just
- 00:02:30 reverse that of course there is one
- 00:02:34 catch I also need to make sure that if I
- 00:02:37 well reduced the item below zero so
- 00:02:41 basically if I remove the item well that
- 00:02:44 I actually do remove it and that I don't
- 00:02:46 get negative quantities or prices so
- 00:02:50 let's start with the easy part first
- 00:02:52 I'll select my items so this items just
- 00:02:57 refers to the items in my cart and then
- 00:03:00 I can select item by ID
- 00:03:02 remember that I'm doing this over here
- 00:03:05 when I store this item I also store it
- 00:03:08 with the ID being D well key in this
- 00:03:11 associative array therefore I can
- 00:03:14 retrieve it with that ID and the year I
- 00:03:16 can access my quantity field of that
- 00:03:20 item not of T total cart quantity for
- 00:03:22 now but just up that item through and
- 00:03:24 well decremented reduce it by one and
- 00:03:28 kind of a similar thing happens for or
- 00:03:31 with the price I select the price here
- 00:03:35 and then of course I don't want to
- 00:03:37 reduce it by one but I want to reduce it
- 00:03:39 by the price of one single item so I'll
- 00:03:43 use this minus equal operator to
- 00:03:45 basically just set this price equal to
- 00:03:49 the old price minus D whatever I'm going
- 00:03:52 to subtract here ante whatever I'm
- 00:03:55 subtracting here is again my item here
- 00:03:59 items selected by ID
- 00:04:01 and then the individual item remember
- 00:04:03 we're storing this individual item in
- 00:04:06 each item group so that so that we have
- 00:04:08 access to title description and also the
- 00:04:11 price of one single item so on that
- 00:04:14 single item then I can access the price
- 00:04:17 again and that is what I want to
- 00:04:19 subtract from the aggregated price so
- 00:04:25 with that the item group will be updated
- 00:04:28 to quantities reduce that the price is
- 00:04:30 well adjusted and I also need to adjust
- 00:04:33 the overall card totals so the total
- 00:04:36 price until the quantity therefore I'll
- 00:04:39 just access total quantities so that's
- 00:04:42 now a detailed quantity off the card I
- 00:04:44 reduce that by one and I also access the
- 00:04:48 total price and I want to reduce that
- 00:04:51 and of course I want to reduce it by the
- 00:04:53 very same amount I reduce the aggregated
- 00:04:56 price of this item group so by the price
- 00:04:59 of a single item like that with that the
- 00:05:03 card will get adjusted correctly and we
- 00:05:06 can actually see this in action so in
- 00:05:08 order to see this in action I need to
- 00:05:10 hook it up though for that I'll go into
- 00:05:13 my product controller and I need to add
- 00:05:16 a new route and I'll add it right here
- 00:05:19 after the get add to cart route or the
- 00:05:23 new action to be precise since I'm in
- 00:05:25 controller so this action should be
- 00:05:28 called get reduce by one for example and
- 00:05:33 I will pass an ID to that action by at
- 00:05:36 the router of course add them here I
- 00:05:40 well what do I want to do well first
- 00:05:44 thing is I want to basically copy the
- 00:05:48 code from my add to cart action here so
- 00:05:51 to fetch the old card and to create a
- 00:05:54 new card well of course I don't want to
- 00:05:56 call a card add here instead I want to
- 00:06:01 call cart and then reduce by one this
- 00:06:05 new function I just created my card
- 00:06:07 object so in here I need
- 00:06:10 pass the idea which I get passed into my
- 00:06:13 controller action here and with that
- 00:06:16 I'll reduce the cart or I'll adjust the
- 00:06:19 cart but of course I also need to store
- 00:06:22 it in my sessions a with session put
- 00:06:24 here and of course well you already
- 00:06:26 should have it otherwise this old code
- 00:06:28 would not worked but make sure to have
- 00:06:30 this use session import here at the top
- 00:06:33 but back to this code here I want to put
- 00:06:37 something on my session and of course I
- 00:06:39 want to put this new card that's just
- 00:06:42 following the same logic I was using
- 00:06:43 throughout this application with
- 00:06:45 recreating my card as a last that step
- 00:06:50 then of course I also want to redirect a
- 00:06:53 user or return a view here I will
- 00:06:55 redirect so I will redirect to a route
- 00:06:58 you route to which I want to redirect is
- 00:07:00 the shopping cart so that is shopping
- 00:07:04 cart no I think I called a shopping cart
- 00:07:08 here like that let's see product
- 00:07:12 shopping cart to this route here do I
- 00:07:14 want to redirect so we're almost there
- 00:07:17 of course I also need to create a new
- 00:07:19 route now and I will create it here
- 00:07:21 below the add to shopping cart route
- 00:07:23 this will be a get route of course and I
- 00:07:27 want you well URLs totally up to you I
- 00:07:30 will call it reduce and then very
- 00:07:33 importantly that's not up to you I need
- 00:07:35 to pass the ID and of course it has to
- 00:07:37 be named ID since I also named it ID in
- 00:07:40 my controller here the dollar sign is
- 00:07:43 omitted though so with that I well
- 00:07:46 created the route of course I need to
- 00:07:48 configure it so I want to use my product
- 00:07:52 controller here and then we get reduce
- 00:07:56 by one method I just created and I will
- 00:08:01 name this route let's say product
- 00:08:05 reviews by one something like that so I
- 00:08:10 can copy that already and then I will go
- 00:08:13 to my shopping
- 00:08:14 our view here and here I got my – well
- 00:08:18 buttons basically the second one isn't
- 00:08:22 what I have written the code for that
- 00:08:24 yet but I can hook up the first one so
- 00:08:27 I'm sure do you plate template
- 00:08:28 expression here oops didn't mean to do
- 00:08:30 that and in here I want to call the
- 00:08:32 route method to wear out function here –
- 00:08:36 then navigate to my reduced by one route
- 00:08:38 and keep in mind I need to pass the ID
- 00:08:40 here so ID shall be well I can't just
- 00:08:44 access this here the product and then
- 00:08:48 keep in mind product is not an item from
- 00:08:51 the database instead of this and array
- 00:08:53 an associative array so here I can then
- 00:08:56 access the item and again this is my
- 00:09:00 single item which has its ID in the
- 00:09:02 database so I can then also access the
- 00:09:04 ID on that so with all these changes in
- 00:09:08 place if we now go back and reload the
- 00:09:10 shopping cart we should see that if I
- 00:09:12 click reduced by one it gets reduced by
- 00:09:15 one you also see the price updated and
- 00:09:18 quantity here at the quantity under your
- 00:09:20 on the right as well as the total price
- 00:09:22 and I can reduce again and now that
- 00:09:25 already the behavior I don't want the
- 00:09:27 price is correct the quantity – but what
- 00:09:30 sense does it make to have an item in
- 00:09:33 the shopping cart of which you have no
- 00:09:35 copies in your shopping cart in the end
- 00:09:38 so you basically try to buy nothing but
- 00:09:41 still want to have it in shopping cart
- 00:09:42 that doesn't make sense and it even gets
- 00:09:45 weirder if I reduce it again well now we
- 00:09:48 have a negative quantity and a negative
- 00:09:50 price well that certainly is a great way
- 00:09:53 to earn money if you would have a shop
- 00:09:55 like that you get money back after all
- 00:09:57 but not really the behavior we want here
- 00:10:00 right and the reason for that of course
- 00:10:02 is that in the cart model here at the
- 00:10:06 end of the reduced by one function we're
- 00:10:09 never checking if we're well reaching a
- 00:10:11 quantity of zero or or lower and we have
- 00:10:15 to do that so what I want to do here is
- 00:10:17 I want to implement a check
- 00:10:19 where I basically check if this items
- 00:10:23 well select by the ID here if the
- 00:10:27 quantity is smaller or equal to zero
- 00:10:31 because that of course means now we have
- 00:10:34 no item in the cart anymore and then I
- 00:10:36 want to remove it from the cart and how
- 00:10:38 do I remove an item from the cart we can
- 00:10:42 use the PHP function unset for that and
- 00:10:45 that basically allows me to well destroy
- 00:10:48 variable and here of that I can destroy
- 00:10:51 an element in my items so in order to do
- 00:10:54 this I will call unset and then unset
- 00:11:00 this items ID so this items will still
- 00:11:05 persist only the item selected with the
- 00:11:09 idea with the key of ID will be deleted
- 00:11:12 and with that I remove it from the cart
- 00:11:15 now since we got that we can also write
- 00:11:18 the function for simply removing an item
- 00:11:22 from the card remove item so here we
- 00:11:25 don't want to reduce it by one we just
- 00:11:27 want to remove it entirely and of course
- 00:11:30 one step of removing is calling unset
- 00:11:33 but we also need to adjust the totals of
- 00:11:36 the cards so we'll just copy that code
- 00:11:39 and reducing the quantity by one is not
- 00:11:44 really right here because we might have
- 00:11:46 had three items off that in the card and
- 00:11:49 since I want to remove the item entirely
- 00:11:51 I can't just reduce it by one I need to
- 00:11:54 remove all copies we had in the card so
- 00:11:57 to make did you do this correctly I need
- 00:12:01 to subtract by well while the item we
- 00:12:05 want to remove but then by the quantity
- 00:12:07 off that I cannot just buy one and get a
- 00:12:10 similar thing is true here for any price
- 00:12:13 I don't want to subtract the price of
- 00:12:16 one item I want to subtract the
- 00:12:19 aggregated price and I can simply do
- 00:12:21 that by removing the item here so I'm
- 00:12:23 not just selecting this single item
- 00:12:25 anymore
- 00:12:26 study aggregate again with that remove
- 00:12:29 item should also work of course with
- 00:12:31 that back in the product controller I
- 00:12:34 also need to add an action for that so a
- 00:12:37 public function get remove item for
- 00:12:40 example which also gets D ID passed into
- 00:12:43 it and then here what do I want to do
- 00:12:46 well of course I want to fetch my cart
- 00:12:49 again and I will also finish with the
- 00:12:52 same actions redirecting to the shopping
- 00:12:54 cart and putting the new card into my
- 00:12:56 session but between these steps I will
- 00:12:59 call cart and then remove item and pass
- 00:13:02 ID so to use the action or the function
- 00:13:06 I just created with that this is
- 00:13:09 prepared and now with if I hooked it up
- 00:13:13 in my routes file I can treat and you
- 00:13:17 get route here route get and then maybe
- 00:13:22 remove then the ID but again the
- 00:13:28 configuration for that route so of
- 00:13:30 course I want to use my product
- 00:13:33 controller here and I want to call the
- 00:13:35 get remove item method or action and I
- 00:13:39 want to give this route a name off let's
- 00:13:42 say product remove something like that
- 00:13:44 and then I can go into my view here and
- 00:13:48 hook this route up I will just copy the
- 00:13:51 code of my other route because
- 00:13:54 technically it's very similar but I need
- 00:13:56 to rename it to just product remove
- 00:13:59 since that was the name I just assigned
- 00:14:01 here so with that if I reload this page
- 00:14:06 and well that is rather stupid here so
- 00:14:10 I'm just removing it all that worked as
- 00:14:14 you already saw now let's add a couple
- 00:14:16 of items here or maybe like that and now
- 00:14:20 let's try out the things we just added I
- 00:14:22 reduce this by 1 looks great now your
- 00:14:25 for a song of ice and fire notice I have
- 00:14:28 two items and notice that this makes up
- 00:14:31 $20 so if I click reduce all we should
- 00:14:35 see that we only got Harry Potter
- 00:14:37 Lord of the Rings left and the tool
- 00:14:38 should just be $30 then looks pretty
- 00:14:43 good to me
- 00:14:43 you also saw that it updated here on the
- 00:14:46 upper right now if I also remove all
- 00:14:50 these items here that looks good but
- 00:14:53 there's one thing I don't like I'm still
- 00:14:56 on the shopping cart page and I could
- 00:14:58 still click checkout even there were no
- 00:15:00 items left on the shopping cart and we
- 00:15:03 also should fix that as you'll see it
- 00:15:06 really is easy to fix I'll go to my
- 00:15:08 product controller and we can apply the
- 00:15:10 fix here in the get card
- 00:15:12 function which is responsible for giving
- 00:15:14 us our shopping cart view here the
- 00:15:19 problem we have is that we never check
- 00:15:21 if the total quantity of the card might
- 00:15:23 be zero yes we check if the session has
- 00:15:27 a card but it does have a card here and
- 00:15:30 we never delete a card I agree that
- 00:15:34 indicate remove item we might all just
- 00:15:36 forget the card so that we check if the
- 00:15:40 shopping cart still has any items if it
- 00:15:43 has items then we well do nothing but it
- 00:15:46 has if it has no item left over after
- 00:15:49 removing an item we might just clear the
- 00:15:52 session so that might be worth doing so
- 00:15:56 we could basically check if card and
- 00:15:58 then items and then what well I'll keep
- 00:16:03 in mind that items is an array so we
- 00:16:07 might just check if the count of that is
- 00:16:11 greater than 0 if this East case then I
- 00:16:17 want to put money shopping card
- 00:16:20 otherwise it looks like well we have no
- 00:16:24 items left so then I might just call
- 00:16:27 session 4 get card which will basically
- 00:16:30 delete the card with that if I only load
- 00:16:34 this well it will still look like that
- 00:16:36 but if I add a new item Harry Potter and
- 00:16:38 then I
- 00:16:40 reduce that now we see no items in card
- 00:16:43 because now that gets cleared however if
- 00:16:45 I have multiplied the most you for
- 00:16:47 example and I only remove one we still
- 00:16:50 have a card only if I remove dad is it
- 00:16:53 gone and now we see the problem again
- 00:16:55 the reason of course being that I only
- 00:16:57 forget it here in the get remove item
- 00:16:59 action but not only reduce one action
- 00:17:02 however here I also have to make sure
- 00:17:05 you well delete it if we have no items
- 00:17:08 left so it matches copy this and yes of
- 00:17:10 course you could refactor that too but I
- 00:17:13 think we should be fine like that so
- 00:17:16 with that just to do a final check if I
- 00:17:19 add let's say these items here and then
- 00:17:23 I reduce that by one it's gone
- 00:17:25 I would use step by one and buy one
- 00:17:28 again and now we have an empty card and
- 00:17:31 with that we have a working shopping
- 00:17:33 card again I also touch to this at the
- 00:17:36 beginning of the video yes you could add
- 00:17:38 further functionalities but you would
- 00:17:39 never stop with this serious Dan and I
- 00:17:42 rather prefer making additional serious
- 00:17:44 on well our topics then stretching this
- 00:17:48 out and lessly you saw how to create a
- 00:17:50 shopping cart how to store it in a
- 00:17:52 session how to make charges how to store
- 00:17:55 orders in the database how to see your
- 00:17:57 shopping cart page if you also take my
- 00:18:00 social networks series on a channel
- 00:18:02 account you should also be able to work
- 00:18:05 on an admin back-end which allows you to
- 00:18:08 create your own products and at the end
- 00:18:10 programming really is about all these
- 00:18:12 things you have to set your own
- 00:18:14 challenges and then try to reach them at
- 00:18:17 being said future serious well account
- 00:18:20 and I will attach some other interesting
- 00:18:23 topics there to you happy to see you
- 00:18:25 Darren and I really hope you enjoyed
- 00:18:26 this series bye