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, product=None): """ function that takes a POST request and adds a product instance to the current customer's shopping cart """ post_data = request.POST.copy() # quantity = post_data.get('quantity', 1) # get quantity added, return 1 if empty quantity = 1 if not product: product_slug = post_data.get( 'product_slug', '') # get product slug from post data, return blank if empty product = get_object_or_404( Product, slug=product_slug ) # fetch the product or return a missing page error cart_products = get_cart_items(request) # get products in cart product_in_cart = False # check to see if item is already in cart for cart_item in cart_products: if cart_item.product.id == product.id: cart_item.augment_quantity( quantity) # update the quantity if found product_in_cart = True break if not product_in_cart: ci = CartItem() # create and save a new cart item ci.product = product 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 """ post_data = request.POST.copy() # get product slug from post data, return blank if empty #product_slug = post_data.get('product_slug','') # get quantity added, return 1 if empty quantity = post_data.get('quantity',1) product_id = post_data.get('product_id', 0) product = get_object_or_404(Product, pk = product_id) # fetch the product or return a missing page error 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 == product.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 = product ci.quantity = quantity ci.cart_id = _cart_id(request) ci.save()
def add_to_cart(request, failed_url="/", cart_url="/shopping-cart/"): """ Adds an item to cart """ if request.method == "POST": # form posted, get form info data = request.POST.copy() quantity = data.get('quantity','') product_slug = data.get('product_slug','') p = get_object_or_404(Product,slug=product_slug) # add item to cart try: item = CartItem.objects.get(item=p) except: item = None # if this product isnt already in the cart... if item is None: # create new Cart Item object item = CartItem(owner=request.user, item=p, quantity=quantity) item.save() else: # increase the quantity item.quantity = item.quantity + int(quantity) item.save() else: # form isnt valid return HttpResponseRedirect(failed_url) # done ! # redirect to user's cart return HttpResponseRedirect(cart_url)
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, failed_url="/", cart_url="/shopping-cart/"): """ Adds an item to cart """ if request.method == "POST": # form posted, get form info data = request.POST.copy() quantity = data.get('quantity', '') product_slug = data.get('product_slug', '') p = get_object_or_404(Product, slug=product_slug) # add item to cart try: item = CartItem.objects.get(item=p) except: item = None # if this product isnt already in the cart... if item is None: # create new Cart Item object item = CartItem(owner=request.user, item=p, quantity=quantity) item.save() else: # increase the quantity item.quantity = item.quantity + int(quantity) item.save() else: # form isnt valid return HttpResponseRedirect(failed_url) # done ! # redirect to user's cart return HttpResponseRedirect(cart_url)
def add(self, stock_item, unit_price, quantity=1): try: cart_item = CartItem.objects.get(cart=self.cart, stock_item=stock_item) cart_item.quantity = quantity cart_item.save() except CartItem.DoesNotExist: cart_item = CartItem() cart_item.cart = self.cart cart_item.stock_item = stock_item cart_item.unit_price = unit_price cart_item.quantity = quantity cart_item.save()
def add_to_cart(request): postdata = request.POST.copy() try: flavor_id = int(postdata.get('flavor_id', False)) volume_id = int(postdata.get('volume_id', False)) conc_id = int(postdata.get('conc_id', False)) assert volume_id and (volume_id in request.volumes_data.keys() ), "You are not allowed to access this page" except (ValueError, TypeError) as e: raise Http404("Sorry! Failed to add product to cart") except AssertionError as e: raise Http404(e) # get quantity added , return 1 if empty quantity = int(postdata.get('quantity', 1)) #fetch the product or return a missing page error p = get_object_or_404(ProductVariant, vol_id=volume_id, conc_id=conc_id, flavor_id=flavor_id, active=True, product_tmpl_id__type="product") # get products in cart cart_products = get_cart_items(request) # Check to see if item already in cart product_in_cart = False available_qty = get_products_availability(p.id)[str(p.id)] added = False cart_quantity = 0 for cart_item in cart_products: if cart_item.product_id == p.id: #upddate the quantity if found product_in_cart = True cart_quantity = cart_item.quantity + quantity if cart_quantity <= available_qty.get('virtual_available', 0): cart_item.augment_quantity(quantity) added = True if not product_in_cart: cart_quantity = quantity if cart_quantity <= available_qty.get('virtual_available', 0): #create and save a new cart item ci = CartItem() ci.product_id = p.id ci.quantity = quantity ci.cart_id = _cart_id(request) if request.user.is_authenticated: ci.user_id = request.user ci.save() added = True return available_qty, cart_quantity, added
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 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) id = postdata.get('product_id') # p = get_object_or_404(Product, slug = product_slug) p = get_object_or_404(Product, id=id) 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 return JsonResponse({'product in cart':product_in_cart}) if not product_in_cart: ci = CartItem() ci.product = p ci.quantity = quantity ci.cart_id = _cart_id(request) ci.save() return JsonResponse({'product in cart': product_in_cart, 'cart_id': ci.cart_id})