Design Shopping Carts with ASP

Source: Internet
Author: User
Tags array definition exit vbscript class
Design 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. Take a look at the picture below:

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 the cart, like this? br> Commodity Number
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) $#@60;$#@62; 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
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 UBound (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 UBound (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
%$#@62;
$#@60;table$#@62;
$#@60;tr$#@62;
$#@60;td$#@62;item name$#@60;/td$#@62;
$#@60;td$#@62;item quantity$#@60;/td$#@62;
$#@60;td$#@62;$#@60;/td$#@62;
$#@60;/tr$#@62;
$#@60;%
For i = LBound (Mudcart) to UBound (Mudcart)
IF Mudcart (i,0) $#@60;$#@62; "" THEN
%$#@62;
$#@60;tr$#@62;
$#@60;td$#@62;$#@60;%=%$#@62;$#@60;/td$#@62;
$#@60;%
Response.Write "Item ID:" & Mudcart (i,0) & "$#@60;--"
Response.Write "$#@60;BR$#@62;"
Response.Write "Item Qty:" & Mudcart (i,1) & "$#@60;--"
Response.Write "$#@60;BR$#@62;"
End IF
NEXT
ELSEIF IsArray (Mudcart) = False THEN
Response.Write "No Item in your cart!"
Response.Write "$#@60;BR$#@62;"
Response.Write "Y don ' t do shop!"
End IF
End Function
RemoveItem (ID) Design:
Function RemoveItem (ID)
Mudcart=session ("Cart")
' Find the Postion/index of given ID.
For i = LBound (Mudcart) to Ubound (Mudcart)
IF Mudcart (i,0) = ID THEN
Pos=i
Exit for
End IF
Next
' Delete Process ...
For i = Pos to UBound (Mudcart)-1
IF not Mudcart (i,0) = "" THEN
Mudcart (i,0) = Mudcart (i+1,0)
End IF
Next
End Function
End Class REM ending class Cartkit definition



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.