def get(self, request, *args, **kwargs ): get_data = super(CheckoutView, self).get(request, *args, **kwargs) cart = self.get_object() user_checkout_id = request.session.get("user_checkout_id") if user_checkout_id != None: user_checkout = UserCheckout.objects.get(id=user_checkout_id) billing_address_id = request.session.get("billing_address_id") shipping_address_id = request.session.get("shipping_address_id") if billing_address_id == None or shipping_address_id == None: return redirect('order_address') else: billing_address = UserAddress.objects.get(id=billing_address_id) shipping_address = UserAddress.objects.get(id=shipping_address_id) try: new_order_id = request.session['order_id'] new_order = Order.objects.get(id=new_order_id) except: new_order = Order() request.session["order_id"] = new_order.id # new_order.cart = cart new_order.user = user_checkout new_order.billing_address = billing_address new_order.shipping_address = shipping_address new_order.save() return get_data
def get(self, request, *args, **kwargs): get_data = super(CheckoutView, self).get(request, *args, **kwargs) cart = self.get_object() user_checkout_id = request.session.get("user_checkout_id") if user_checkout_id != None: user_checkout = UserCheckout.objects.get(id=user_checkout_id) # user=user_checkout billing_address_id = request.session.get("billing_address_id") shipping_address_id = request.session.get("shipping_address_id") print(billing_address_id) print(shipping_address_id) if billing_address_id == None or shipping_address_id == None: return redirect("order_address") else: billing_address = UserAddress.objects.get( id=billing_address_id) shipping_address = UserAddress.objects.get( id=shipping_address_id) try: new_order_id = request.session["order_id"] new_order = Order.objects.get(id=new_order_id) except: new_order = Order() request.session["order_id"] = new_order.id # new_order=Order() new_order.cart = cart new_order.user = user_checkout new_order.billing_address = billing_address new_order.shipping_address = shipping_address new_order.save() return get_data
def checkout(request): cart = Cart(request) if len(cart) == 0 or not cart: messages.warning(request, _("Your cart is empty")) return redirect("items:home") user = request.user def_billing_address = ( def_shipping_address ) = shipping_initial = billing_initial = None if request.user.is_authenticated: try: def_billing_address = Address.objects.default_billing(user)[0] def_shipping_address = Address.objects.default_shipping(user)[0] shipping_initial = def_shipping_address.__dict__ billing_initial = def_billing_address.__dict__ except (ObjectDoesNotExist, IndexError): pass if request.method == "POST": billing_form = BillingAddressForm( request.POST, prefix="billing", user=user, initial=billing_initial ) shipping_form = ShippingAddressForm( request.POST, prefix="shipping", user=user, initial=shipping_initial ) if billing_form.is_valid() and shipping_form.is_valid(): billing = billing_form.save(commit=False) shipping = shipping_form.save(commit=False) order = Order() if user.is_authenticated: billing.user = shipping.user = order.user = user if def_billing_address and not billing_form.has_changed(): order.billing_address = def_billing_address else: billing.save() order.billing_address = billing if def_shipping_address and not shipping_form.has_changed(): order.shipping_address = def_shipping_address else: shipping.save() order.shipping_address = shipping order.session = request.session.session_key if cart.coupon: order.coupon = cart.coupon order.discount = cart.get_discount() order.save() for item in cart: OrderItem.objects.create( order=order, item=item["item"], price=item["price"], quantity=item["quantity"], size=item["size"], ) return redirect("payments:order_payment", order_id=order.id) else: coupon_form = CouponForm() context = { "cart": cart, "shipping_form": ShippingAddressForm( prefix="shipping", initial=shipping_initial, user=user ), "billing_form": BillingAddressForm( prefix="billing", initial=billing_initial, user=user ), "coupon_form": coupon_form, } return render(request, "checkout.html", context)