What is a shopping cart?
You must have been to the supermarket. There you can push the cart, put your favorite goods into the cart, or take out the goods from the cart and put them back on the shelf, and finally you push the cart to check out.
Then, in online supermarkets, customers should also be able to put their favorite products into the "electronic cart" when browsing the product catalog. Electronic carts are electronicization of supermarket carts. In online stores, this type of electronic cart is also called "shopping cart", which is shopping cart in English.
Design goals of shopping carts
From the perspective of programmers, shopping carts are an object that maintains shoppers’ product selection, inspection and modification. The shopping cart itself is a very simple program, but developers should consider it to connect to the product catalog subsystem, order subsystem, customer account subsystem, site management subsystem, etc. to form a fully functional online store.
Here are the design goals of shopping carts:
1. Continuity: The shopping cart should remember its content from its previous session.
2. Shopping carts are related to customers, not to customers’ computers. Customers can access the shopping cart from another computer or browser.
3. When new products are added to or removed from the cart, the content of the cart can be displayed to the user.
4. The cart can accommodate many or even unlimited products.
System design/process design
Before writing our shopping cart, we take a look at its system architecture and processes.
If the customer selects an item from the product directory, we pass the customer's request to proxy.asp, and at the same time pass the action variable "add product". proxy.asp reads this variable and decides which action to perform the shopping cart. These actions include: increasing the product, updating the product quantity, removing the product, or viewing the shopping cart.
Some actions are called internally. If we create a cart, we need to check whether the cart already exists (checkcart). When adding, deleting or updating the product and its quantity, we need to confirm whether this product already exists in our shopping cart (checkitem). Below we will design 8 methods for shopping carts, namely:
viewitem View cart
checkcart checkcart
createcart Create a cart
additem(id, qty), add product
removeitem(id) Remove item
updateitem(id, qty), update number
removeall clear
checkitem(id) product checkitem
Shopping cart design
A shopping cart requires three elements: a vbscript 5 class cartkit, a multi-dimensional array mudcart, and a session variable session("cart").
This vbscript class, called cartkit, contains 8 methods, as shown in the above table. In this article, we only use id and qty, which represent the product number and quantity respectively.
In this way, we can use a two-dimensional array to express the cart, like this:
Product number Product quantity
id-1 23
id-3 10
id-23 6
id-2 1
Then we save this two-dimensional array into the session variable.
Design of shopping carts
Please download: cartkit.asp
Createcart design:
Please see the program code:
class cartkit rem starts the definition of class cartkit
sub createcart()
if isarray(session("cart")) = false then
dim mudcart(19,1)
session("cart") = mudcart
end if
end sub
Among them: session("cart") saves the content of the cart. If the cart does not exist, we define a two-dimensional array mudcart to express the cart and save it in session("cart").
Checkcart design:
This function determines whether cart has been created. It's relatively simple.
function checkcart()
if isarray(session("cart")) then
checkcart=true
else
checkcart=false
end if
end function
Checkitem design:
Please see the code:
function checkitem(id)
if checkcart=true then
varmudcart = session("cart")
for i = lbound(varmudcart) to ubund(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 number id of the product with the product number in the shopping cart one by one. If there is equality, return true. Otherwise it is false.
additem(id,qty) design:
Please refer to the attachment of this article cartkit.asp. We encapsulate the functions introduced here into a class called cartkit. The following code snippet first creates a cartkit object and then checks whether the cart already exists. If it does not exist, create a cart and add new items; otherwise, check whether the item number already exists in the cart. If so, the quantity will be updated; otherwise, new products will be added.
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
session("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 = lbound(mudcart) to ubund(mudcart)
if mudcart(i,0) = "" then
mudcart(i,0) = id
mudcart(i,1) = qty
session("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 ubund(mudcart)
if mudcart(i,0) = id then
mudcart(i,1) = qty
session("cart")=mudcart
exit function
end if
next
end function
viewitem design:
function viewitem()
mudcart=session("cart")
if isarray(mudcart) then
%>