def add_cart(request, *args, **kwargs): pid = kwargs.get('id') pdt = Product.objects.filter(pk=pid).first() next_url = request.GET.get('next') print(next_url) if pdt: if Cart.objects.filter(owner=request.user, product=pdt).first(): return redirect(next_url or 'home') cart = Cart(owner=request.user, product=pdt) cart.save() return redirect(next_url or 'home')
def post(self, request, *args, **kwargs): # 获取商品id food_id = request.POST.get('id') food_num = request.POST.get('foodNum') print(food_num, type(food_num)) # 获取商品 food = Foods.objects.get(pk=food_id) # 获取用户 user = request.user # 购物车查询当前用户购物车记录 carts = Cart.objects.filter(user_id=user, c_is_select=1) # 查询该用户购物车是否有相同商品 carts = carts.filter(food_id=food) # 判断是否有该商品 if carts.exists(): # 存在该商品,数量加1 cart = carts.first() cart.c_food_num += int(food_num) cart.save() else: # 没有该商品,添加该商品 cart = Cart() cart.c_food_num = int(food_num) cart.c_is_select = 1 cart.food_id = food cart.user_id = user cart.save() return JsonResponse({'msg': '加入购物车成功'})
def add_item(request): if request.user.is_authenticated and request.is_ajax() and request.GET: try: product_id = request.GET.get('q') if len( Cart.objects.filter(product_id=product_id, user=request.user)) > 0: item = Cart.objects.filter(product_id=product_id, user=request.user) for i in item: i.quantity = int(i.quantity + 1) i.save() else: item = Cart() item.product = Products.objects.get(product_id=product_id) item.user = request.user item.quantity = 1 item.save() size = cart_count(request.user) json_response = {'cart_count': size} return JsonResponse(json_response) except: e = sys.exc_info() print(e) return HttpResponse(e) else: return redirect('user:signup')
def place_order(request, paymethod, profile_id): cart_id = random.randint(1, 100000) cartinfo = request.session['order'] cart = request.session['cart'] for key in cart: print(key) p = Products.objects.get(pk=int(key)) for product in cartinfo: print(product) if product['product_id'] == p.pk: c = Cart(user=request.user, product=p, quantity=product['quantity'], cart_id=cart_id) c.save() del request.session['cart'] #amount=request.session['amount'] #order=Order(profile=profile_id,cart_id=cart_id,total_amount=amount,payment_mode=paymethod,date=date.today()) return render(request, 'notification.html', {"order": True})
def add_to_cart(request, pk, backto): product = Product.objects.get(id=pk) if Cart.objects.filter(product=product).exists(): cart_item = Cart.objects.get(product=product) cart_item.update_count() cart_item.get_total() cart_item.save() return redirect(backto) else: cart_item = Cart(user=request.user, product=product) cart_item.get_total() cart_item.save() return redirect(backto)
def check_out_item(request, product_id): if request.user.is_authenticated: if len(Cart.objects.filter(product_id=product_id, user=request.user)) > 0: item = Cart.objects.filter(product_id=product_id, user=request.user) for i in item: i.quantity = int(i.quantity + 1) i.save() else: item = Cart() item.product = Products.objects.get(product_id=product_id) item.user = request.user item.quantity = 1 item.save() return redirect('user:check_out') return redirect('user:signup')
def add_item(request): print(request.user.is_authenticated()) print(request.is_ajax()) if request.user.is_authenticated() and request.is_ajax() and request.GET: try: product_id = request.GET.get('q') print(product_id) if len( Cart.objects.filter(product_id=product_id, user=request.user)) > 0: item = Cart.objects.filter(product_id=product_id, user=request.user) for i in item: i.quantity = int(i.quantity + 1) else: item = Cart() item.product = Products.objects.get(product_id=product_id) item.user = request.user item.quantity = 1 item.save() size = cart_count(request.user) json_response = {'cart_count': size} # perform operations on the user name. print(json_response) return JsonResponse(json_response) except: e = sys.exc_info() print(e) return HttpResponse(e) # print('add_item \n' + str(len(Cart.objects.filter(product_id=product_id, user=request.user)))) # if len(Cart.objects.filter(product_id=product_id, user=request.user)) > 0: # item = Cart.objects.filter(product_id=product_id, user=request.user) # for i in item: # i.quantity = int(i.quantity + 1) # else: # item = Cart() # item.product = Products.objects.get(product_id=product_id) # item.user = request.user # item.quantity = 1 # item.save() # print(request.get_full_path()) # return HttpResponseRedirect(request.META['HTTP_REFERER']) else: return redirect('user:signup')