def checkout_view(request): try: cart_id = request.session['cart_id'] cart = Cart.object.get(id=cart_id) request.session['total'] = CartItem.object.count() except: cart = Cart() cart.save() cart_id = cart.id request.session['cart_id'] = cart_id cart = Cart.object.get(id=cart_id) new_cart_total = 0.00 for item in cart.items.all(): new_cart_total += float(item.item_total) cart.cart_total = new_cart_total cart.save form = OrderForms(request.POST or None) if form.is_valid(): first_name = form.cleaned_data['name'] last_name = form.cleaned_data['last_name'] phone = form.cleaned_data['phone'] buying_type = form.cleaned_data['buying_type'] address = form.cleaned_data['address'] comments = form.cleaned_data['comments'] print(buying_type) new_order = Order.object.create( user = request.user, items = cart, total = cart.cart_total, first_name = first_name, last_name = last_name, phone = phone, address = address, buying_type = buying_type, comments = comments, ) del request.session['cart_id'] del request.session['total'] return HttpResponseRedirect(reverse('thanks')) context = { 'cart': cart, 'cart_total':CartItem.object.count(), 'form': form } return render(request, 'checkout.html', context)
def add_to_cart_view(request): try: cart_id = request.session['cart_id'] cart = Cart.object.get(id=cart_id) request.session['total'] = CartItem.object.count() except: cart = Cart() cart.save() cart_id = cart.id request.session['cart_id'] = cart_id cart = Cart.object.get(id=cart_id) product_slug = request.GET.get('product_slug') product = Product.object.get(slug=product_slug) cart.add_to_cart(product.slug) new_cart_total = 0.00 for item in cart.items.all(): new_cart_total += float(item.item_total) cart.cart_total = new_cart_total cart.save return JsonResponse({'cart_total':CartItem.object.count(), 'cart_total_price': cart.cart_total,})
def cart_view(request): try: cart_id = request.session['cart_id'] cart = Cart.object.get(id=cart_id) request.session['total'] = CartItem.object.count() except: cart = Cart() cart.save() cart_id = cart.id request.session['cart_id'] = cart_id cart = Cart.object.get(id=cart_id) new_cart_total = 0.00 for item in cart.items.all(): new_cart_total += float(item.item_total) cart.cart_total = new_cart_total cart.save context = { 'cart': cart, 'cart_total':CartItem.object.count() } return render(request, 'cart.html', context)
def remove_from_cart(request): '''Delete product from cart''' try: cart_id = request.session['cart_id'] cart = Cart.objects.get(id=cart_id) request.session['total'] = cart.items.count() except: cart = Cart() cart.save() cart_id = cart.id request.session['cart_id'] = cart_id cart = Cart.objects.get(id=cart_id) product_slug = request.GET.get('product_slug') product = Product.objects.get(slug=product_slug) cart.remove_from_cart_mod(product.slug) value_cart_total = 0.00 for item in cart.items.all(): value_cart_total += float(item.item_total) cart.cart_total = value_cart_total cart.save() return JsonResponse({ 'cart_total': cart.items.count(), 'cart_total_final': cart.cart_total, })