【问题标题】:Update shopping cart price with multiple items使用多个项目更新购物车价格
【发布时间】:2015-12-01 00:27:51
【问题描述】:

我一直致力于制作一个带有蜡烛购物车的 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

【问题讨论】:

  • 老兄,请不要将商品名称和价格硬编码到代码中。为产品创建一个小 dto 对象,并在一个没有 UI 代码的方法中......至少从 .xml 文件中获取它们。 List GetAllProducts() { /* 在此处读取一些 .xml */ }。我正在努力提供帮助。硬编码值是维护的噩梦。这是一个基本/基本示例:stackoverflow.com/questions/9336851/…
  • 我们没有收到您的任何反馈。请接受答案,我认为它可以解决您的问题

标签: asp.net vb.net visual-studio-2013 cart


【解决方案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)

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

【讨论】:

  • 非常感谢您,我的意思是尽快回复我道歉。你的回答很透彻,很有帮助!
猜你喜欢
  • 1970-01-01
  • 2017-04-05
  • 2012-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-06
相关资源
最近更新 更多