Shopping Cart in Asp.Net
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())
TAGS: Asp.net, dotnet, code snippet, seo, search engine optimization, visual studio.net, sample code, c#, vb.net
























Q Le said
am March 17 2007 @ 10:24 am
Nicely done!!! It was very helpfull for me. Thanks
SuperTal said
am November 5 2007 @ 4:10 am
Great job! Excellent article…
Geoff said
am November 21 2007 @ 10:00 pm
I’ve been looking for something like this for a while. One question I have, is what is the best method for allowing users to modify the quantity of an item in the cart.
Right now I’m trying to figure out how to bind the cart to a repeater so that I can display the quantity in a text box with a button that calls the modify method of the shopping cart class, but I’m hitting a brick wall trying to figure out how to get the session data in a format that will bind to the repeater.
idibiasi said
am November 26 2007 @ 1:28 am
The best wey to get an easy binding is to write a Property with Get and Set, then use the property as binding field
Geoff said
am November 26 2007 @ 10:13 pm
The Modify method works fine for me for changing the quantity to anything but zero. When changed to a zero, the cart still displays the cart item with a zero quantity, and doesn’t dissapear until I try to modify the quantity for that item a second time.
Any ideas? As you can probably tell I’m fairly new to .net
Geoff said
am November 27 2007 @ 1:12 am
Having the same problem with the clear method. It seems like I’m missing something on getting the session to update right away. I click the clear button that I created to call the clear method, it looks like nothing happens, but then if I call another method (such as the modify method) I then see that the cart WAS cleared. GRRR!