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): 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()
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})