我一直致力于建立一个拥有蜡烛购物车的ASP.net网站。当您从DDL中选择罐子类型,香水和染料并点击提交到购物车按钮时,它会将它们添加到购物车中并更新价格。我们有两个主要问题......
1)当我们选择第一支蜡烛并将其提交给购物车时,它会更新价格。但是,当我们尝试向购物车添加另一根蜡烛时,它不会更新价格。
2)当我们从购物车中选择一个项目后点击删除项目按钮时,它会抬高价格,如果我们删除所有内容,它会将总数设置为负数。
以下是我们的代码,任何帮助都会非常感谢我们在网上搜索并要求多个朋友无济于事。
这是将项目添加到购物车按钮。添加多个蜡烛时,购物车总价不会更新:
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
从购物车中删除商品。没有正确更新价格:
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
答案 0 :(得分:1)
引用:“当添加多个蜡烛时,购物车总价不会更新”
您在此处显示总价lblTotal.Text = decTotal.ToString("c")
但是当您计算它时,您不会继续添加它,您只需添加最后一项
decTotal = decSubtotal + decTax + decShipping
应该是
decTotal = decTotal + decSubtotal + decTax + decShipping
同样,当您移除项目时,您希望从总计中减去项目的price+tax+shipping
。而不是
decTotal = decSubtotal + decTax + decShipping
待办事项
decTotal = decTotal - (decSubtotal + decTax + decShipping)
虽然,这是原始的运费计算。运费应始终根据购物车中剩余的商品,根据其重量,尺寸等计算。一旦您移除您的商品(价格+税) - 重新计算运费并将其添加到单独保留的价格。