def add_to_cart(request): """ function that takes a POST request and adds a product instance to the current customer's shopping cart """ postdata = request.POST.copy() # get product slug from post data, return blank if empty product_slug = postdata.get('product_slug', '') # get quantity added, return 1 if empty quantity = postdata.get('quantity', 1) # fetch the product or return a missing page error p = get_object_or_404(Product, slug=product_slug) # get products in cart cart_products = get_cart_items(request) product_in_cart = False # check to see if item is already in cart for cart_item in cart_products: if cart_item.product.id == p.id: # update the quantity if found cart_item.augment_quantity(quantity) product_in_cart = True if not product_in_cart: # create and save a new cart item ci = CartItem() ci.product = p ci.quantity = quantity ci.cart_id = _cart_id(request) ci.save()
def add_to_cart(request): """ function that takes a POST request and adds a product instance to the current customer's shopping cart """ postdata = request.POST.copy() # get product slug from post data, return blank if empty product_slug = postdata.get('product_slug','') # get quantity added, return 1 if empty quantity = postdata.get('quantity',1) # fetch the product or return a missing page error p = get_object_or_404(Product, slug=product_slug) #get products in cart cart_products = get_cart_items(request) product_in_cart = False # check to see if item is already in cart for cart_item in cart_products: if cart_item.product.id == p.id: # update the quantity if found cart_item.augment_quantity(quantity) product_in_cart = True if not product_in_cart: # create and save a new cart item ci = CartItem() ci.product = p ci.quantity = quantity ci.cart_id = _cart_id(request) ci.save() print("AGGGGG") request.session['items_total'] = get_total_number(request) print(request.session['items_total']) print("DANK MEMES")
def add_orderitem_to_cart(orderitem, request): quantity = orderitem.quantity p = get_object_or_404(Product, slug=orderitem.product.slug) cart_products = get_cart_items(request) product_in_cart = False for cart_item in cart_products: if cart_item.product.id == p.id: cart_item.augment_quantity(quantity) product_in_cart = True if not product_in_cart: ci = CartItem() ci.product = p ci.quantity = quantity ci.cart_id = _cart_id(request) ci.save()
def cart_add(request, restaurant_slug, meal_slug, size, template_name="cart.html"): nameRestaurant = Restaurant.objects.filter(slug=restaurant_slug) meal = MenuPosition.objects.filter( slug=meal_slug, restaurant=nameRestaurant[0]) cart = Cart.objects.filter(user=request.user.id, ordered=False) if cart: size = Size.objects.filter(size=size, name=meal[0].id) ci = CartItem() ci.product = size[0] ci.quantity = 1 ci.cart_id = cart[0] ci.save() else: new_cart = Cart() new_cart.user = request.user new_cart.save() size = Size.objects.filter(size=size, name=meal[0].id) ci = CartItem() ci.product = size[0] ci.quantity = 1 ci.cart_id = new_cart ci.save() return redirect("/my_cart/")
def add_to_cart(request): postdata = request.POST.copy() product_slug = postdata.get('product_slug', '') quantity = postdata.get('quantity', 1) p = get_object_or_404(Product, slug=product_slug) cart_products = get_cart_items(request) product_in_cart = False for item in cart_products: if item.product.id == p.id: item.augment_quantity(quantity) product_in_cart = True if not product_in_cart: ci = CartItem() ci.product = p ci.quantity = quantity ci.cart_id = _cart_id(request) ci.save()
def add_to_cart(request): postdata = request.POST.copy() product_slug = postdata.get('product_slug', '') print('add_to_cart') quantity = postdata.get('quantity', 1) p = get_object_or_404(Product, slug=product_slug) cart_products = get_cart_items(request) product_in_cart = False for cart_item in cart_products: if cart_item.product.id == p.id: cart_item.augment_quantity(quantity) product_in_cart = True if not product_in_cart: ci = CartItem() ci.product = p ci.quantity = quantity ci.cart_id = _cart_id(request) ci.save()
def add_to_cart(request): postdata = request.POST.copy() product_slug = postdata.get('product_slug', '') quantity = postdata.get('quantity', 1) added_product = get_object_or_404(Product, slug=product_slug) cart_products = get_cart_items(request) product_in_cart = False for cart_item in cart_products: if cart_item.product.id == added_product.id: cart_item.augment_quantity(quantity) product_in_cart = True if not product_in_cart: new_cart_item = CartItem() new_cart_item.product = added_product new_cart_item.cart_id = _cart_id(request) new_cart_item.save()
def add_to_cart(request): postdata = request.POST.copy() product_id = postdata.get('product_id') quantity = postdata.get('quantity', 1) size_id = postdata.get('size_id', 1) p = get_object_or_404(Flower, id=product_id) s = get_object_or_404(Size, id=size_id) products = get_cart_items(request) in_cart = False for item in products: if item.product.id != p.id and item.size.id != s.id: item.augment_quantity(quantity) in_cart = True if not in_cart: item = CartItem() item.product = p item.quantity = quantity item.cart_id = _cart_id(request) item.size = s item.save()
def add_to_cart(request): postdata = request.POST.copy() # get product slug from post data, return blank if empty product_slug = postdata.get('product_slug', '') # get quantity added, return 1 if empty quantity = postdata.get('quantity', 1) # fetch the product or return a missing page error p = get_object_or_404(Product, slug = product_slug) # get products in cart cart_products = get_cart_items(request) product_in_cart = False # check to see if item is already in cart for cart_item in cart_products: if cart_item.product.id == p.id: # update the quantity if found cart_item.augment_quantity(quantity) product_in_cart = True if not product_in_cart: # create and save a new cart item ci = CartItem() ci.product = p ci.quantity = quantity ci.cart_id = _cart_id(request) ci.save()
def add_to_cart(request): postdata = request.POST.copy() # get product slug from post data, return blank if empty product_slug = postdata.get("product_slug", "") # get quantity added, return 1 if empty quantity = postdata.get("quantity", 1) # fetch the product or return a missing page error p = get_object_or_404(Product, slug=product_slug) # get products in cart cart_products = get_cart_items(request) product_in_cart = False # check to see if item is already in cart for cart_item in cart_products: if cart_item.product.id == p.id: # update the quantity if found cart_item.augment_quantity(quantity) product_in_cart = True if not product_in_cart: # create and save a new cart item ci = CartItem() ci.product = p ci.quantity = quantity ci.cart_id = _cart_id(request) ci.save()
return CartItem.objects.filter(cart_id=_cart_id(request)) #add an item to the cart def add_to_cart(request): postdata = request.POST.copy() # get product slug from post data, return blank if empty product_slug = postdata.get('product_slug', '') # get quantity added, return 1 if empty quantity = postdata.get('quantity', 1) # fetch the product or return a missing page error p = get_object_or_404(Product, slug=product_slug) #get products in cart cart_products = get_cart_items(request) product_in_cart = False # check to see if item is already in cart for cart_item in cart_products: if cart_item.product.id = p.id: # update the quantity if found cart_item.augment_quantity(quantity) product_in_cart = True if not product_in_cart: # create and save a new cart item ci = CartItem() ci.product = p ci.quantity = quantity ci.cart_id = _cart_id(request) ci.save() # returns the total number of items in the user's cart def cart_distinct_item_count(request): return get_cart_items(request).count()