def make_order_view(request): categories = Category.objects.all() 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) form = OrderForm(request.POST or None) if form.is_valid(): 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'] new_order = Order.objects.create(user=request.user, items=cart, total=cart.cart_total, first_name=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('thank_you')) return render(request, 'order/order.html', {'categories': categories})
def remove_from_cart_view(request): 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) slug = request.GET.get('slug') product = Product.objects.get(slug=slug) cart.remove_from_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': cart.items.count(), 'cart_total_price': cart.cart_total })
def checkout_view(request): categories = Category.objects.all() 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) context = {'cart': cart, 'categories': categories} return render(request, 'order/checkout.html', context)
def order_create_view(request): categories = Category.objects.all() 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) form = OrderForm(request.POST or None) context = {'form': form, 'cart': cart, 'categories': categories} return render(request, 'order/order.html', context)
def change_item_qty(request): 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) qty = request.GET.get('qty') item_id = request.GET.get('item_id') cart.change_qty(qty, item_id) cart_item = CartItem.objects.get(id=int(item_id)) return JsonResponse({ 'cart_total': cart.items.count(), 'item_total': cart_item.item_total, 'cart_total_price': cart.cart_total })