1

I have been working on making an ASP.net site that has a shopping cart for candles. When you select the jar type, fragrance, and dye from the DDL's and hit the submit to cart button it adds them into the cart and updates the price. We are having two main issues...

1) When we select the first candle and submit it to the cart it updates the price. However when we attempt to add another candle to the cart it does not update the price.

2) When we hit the remove item button after selecting an item from the cart it messes up the price and if we remove everything it sets the total to a negative number.

Below is our code any help would be really appreciated guys I have scoured the web and asked multiple friends to no avail.

This is the Add Item To Cart button. When adding more than one candle, the cart Total Price does not update:

 Protected Sub btnCart_Click(sender As Object, e As EventArgs) Handles btnCart.Click


    'jar
    If ddlJar.SelectedIndex = 0 Then
        decSubtotal += 11.99D
    End If
    If ddlJar.SelectedIndex = 1 Then
        decSubtotal += 11.99D
    End If
    If ddlJar.SelectedIndex = 2 Then
        decSubtotal += 7.99D
    End If
    If ddlJar.SelectedIndex = 3 Then
        decSubtotal += 8.99D
    End If
    If ddlJar.SelectedIndex = 4 Then
        decSubtotal += 3.99D
    End If
    If ddlJar.SelectedIndex = 5 Then
        decSubtotal += 7.99D
    End If


    'Fragrance
    If ddlFrag.SelectedIndex = 0 Then
        decSubtotal += 5.99D
    End If
    If ddlFrag.SelectedIndex = 1 Then
        decSubtotal += 5.99D
    End If
    If ddlFrag.SelectedIndex = 2 Then
        decSubtotal += 5.99D
    End If
    If ddlFrag.SelectedIndex = 3 Then
        decSubtotal += 5.99D
    End If
    If ddlFrag.SelectedIndex = 4 Then
        decSubtotal += 5.99D
    End If
    If ddlFrag.SelectedIndex = 5 Then
        decSubtotal += 5.99D
    End If
    If ddlFrag.SelectedIndex = 6 Then
        decSubtotal += 5.99D
    End If
    If ddlFrag.SelectedIndex = 7 Then
        decSubtotal += 5.99D
    End If
    If ddlJar.SelectedIndex = 8 Then
        decSubtotal += 5.99D
    End If
    If ddlFrag.SelectedIndex = 9 Then
        decSubtotal += 5.99D
    End If
    If ddlFrag.SelectedIndex = 10 Then
        decSubtotal += 5.99D
    End If



    'Calc Tax, Total, discount
    decTax = decSubtotal * decTAX_RATE
    decTotal = decSubtotal + decTax + decShipping
    decShipping = decShipping_Rate * 1


    lblSubtotal.Text = decSubtotal.ToString("c")
    lblTax.Text = decTax.ToString("c")
    lblShipping.Text = decShipping.ToString("c")
    lblTotal.Text = decTotal.ToString("c")

    lstCart.Items.Add(ddlJar.SelectedItem)
    lstCart.Items.Add(ddlDye.SelectedItem)
    lstCart.Items.Add(ddlFrag.SelectedItem)

End Sub

Removes item from cart. Doesn't correctly update price:

Protected Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click


    If lstCart.SelectedIndex = -1 Then
        Response.Write("<script type=""text/javascript"">alert(""You must have items in your cart."");</script")
    Else

        'jar
        If lstCart.SelectedItem.ToString = "12 Status" Then
            decSubtotal -= 11.99D
        End If
        If lstCart.SelectedItem.ToString = "12 Hex" Then
            decSubtotal -= 11.99D
        End If
        If lstCart.SelectedItem.ToString = "8 Tin" Then
            decSubtotal -= 7.99D
        End If
        If lstCart.SelectedItem.ToString = "9 Hex" Then
            decSubtotal -= 8.99D
        End If
        If lstCart.SelectedItem.ToString = "4 Hex" Then
            decSubtotal -= 3.99D
        End If
        If lstCart.SelectedItem.ToString = "8 Jelly" Then
            decSubtotal -= 7.99D
        End If


        'Fragrance
        If lstCart.SelectedItem.ToString = "Monkey Farts" Then
            decSubtotal -= 5.99D
        End If
        If lstCart.SelectedItem.ToString = "Grapefruit" Then
            decSubtotal -= 5.99D
        End If
        If lstCart.SelectedItem.ToString = "Stress Relief" Then
            decSubtotal -= 5.99D
        End If
        If lstCart.SelectedItem.ToString = "Beachwood" Then
            decSubtotal -= 5.99D
        End If
        If lstCart.SelectedItem.ToString = "Blueberry Cobbler" Then
            decSubtotal -= 5.99D
        End If
        If lstCart.SelectedItem.ToString = "Black Ice" Then
            decSubtotal -= 5.99D
        End If
        If lstCart.SelectedItem.ToString = "Beautiful Day" Then
            decSubtotal -= 5.99D
        End If
        If lstCart.SelectedItem.ToString = "Polo Black" Then
            decSubtotal -= 5.99D
        End If
        If lstCart.SelectedItem.ToString = "Lime Basil" Then
            decSubtotal -= 5.99D
        End If
        If lstCart.SelectedItem.ToString = "VS LoveSpell" Then
            decSubtotal -= 5.99D
        End If
        If lstCart.SelectedItem.ToString = "Georgia Peach" Then
            decSubtotal -= 5.99D
        End If



        'Calc Tax, Total, discount
        decTax = decSubtotal * decTAX_RATE
        decTotal = decSubtotal + decTax + decShipping
        decShipping = decShipping_Rate * 1


        lblSubtotal.Text = decSubtotal.ToString("c")
        lblTax.Text = decTax.ToString("c")
        lblShipping.Text = decShipping.ToString("c")
        lblTotal.Text = decTotal.ToString("c")

        lstCart.Items.RemoveAt(lstCart.SelectedIndex)
    End If


End Sub
T.S.
  • 18,195
  • 11
  • 58
  • 78
  • 2
    Dude, please don't hard code item-names and prices into the code. Create a little dto object for the Products, And in a method that has no UI code....go fetch them from an .xml file at the very least. List GetAllProducts() { /* read some .xml here */ }. I'm trying to help. Hardcoding values is a maintenance nightmare. Here is a basic/basic example: http://stackoverflow.com/questions/9336851/how-to-map-xml-file-content-to-c-sharp-objects – granadaCoder Dec 01 '15 at 02:13
  • We didn't hear any feedback from you. Please accept the answer, which I think, answers your troubles – T.S. Dec 01 '15 at 23:10

1 Answers1

1

Quote: "When adding more than one candle, the cart Total Price does not update"

You display total price here lblTotal.Text = decTotal.ToString("c") But when you calculate it, you don't keep adding to it, you only add last item

decTotal = decSubtotal + decTax + decShipping

Should be

decTotal = decTotal + decSubtotal + decTax + decShipping

Similarly, when you remove item, you want to subtract item's price+tax+shipping from total. Instead of

decTotal = decSubtotal + decTax + decShipping

Do

decTotal = decTotal - (decSubtotal + decTax + decShipping)

Although, this is primitive shipping calculation. Shipping should be always calculated based on items remaining in cart, based on their weight, size, etc. Once you remove your item (price + tax) - recalculate shipping and add it to separately kept price.

T.S.
  • 18,195
  • 11
  • 58
  • 78