def add_to_cart(request, pk): item = get_object_or_404(Product, pk=pk) print("Item") print(item) order_item = Cart.objects.get_or_create(item=item, user=request.user, purchased=False) print("Order Item Object:") print(order_item) print(order_item[0]) order_qs = Order.objects.filter(user=request.user, ordered=False) print("Order Qs:") print(order_qs) #print(order_qs[0]) if order_qs.exists(): order = order_qs[0] print("If Order exist") print(order) if order.orderitems.filter(item=item).exists(): order_item[0].quantity += 1 order_item[0].save() messages.info(request, "This item quantity was updated.") return redirect("App_Shop:home") else: order.orderitems.add(order_item[0]) messages.info(request, "This item was added to your cart.") return redirect("App_Shop:home") else: order = Order(user=request.user) order.save() order.orderitems.add(order_item[0]) messages.info(request, "This item was added to your cart.") return redirect("App_Shop:home")
def add_to_cart(request, pk): item = get_object_or_404(Product, pk=pk) print("item :" + str(item)) # this will also make a list if the product is selected before # idexing order_item = Cart.objects.get_or_create(item=item, user=request.user, purchased=False) print("order Item :" + str(order_item)) # get the order object that is yet not paid # and try to add the cart in it # other wise create it # it will return a list even it is only one # so make indexing order_qs = Order.objects.filter(user=request.user, ordered=False) ## chek if you even create any unpaid order yet if order_qs.exists(): order = order_qs[0] print("Order Exists adding to it") print(order) if order.orderitems.filter(item=item).exists(): # the product exists in the order # so increase the cart # orderitem[0] is the cart that is matched because it return a list even # it is only one element # we checked all the cart in a order to seach the duplicate # then increase it # we are increasing the cart order_item[0].quantity += 1 order_item[0].save() messages.info(request, "This Item Quantity was updated") return redirect("App_Shop:home") else: ## add to the cart that belongs ## order object that has (ordered=False) order.orderitems.add(order_item[0]) messages.info(request, "This item is added to the cart") return redirect("App_Shop:home") else: ## first time ## never created a order ## so create the order object then add the cart order = Order(user=request.user) order.save() order.orderitems.add(order_item[0]) messages.info(request, "Item added To the Cart") return redirect('App_Shop:home')
def add_to_cart(request, pk): # Retrieving the item item = get_object_or_404(Product, pk=pk) print("Item") print(item) # Running the query if item is in cart order_item_qs = Cart.objects.get_or_create( item=item, user=request.user, purchased=False) # Retrieving the item from query order_item = order_item_qs[0] print("Order Item Object:") print(order_item_qs) print(order_item_qs[0]) # Running a query if there is an order that was created order_qs = Order.objects.filter(user=request.user, ordered=False) print("Order Qs:") print(order_qs) # Checking if there is a order that exists or doesnt if order_qs.exists(): # Retrieving the order from query bcz it exists order = order_qs[0] print(order_qs[0]) print("If Order exist") print(order) # Checking if the item is in already in orderList or not if order.orderitems.filter(item=item).exists(): # Incresing item's quantity bcz its already in orderList and save it. order_item.quantity += 1 order_item.save() messages.info(request, "This item quantity was updated.") return redirect("App_Shop:product_detail", pk=item.pk) else: # Adding the item in orderList bcz it doesnt exist yet order.orderitems.add(order_item) messages.info(request, "This item was added to your cart.") return redirect("App_Shop:product_detail", pk=item.pk) else: # There is no ORDER! so CREATE and SAVE IT! order = Order(user=request.user) order.save() # And then add the item in orderList order.orderitems.add(order_item) messages.info(request, "This item was added to your cart.") return redirect("App_Shop:product_detail", pk=item.pk)
def add_to_cart(request, pk): item = get_object_or_404(Product, pk=pk) order_item = Cart.objects.get_or_create(item=item, user=request.user, purchased=False) order_qs = Order.objects.filter(user=request.user, ordered=False) if order_qs.exists(): order = order_qs[0] if order.orderitems.filter(item=item).exists(): order_item[0].quantity += 1 order_item[0].save() messages.info(request, "This item quantity is updated") return redirect("App_Shop:home") else: order.orderitems.add(order_item[0]) messages.info(request, "This item is addedd to your cart") return redirect("App_Shop:home") else: order = Order(user=request.user) order.save() order.orderitems.add(order_item[0]) messages.info(request, "This item is added to your cart") return redirect("App_Shop:home")
def add_to_cart(request, pk): item = get_object_or_404(Product, pk=pk) order_item = Cart.objects.get_or_create(item=item, user=request.user, purchased=False) order_qs = Order.objects.filter(user=request.user, ordered=False) if order_qs.exists(): order = order_qs[0] if order.orderItems.filter(item=item).exists(): order_item[0].quantity += 1 order_item[0].save() messages.info(request, f'{item.name} quantity has been updated') return redirect('App_Shop:home') else: order.orderItems.add(order_item[0]) messages.info(request, f'{item.name} has been added to your cart') return redirect('App_Shop:home') else: order = Order(user=request.user) order.save() order.orderItems.add(order_item[0]) messages.info(request, f'{item.name} has been added to your cart') return redirect('App_Shop:home')