mercredi 31 juillet 2019

Flask shopping cart, how can i caclulate the total price of all the items in my cart

I have successfully been able to add to basket using flask Sessions but I am having trouble with caclulating the total price of all the products. I have tried to set a varible which calculates the price using the value in the list but that doesn't work and returns the first item instead. Could someone point me in the right direction. I also have a feeling that there would be a better way of doing this is anyone is aware then could they let me know as this is my one of my flask apps and would help a lot, thanks.

@app.route("/Menu")
def menu():
    MenuItems = Menu.query.all()
    return render_template("menu.html", MenuItems=MenuItems)

@app.route("/AddToCart", methods=["POST", "GET"])
def addToCart():
    itemId = int(request.form.get("productId"))
    MenuItem = Menu.query.get(itemId)

    if MenuItem is None:
        return render_template("error.html", errorMessage="There has been an issue adding this item to your basket")

    sVars = session['cart']

    sVars.append([str(MenuItem.ItemName), float(MenuItem.ItemPrice)])
    session['cart'] = sVars

    allPrices = sum(sVars[1])

    return render_template("cart.html", cartSession=session['cart'], allPrices=allPrices)




Aucun commentaire:

Enregistrer un commentaire