What is a shopping cart?
You must have been to the supermarket. There you can push the cart, put the favorite items into the cart, or take the goods out of the cart and put them back on the shelves, and then you push the cart to checkout.
Then, in the online supermarket, customers should also be able to browse the catalogue, the favorite products into the "electronic cart." Electronic carts are the electronics of supermarket carts. In the online store, this kind of electronic trolley is also called "Shopping cart", English is shopping cart.
design goals for shopping carts
From a programmer's point of view, a shopping cart is an object that maintains shopper purchases, allows viewing, and allows modifications. The shopping cart itself is a very simple program, but developers have to consider it will be connected to the commodity directory subsystem, order subsystem, customer account subsystem, site management subsystem, etc., to form a full-featured online store.
The following are the design goals for the shopping cart:
1. Persistence: The shopping cart should remember its contents from its previous session.
2, Shopping carts and customer-related, not with the customer's computer-related. Customers can access the shopping cart from another computer or browser.
3. When a new product is added to the cart or removed from the cart, the contents of the cart can be displayed to the user.
4, the trolley can accommodate a lot of even unlimited merchandise.
system design/Process design
Before writing our shopping cart, let's look at its system architecture and process.
If the customer chooses a product from the catalogue, we pass the customer's request to the proxy.asp, while passing the action variable "increase the product". Proxy.asp reads this variable and decides which action to perform in the shopping cart. These actions include adding goods, updating the number of items, removing goods, or viewing shopping carts.
Some actions are called as internal calls. For example, before creating a cart, we need to check if the cart already exists (Checkcart). When adding, deleting or updating items and their quantities, we want to make sure that the product is already in our shopping cart (Checkitem). Below we will design 8 methods for shopping carts, respectively:
ViewItem the cart.
Checkcart Check Cart
Createcart Create Cart
AddItem (ID, qty), add product
RemoveItem (ID) Delete product
UpdateItem (ID, qty), Update quantity
RemoveAll, all clear.
Checkitem (ID) Commodity inspection
the design of shopping carts
Shopping carts need three elements: a VBScript 5 class Cartkit, a multidimensional array mudcart, a Session variable session ("Cart").
This VBScript class, which we call Cartkit, contains 8 methods, as shown in the previous table. In this article we use only IDs and qty, which represent the number and number of items.
So we can use a two-dimensional array to express this cart, like this:
Item number number of items
Id-1 23
Id-3 10
Id-23 6
Id-2 1
Then we'll save the two-dimensional array in the session variable.
Design of Shopping cart type Cartkit
Please download: cartkit.asp
Createcart Design:
Please look at the program code:
Class Cartkit REM Start class Cartkit definition
Sub Createcart ()
If IsArray (Session ("cart")) = False Then
Dim Mudcart (19,1)
Session ("cart") = Mudcart
End If
End Sub
Where: Session ("cart") is to save the contents of the cart. If the cart does not exist, we define a two-dimensional array mudcart to express the cart and save it to the session ("cart").
Checkcart Design:
This function determines whether the cart has been created. relatively simple.
function Checkcart ()
If IsArray (Session ("cart")) Then
Checkcart=true
Else
Checkcart=false
End If
End Function
Checkitem Design:
Please look at the code:
function Checkitem (ID)
If Checkcart=true Then
Varmudcart = Session ("Cart")
For i = LBound (Varmudcart) To UBound (Varmudcart)
If Varmudcart (i,0) = ID Then
Checkitem=true
Exit function
ElseIf Varmudcart (i,0) <> ID Then
Checkitem=false
End If
Next
End If
End Function
First determine whether the cart exists. Then compare the item number ID with the item number in the shopping cart. If there is equal, return true. Otherwise it is false.
AddItem (id,qty) Design:
Please refer to the attachment cartkit.asp in this article for the functions described here we encapsulate them in a class called Cartkit. The following code snippet first creates a Cartkit object and then checks to see if the cart already exists. If it does not exist, create a cart and add the item, or check that the item number is already in the cart. If yes, update the quantity; otherwise, add the product.
function additem (ID, qty)
Set cartobj = new Cartkit
Varcartstatus = Cartobj.checkcart
If varcartstatus =false then
Cartobj.createcart
mudcart=session ("cart")
Mudcart (0,0) =id
Mudcart (0,1) =qty
Sess Ion ("cart") =mudcart
Exit Function
ElseIf varcartstatus=true then
if Cartobj.checkitem (id) = True then
Cartobj.updateitem id,qty
ElseIf cartobj.checkitem (id) = False then
Mudcart = Session ("Cart")
for i = l Bound (Mudcart) to UBound (Mudcart)
if Mudcart (i,0) = "" Then
Mudcart (i,0) = ID
Mudcart (i,1) = Qty
SE Ssion ("cart") = Mudcart
Exit Function
End If
Next
End If
End If
End Function
UpdateItem design :
function UpdateItem (ID, qty)
Mudcart = Session ("Cart")
For i = LBound (Mudcart) To UBound (Mudcart)
if Mudcart (i,0) = ID then
Mudcart (i,1) = Qty
Session ("C Art ") =mudcart
Exit Function
End-if
Next
End Function
ViewItem design:
function ViewItem ()
Mud Cart=session ("cart")
if IsArray (mudcart) then
%>