Today i needed to add Shopping Cart functionality to one of my websites.
The solution i adopted is really simple, i did it in few minutes but i think it could be useful. It can be improved but it’s a good starting point if u just need a simple shopping cart.
I choosed this way:
- Create a class named ShoppingCart
- Create a class named CartItem to store item infos
- In the Session_Start event of Global.asax i create the ShoppingCart instance and add it to session. I named it Session(”ShoppingCart”)
- In each page that needs to access the Shopping Cart i use the stored Session(”ShoppingCart”) and use it’s methods
The Shopping Cart Class methods i wrote are:
- New, to create the empty cart
- Add, to add a CartItem object
- Modify, to vary the count of an item or remove it setting it’s count to zero
- Empty, to empty the Shopping Cart
- ItemTotal, to get the total price for an object (count * price)
- Total, to get the Shopping Cart total price to pay
The CartItem class contains these infos:
- ID
- Name
- Price
- Count
Here is the Code:
Public Class CartItem Public ID As String Public Name As String Public Price As Double Public Count As Integer Public Sub New() ID = "-1" Name = "" Price = 0 Count = 0 End Sub End Class Public Class ShoppingCart Public Items As New Hashtable Public Sub New() Items.Clear() End Sub 'Add an item to the Shopping Cart Public Function Add(ByVal c As CartItem) As Boolean Dim tmpItem As CartItem If c.Name <> "" And c.Count > 0 Then tmpItem = Items(c.Name) If Not tmpItem Is Nothing Then tmpItem.Count += c.Count Else Items.Add(c.Name, c) End If Return True Else Return False End If End Function 'Modify an object quantity 'If quantity is zero the item is removed Public Function Modify(ByVal c As CartItem) As Boolean Dim tmpItem As CartItem If c.Name <> "" Then tmpItem = Items(c.Name) If Not tmpItem Is Nothing Then If tmpItem.Count > 0 Then tmpItem.Count = c.Count Else Items.Remove(c.Name) End If Return True Else Return False End If Else Return False End If End Function 'Empty the Shopping Cart Public Sub Empty() Items.Clear() End Sub 'calculate an object total price (items*price) Public Function ItemTotal(ByVal itemName As String) As Double Dim tmpItem As CartItem If itemName <> "" Then tmpItem = Items(itemName) If Not tmpItem Is Nothing Then Return tmpItem.Count * tmpItem.Price Else Return 0.0 End If Else Return 0.0 End If End Function 'Calculate the total price to pay Public Function Total() As Double Dim totale As Double = 0.0 For Each k As String In Items.Keys Dim ci As CartItem = Items(k) totale += ci.Price * ci.Count Next Return totale End Function End Class
Download this code: ShoppingCart.vb.txt
In the Global.asax file you need to write
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
Session(”ShoppingCart”) = New ShoppingCart
End Sub
To Add an item to the Shopping Cart
Dim cart As ShoppingCart = Session(”ShoppingCart”)
Dim it As New CartItem
it.Name = “Test Object”
it.ID = “1″
it.Price = 100.22
it.Count = 1
cart.Add(it)
To Show the cart items
Dim cart As ShoppingCart = Session(”ShoppingCart”)
For Each k As String In cart.Items.Keys
Dim ci As CartItem = cart.Items(k)
Response.write(ci.Name + “-” + ci.Count.ToString() + ” Price: ” + ci.Price.ToString + ”
“)
Next
Response.Write(”Total: ” + cart.Total.ToString())
