def shop_cart_add(request, proid): # if this is a POST request we need to process the form data url = request.META.get('HTTP_REFERER') # Bir önceki adresi alır # return HttpResponse(url) form = ShopCartForm(request.POST or None) if request.method == 'POST': # check whether it's valid: if form.is_valid(): current_user = request.user # Get User id quantity = form.cleaned_data[ 'quantity'] #get product quantity from form # Checking product in ShopCart try: q1 = ShopCart.objects.get(user_id=current_user.id, product_id=proid) except ShopCart.DoesNotExist: q1 = None form_1 = CartAddProductForm(request.POST) if q1 != None: # Update quantity to exist product quantity q1.quantity = q1.quantity + quantity q1.save() elif form_1.is_valid() and form_1.cleaned_data['update']: q1.quantity = int(form_1.cleaned_data['quantity']) q1.save() else: # Add new item to shop cart data = ShopCart(user_id=current_user.id, product_id=proid, quantity=quantity) data.save() request.session['cart_items'] = ShopCart.objects.filter( user_id=current_user.id).count() #Count item in shop cart messages.success(request, "Product added to cart.. ") return HttpResponseRedirect(url) return HttpResponseRedirect(reverse('product', args=[proid]))
def addtoshopcart(request, id): url = request.META.get('HTTP_REFERER') # get last url current_user = request.user # Access User Session information product = Product.objects.get(pk=id) if product.variant != 'None': variantid = request.POST.get('variantid') # from variant add to cart checkinvariant = ShopCart.objects.filter( variant_id=variantid, user_id=current_user.id) # Check product in shopcart if checkinvariant: control = 1 # The product is in the cart else: control = 0 # The product is not in the cart""" else: variantid = request.POST.get('variantid') checkinproduct = ShopCart.objects.filter( product_id=id, user_id=current_user.id) # Check product in shopcart if checkinproduct: control = 1 # The product is in the cart else: control = 0 # The product is not in the cart""" if request.method == 'POST': # if there is a post form = ShopCartForm(request.POST) if form.is_valid(): if control == 1: # Update shopcart if product.variant == 'None': data = ShopCart.objects.get(product_id=id, user_id=current_user.id) else: data = ShopCart.objects.get(product_id=id, variant_id=variantid, user_id=current_user.id) data.quantity = form.cleaned_data['quantity'] data.save() # save data else: # Insert to Shopcart data = ShopCart() data.user_id = current_user.id data.product_id = id data.variant_id = variantid data.quantity = form.cleaned_data['quantity'] print('Yes reached') data.save() messages.success(request, "Product added to Shopcart ") return HttpResponseRedirect(url) else: # if there is no post if control == 1: # Update shopcart if product.variant == 'None': data = ShopCart.objects.get(product_id=id, user_id=current_user.id) else: v = Variants.objects.get(product_id=id, price=product.price) try: data = ShopCart.objects.get(user_id=current_user.id, product_id=id, variant_id=v.id) except: data = ShopCart() # model ile bağlantı kur data.user_id = current_user.id data.product_id = id data.quantity = 0 v = Variants.objects.get(product_id=id, price=product.price) data.variant_id = v.id data.quantity += 1 data.save() # else: # Inser to Shopcart data = ShopCart() # model ile bağlantı kur data.user_id = current_user.id data.product_id = id data.quantity = 1 if product.variant == 'None': data.variant_id = None else: v = Variants.objects.get(product_id=id, price=product.price) data.variant_id = v.id data.save() # messages.success(request, "Product added to Shopcart") return HttpResponseRedirect(url)