更新多个项目的购物车价格

Update shopping cart price with multiple items

我一直致力于制作一个 ASP.net 网站,该网站有一个蜡烛购物车。当您 select 来自 DDL 的罐子类型、香水和染料并点击提交到购物车按钮时,它会将它们添加到购物车并更新价格。我们有两个主要问题...

1) 当我们 select 第一根蜡烛并将其提交到购物车时,它会更新价格。然而,当我们尝试向购物车中添加另一根蜡烛时,它不会更新价格。

2) 当我们在 select 从购物车中取出一件商品后点击删除商品按钮时,它会弄乱价格,如果我们删除所有东西,它会将总数设置为负数。

下面是我们的代码,如果有任何帮助,我将不胜感激,我已经在网上搜索并询问了多个朋友无济于事。

这是将商品添加到购物车按钮。添加多根蜡烛时,购物车 总价 不更新:

 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

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

你这里显示的是总价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)

不过,这是原始运费计算。运费应始终根据购物车中剩余的商品、重量、尺寸等计算。一旦您删除商品(价格 + 税)- 重新计算运费并将其添加到单独保存的价格中。