Пример #1
0
def add_to_cart(request, id):
    product = Product.objects.get(id=id)
    cart = request.session.get("cart", None)
    if not cart:
        cart = Cart()
        request.session["cart"] = cart
    cart.add_product(product)
    request.session["cart"] = cart
    # return view_cart(request)
    return HttpResponseRedirect("/depotapp/cart/view/")
Пример #2
0
def add_to_cart(request,id):
    product=Product.objects.get(id=id)
    cart=request.session.get("cart",None)
    if not cart:
        cart=Cart()
        request.session["cart"]=cart
    cart.add_product(product)
    request.session["cart"]=cart
    # return view_cart(request)
    return HttpResponseRedirect("/depotapp/cart/view/")
Пример #3
0
def add_to_cart(request, num=1):
    if request.method == "POST":
        food_id = request.POST['foodId']
        food = Food.objects.get(id=food_id)
        cart_session = request.session.get('cart', None)
        # Lineitem save in cart
        # session is empty
        if not cart_session:
            # 购物车中不存在物品的情况
            cart = Cart()
            li = LineItem()
            li.food = food
            li.unite_price = food.price
            li.quantity = num
            li.save()
            cart.add_product(li, num)
        else:
            cart = pickle_load(cart_session)

            has_food = False
            for li in cart.items:
                if li.food.id == food.id:
                    li.quantity += num
                    has_food = True
                    cart.total_price += food.price * num
            if not has_food:
                li = LineItem()
                li.food = food
                li.unite_price = food.price
                li.quantity = num
                li.save()
                cart.add_product(li, num)
        cart.total_price = round(cart.total_price, 2)
        data = pickle_dump(cart)
        request.session['cart'] = data
        return HttpResponse(cart.total_price)
    else:
        logger.error('Get Request to add_to_cart')
        return HttpResponse('request method err')
Пример #4
0
def add_to_cart(request, num=1):
    if request.method == "POST":
        food_id = request.POST['foodId']
        food = Food.objects.get(id=food_id)
        cart_session = request.session.get('cart', None)
        # Lineitem save in cart
        # session is empty
        if not cart_session:
            # 购物车中不存在物品的情况
            cart = Cart()
            li = LineItem()
            li.food = food
            li.unite_price = food.price
            li.quantity = num
            li.save()
            cart.add_product(li, num)
        else:
            cart = pickle_load(cart_session)

            has_food = False
            for li in cart.items:
                if li.food.id == food.id:
                    li.quantity += num
                    has_food = True
                    cart.total_price += food.price * num
            if not has_food:
                li = LineItem()
                li.food = food
                li.unite_price = food.price
                li.quantity = num
                li.save()
                cart.add_product(li, num)
        cart.total_price = round(cart.total_price, 2)
        data = pickle_dump(cart)
        request.session['cart'] = data
        return HttpResponse(cart.total_price)
    else:
        logger.error('Get Request to add_to_cart')
        return HttpResponse('request method err')