def add_to_cart(request): """ Add product to cart """ product_id = int(request.POST['product_id']) # Checking if user already has cart in session # otherwise create a new cart for the user if 'cart_id' in request.session: cart_id = int(request.session['cart_id']) cart = Cart.get_cart(cart_id) else: cart = Cart.get_cart() request.session['cart_id'] = cart.id try: quantity = int(request.POST['quantity']) if quantity > 0: cart.add_item(product_id, quantity, request.user) else: raise ValueError() except ValueError: return HttpResponseBadRequest( 'Product quantity is not correct, please enter one or more products in numbers.' ) if request.is_ajax(): default_currency = get_default_currency(request) return render(request, 'sales/cart_basket.html', { 'cart': cart, 'default_currency': default_currency }) return HttpResponseRedirect(reverse('sales_checkout_cart'))
def add_to_cart(request): """ Add product to cart """ product_id = int(request.POST['product_id']) # Checking if user already has cart in session # otherwise create a new cart for the user if 'cart_id' in request.session: cart_id = int(request.session['cart_id']) cart = Cart.get_cart(cart_id) else: cart = Cart.get_cart() request.session['cart_id'] = cart.id try: quantity = int(request.POST['quantity']) if quantity > 0: cart.add_item(product_id, quantity, request.user) else: raise ValueError() except ValueError: return HttpResponseBadRequest('Product quantity is not correct, please enter one or more products in numbers.') if request.is_ajax(): default_currency = get_default_currency(request) return render(request, 'sales/cart_basket.html', {'cart': cart, 'default_currency': default_currency}) return HttpResponseRedirect(reverse('sales_checkout_cart'))
def get(self, request, **kwargs): if ('cart_id' in request.session and 'payment_method' in request.session and CheckoutBillingView.session_address_key in request.session and CheckoutShippingView.session_address_key in request.session): cart = Cart.get_cart(int(request.session['cart_id'])) payment_method = PaymentMethod.ALL_METHODS[ request.session['payment_method']] billing_address = get_object_or_404( Address, id=int( request.session[CheckoutBillingView.session_address_key])) shipping_address = get_object_or_404( Address, id=int( request.session[CheckoutShippingView.session_address_key])) return super(CheckoutOrderView, self).get(request, cart=cart, payment_method=payment_method, billing_address=billing_address, shipping_address=shipping_address, **kwargs) return HttpResponseRedirect(reverse('sales_checkout_cart'))
def post(self, request): error = None message = None if 'cart_id' in request.session: cart_id = int(request.session['cart_id']) product_id = int(request.POST['product_id']) cart = Cart.get_cart(cart_id) try: quantity = int(request.POST['quantity']) if quantity > 0: cart.update_item(product_id, quantity) message = 'Your shopping cart has been updated.' else: raise ValueError() except ValueError: error = 'Product quantity is not correct, please enter one or more products in numbers.' else: cart = Cart() return super(CheckoutCartView, self).get(request, cart=cart, error=error, message=message)
def get(self, request, **kwargs): if ( "cart_id" in request.session and "payment_method" in request.session and CheckoutBillingView.session_address_key in request.session and CheckoutShippingView.session_address_key in request.session ): cart = Cart.get_cart(int(request.session["cart_id"])) payment_method = PaymentMethod.ALL_METHODS[request.session["payment_method"]] billing_address = get_object_or_404( Address, id=int(request.session[CheckoutBillingView.session_address_key]) ) shipping_address = get_object_or_404( Address, id=int(request.session[CheckoutShippingView.session_address_key]) ) return super(CheckoutOrderView, self).get( request, cart=cart, payment_method=payment_method, billing_address=billing_address, shipping_address=shipping_address, **kwargs ) return HttpResponseRedirect(reverse("sales_checkout_cart"))
def get(self, request): if 'cart_id' in request.session: cart_id = int(request.session['cart_id']) cart = Cart.get_cart(cart_id) else: cart = Cart() return super(CheckoutCartView, self).get(request, cart=cart)
def cart_basket(context): """ Returns cart summary """ request = context['request'] default_currency = context['default_currency'] if 'cart_id' in request.session: cart = Cart.get_cart(int(request.session['cart_id'])) else: cart = Cart() return {'cart': cart, 'default_currency': default_currency, 'MEDIA_URL': settings.MEDIA_URL }
def remove_all_from_cart(request): """ Remove all products from cart """ if request.method == 'POST': if 'cart_id' in request.session: cart_id = int(request.session['cart_id']) cart = Cart.get_cart(cart_id) cart.remove_all_items() else: cart = Cart() if request.is_ajax(): default_currency = get_default_currency(request) return render(request, 'sales/cart_basket.html', {'cart': cart, 'default_currency': default_currency}) return HttpResponseRedirect(reverse('sales_checkout_cart'))
def cart_basket(context): """ Returns cart summary """ request = context['request'] default_currency = context['default_currency'] if 'cart_id' in request.session: cart = Cart.get_cart(int(request.session['cart_id'])) else: cart = Cart() return { 'cart': cart, 'default_currency': default_currency, 'MEDIA_URL': settings.MEDIA_URL }
def remove_all_from_cart(request): """ Remove all products from cart """ if request.method == "POST": if "cart_id" in request.session: cart_id = int(request.session["cart_id"]) cart = Cart.get_cart(cart_id) cart.remove_all_items() else: cart = Cart() if request.is_ajax(): default_currency = get_default_currency(request) return render(request, "sales/cart_basket.html", {"cart": cart, "default_currency": default_currency}) return HttpResponseRedirect(reverse("sales_checkout_cart"))
def remove_from_cart(request): """ Remove product from cart """ product_id = int(request.POST['product_id']) # Checking if user session has cart or session may already flushed # Cart an empty cart for user if 'cart_id' in request.session: cart_id = int(request.session['cart_id']) cart = Cart.get_cart(cart_id) cart.remove_item(product_id) else: cart = Cart() if request.is_ajax(): default_currency = get_default_currency(request) return render(request, 'sales/cart_basket.html', {'cart': cart, 'default_currency': default_currency}) return HttpResponseRedirect(reverse('sales_checkout_cart'))