Answered by:
Cart Total All Items VB

Question
-
User2090826535 posted
I have a simple project that gets a product and sends the Quantity to a cart where I can display the items total.
My issue is how can I total the items for a grand total.
This is what I'm using to template a project , http://www.beansoftware.com/ASP.NET-Tutorials/Developing-Shopping-Cart.aspx
Below I modified the cartItem.vb class to give me item qty total. Just need to total all the items and send to like a label.
Public Class CartItem Public Product As Product Public Quantity As Integer Public Function Display() As String Return Product.Name + ", Qty " + Quantity.ToString() _ + " at " + FormatCurrency(Product.UnitPrice) + " each, Current Item Total " + FormatCurrency(Quantity * Product.UnitPrice) End Function
Wednesday, March 18, 2015 11:28 AM
Answers
-
User281315223 posted
I suppose you could try explicitly converting the value :
Public Function GetTotal() As Decimal Return Convert.ToDecimal(Cart.Sum(Function(c) c.Quantity * c.Product.UnitPrice)) End Function
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, March 18, 2015 3:23 PM
All replies
-
User281315223 posted
Since you know that you can get the total of a single item by multiplying the Quantity and the Product.UnitPrice properties, you would just need to do this for every item within your Cart (which I am assuming is a collection of CartItem objects :
' Retrieve the total for all of the items in your Shopping Cart' Dim total = YourShoppingCartItems.Sum(Function(i) i.Quantity * Product.UnitPrice)
Without knowing a bit more about your ShoppingCart class itself, this is just a guess. But basically you can use the available Sum() method through LINQ to calculate the total for each item in your collection (e.g. YourShoppingCartItems or Cart.Items, however you have it defined) and then sum those.
Wednesday, March 18, 2015 12:28 PM -
User2090826535 posted
Rion - I'm having an issue placing that logic in the code. This is as close as I can get.
This is the cartitem.vb class.
Public Function Display() As String Return Product.Name + ", Qty " + Quantity.ToString() _ + " at " + FormatCurrency(Product.UnitPrice) + " each, Current Item Total " + FormatCurrency(Quantity * Product.UnitPrice) End Function Public Function Tsum() As String Dim CartItem As CartItem Dim CartEntry As DictionaryEntry Dim t As String For Each t In CartItem.Product Dim total = CartItems.Sum(Function(i) i.Quantity * Product.UnitPrice) Return total Next End Function
Wednesday, March 18, 2015 12:48 PM -
User281315223 posted
What does your "Cart" object look like?
I would imagine that it contains a collection of items (e.g. CartItems) that you would use to get a total for. If that is the case, you could simply define a function on it to return the total :
' Define this on your actual Cart ' Public Function GetTotal() As Decimal Dim total = CartItems.Sum(Function(i) i.Quantity * Product.UnitPrice) Return total End Function
Wednesday, March 18, 2015 12:56 PM -
User2090826535 posted
Here is the cart object with the additional code. I missing something.
Imports Microsoft.VisualBasic Public Class CartItem Public Product As Product Public Quantity As Integer Public Function Display() As String Return Product.Name + ", Qty " + Quantity.ToString() _ + " at " + FormatCurrency(Product.UnitPrice) + " each, Current Item Total " + FormatCurrency(Quantity * Product.UnitPrice) End Function ' Define this on your actual Cart ' Public Function GetTotal() As Decimal Dim total = CartItem.Sum(Function(i) i.Quantity * Product.UnitPrice) Return total End Function End Class
Here it the aspx page to display it. This works just need the total. Private Sub DisplayCart() lstCart.Items.Clear() Dim CartItem As CartItem Dim CartEntry As DictionaryEntry For Each CartEntry In Cart CartItem = CType(CartEntry.Value, CartItem) lstCart.Items.Add(CartItem.Display) Next End Sub
Wednesday, March 18, 2015 1:09 PM -
User281315223 posted
So it looks like you are storing all of your individual cart items within a collection (Cart) if that is the case, you can use that to grab your total :
Private Sub GetTotal() As Decimal Return Cart.Sum(Function(c) c.Quantity * c.Product.UnitPrice) End Sub
Wednesday, March 18, 2015 1:16 PM -
User2090826535 posted
I have the GetTotal function in my Class called Cartitem.vb it wants cart to be declared I have tried Decimal, Integer and String none work.
I believe this is what your indicating.
Imports Microsoft.VisualBasic Public Class CartItem Public Product As Product Public Quantity As Integer Public Cart As Decimal Public Function Display() As String Return Product.Name + ", Qty " + Quantity.ToString() _ + " at " + FormatCurrency(Product.UnitPrice) + " each, Current Item Total " + FormatCurrency(Quantity * Product.UnitPrice) End Function Public Function GetTotal() As Decimal Return Cart.Sum(Function(c) c.Quantity * c.Product.UnitPrice) End Function
Wednesday, March 18, 2015 1:36 PM -
User2090826535 posted
With this it say's sum is not a member of Decimal.Public Class CartItem Public Product As Product Public Quantity As Integer Public Cart As Decimal Public Function Display() As String Return Product.Name + ", Qty " + Quantity.ToString() _ + " at " + FormatCurrency(Product.UnitPrice) + " each, Current Item Total " + FormatCurrency(Quantity * Product.UnitPrice) End Function Public Function GetTotal() As Decimal Return Cart.Sum(Function(c) c.Quantity * c.Product.UnitPrice) End Function End Class
Wednesday, March 18, 2015 1:40 PM -
User281315223 posted
Try Double :
Public Function GetTotal() As Double Return Cart.Sum(Function(c) c.Quantity * c.Product.UnitPrice) End Function
Wednesday, March 18, 2015 2:55 PM -
User2090826535 posted
No go on Double.
Thanks
Wednesday, March 18, 2015 3:13 PM -
User281315223 posted
I suppose you could try explicitly converting the value :
Public Function GetTotal() As Decimal Return Convert.ToDecimal(Cart.Sum(Function(c) c.Quantity * c.Product.UnitPrice)) End Function
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, March 18, 2015 3:23 PM