Good day again!
This is the third and final part of the shopping cart basic concepts tutorial.
In the previous parts of the tutorial ( part 1, part 2) we haw saw how a shopping cart works, how it keeps track of the client's purchases, how to build the code that gives the user the possibility to add and delete items from the cart and today we will finally see how to place the final order, how to perform different calculations that could be necessary to the user.
First of all, let's take a small review upon what said in the previous parts.
1. The visitor enters your website.
2. Every single page contains the code that verifies if he has already a cart associated to him.
3. In case he doesn't have a shopping cart, we launch the block of code that creates his personal shopping cart, at the beginning, empty.
4. Once we have a shopping cart, we ask for the ID of the shopping cart contained into the COOKIE and we count the number of items associated with the cart.
5. We display the items contained in the database according to the items associated with the shopping cart (if the item is not associated with the cart we can display the Add to cart button. Else, we can display the Delete from cart button).
* Ask for the cart ID
* Extract all the items from the database
* For every single item, verify if the item's ID is associated with the shopping cart's ID
* Display the information
6. We provide the code that the page will use in order to filter the requests coming and perform the required tasks (the switch with the containing code)
To understand better the way that the user is linked to the items he bought, take a look at the illustration bellow:
Now, we will see how to build the page that displays the content of the cart and how do we place an order.
Let's suppose that the page is named mycart.php
When the user enters the page we have to display only the items he selected to make part of his cart.
The items table and the carts table are linked through rel table.
For that reason, we will retrieve the shopping cart's ID from the cookie, and we will select the item ID's that are linked to it inside the rel table.
After that, for every single item ID, we will extract the item details from items table.
To do that we use the code:
The resulting page will looke like this:
Considering that the displayed elements are all contained inside the cart, we can also display for every item the Delete from cart button, adding the following code:
Now, the whole code has become like this:
This way, the user will be directed back to mypage.php. If you want to avoid this, you can set inside the form action the value mycart.php and add to the page the code that filters the requests and does the required action. Watch part two
Very good! You are doing great!
What's left to do is adding another function to the page. I am talking about the function that can modify the item quantity selected by the user, without having to delete the item and add it again, directly from cart.
To do this, we need to display the item quantity in a text input, contained in a form along a submit button.
I will use GET method for this too.
The form would look like this:
By assembling this piece of code with the rest, we obtain the final "display rules" for the mycart.php page
It all looks like this:
Just as on mypage.php, we have to different possible requests, so we need to place the code that does the difference between them.
It looks like this:
After adding the code that will handle the requests inside the switch cases, the whole code looks like this.
Note that the switch code must be placed inside the page before the code that builds the HTML because you must update the content of the cart before displaying it.
Now you have all the functions necessary on this page.
The page will look like this:
What's left to do is calculate the total amount and display the Place Order button
The total amount could be calculated inside the WHILE used for the display.
We create a new variable with an initial value of 0, and by every loop we add to it the value of every single item.
At the end of all loops, we can display the calculated value and the button to place the order.
I will go back to review the code and add the necessary instructions.
The resulting code will look like this:
We need to display the total price and the button.
The code that will handle the place order request will be inserted in the myorder.php page that will contain also a form where the user can insert billing address, personal data and so on.
To process the order we need a new table in which we will store the passed information
Use the following code:
You will obtain this:
The myorder.php page will retrieve the cart ID from the cookie and by doing that, will know what items have been ordered.
In other words, the orders page will link the rel table with the orders table and after doing that will delete the cookie that links the user with the cart. That way, the cart does not exist any more (in a way of speaking) and the order will be registered.
Because the cookie will be deleted, as soon as the users navigates to another page of the website he will have another cookie with another code set, so he will have a brand new empty shopping cart.
This is how our system works now:
We presume that myorder.php send the request of order registration along with client name and address via GET method.
First, we grab the shopping cart ID and the passed data.
We register all the data inside the orders table.
After doing that, we unset the cookie that contains the shopping cart ID.
Here is the code to do this.
Now the order has been placed and the user will have a new empty shopping cart.
I think this is all, my friends and i hope i have not forgot anything.
For further questions, just post a comment and i will provide a solution as soon as possible.
See you soon!
Good day again!