def add_to_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) product_slug = request.GET.get('product_slug') product = Product.objects.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': cart.items.count(), 'cart_total_price': cart.cart_total })
def make_order_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) form = OrderForm(request.POST or None) categories = Category.objects.all() 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 ) ''' new_order = Order() new_order.user = request.user new_order.save() new_order.items.add(cart) new_order.first_name = name new_order.last_name = last_name new_order.phone = phone new_order.address = address new_order.buying_type = buying_type new_order.comments = comments new_order.total = cart.cart_total new_order.save() del request.session['cart_id'] del request.session['total'] return render(request, 'thank_you.html', {'cart': cart}) return render(request, 'order.html', {'categories': categories})
def 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) categories = Category.objects.all() context = {'cart': cart, 'categories': categories} return render(request, 'cart.html', context)
def product_view(request, product_slug): 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 = Product.objects.get(slug=product_slug) categories = Category.objects.all() context = {'product': product, 'categories': categories, 'cart': cart} return render(request, 'product.html', context)
def order_create_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) form = OrderForm(request.POST or None) categories = Category.objects.all() context = {'form': form, 'cart': cart, 'categories': categories} return render(request, 'order.html', context)
def change_item_qty_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) 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 })