def make_order_view(request): cart = cart_create(request) 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.save() del request.session['cart_id'] del request.session['total'] return HttpResponseRedirect(reverse('thank_you')) return render(request, 'sweets/order.html', {'categories': categories})
def cart_view(request): categories = Category.objects.all() cart = cart_create(request) context = { 'cart': cart, 'categories': categories } return render(request, 'sweets/cart.html', context)
def order_create_view(request): categories = Category.objects.all() cart = cart_create(request) form = OrderForm(request.POST or None) context = { 'form': form, 'cart': cart, 'categories': categories } return render(request, 'sweets/order.html', context)
def product_view(request, product_slug): cart = cart_create(request) categories = Category.objects.all() product = Product.objects.get(slug=product_slug) context = { 'product': product, 'cart': cart, 'categories': categories, } return render(request, 'sweets/product.html', context)
def add_to_cart_view(request): cart = cart_create(request) 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 remove_from_cart_view(request): cart = cart_create(request) product_slug = request.GET.get('product_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 change_item_qty(request): cart = cart_create(request) qty = request.GET.get('qty') item_id = request.GET.get('item_id') cart_item = CartItem.objects.get(id=int(item_id)) cart_item.qty = int(qty) cart_item.item_total = cart_item.qty * Decimal(cart_item.product.price) cart_item.save() 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(), 'item_total': cart_item.item_total, 'cart_total_price': cart.cart_total })
def base_view(request): categories = Category.objects.all() products = Product.objects.all() cart = cart_create(request) context = {'categories': categories, 'products': products, 'cart': cart} return render(request, 'sweets/base.html', context)