feat: order creation on cart detail view

This commit is contained in:
2025-01-03 12:18:08 +01:00
parent 10b411ba33
commit 340c0d0508
9 changed files with 473 additions and 47 deletions
+14 -8
View File
@@ -91,12 +91,18 @@ def cart(request, *args, **kwargs):
total = base_total + tax_total
billing_addresses = CustomerAddress.objects.filter(
user=request.user, address_type=CustomerAddress.Types.BILLING
)
shipping_addresses = CustomerAddress.objects.filter(
user=request.user, address_type=CustomerAddress.Types.SHIPPING
)
if request.user.is_authenticated:
billing_address = CustomerAddress.objects.filter(
user=request.user, address_type=CustomerAddress.Types.BILLING, default=True
).first()
shipping_address = CustomerAddress.objects.filter(
user=request.user,
address_type=CustomerAddress.Types.SHIPPING,
).first()
else:
billing_address = None
shipping_address = None
shipping_methods = ShippingMethod.objects.all()
response = render(
@@ -108,8 +114,8 @@ def cart(request, *args, **kwargs):
"total": total,
"base_total": base_total,
"tax_total": tax_total,
"billing_addresses": billing_addresses,
"shipping_addresses": shipping_addresses,
"billing_address": billing_address,
"shipping_address": shipping_address,
"shipping_methods": shipping_methods,
},
)
+44 -7
View File
@@ -11,6 +11,8 @@ from shop.models import (
WishlistedProduct,
CustomerAddress,
ShippingMethod,
Order,
OrderLine,
)
from shop.utils import create_order_from_cart
from web.forms import CreateOrderForm
@@ -88,24 +90,58 @@ class CartDetail(TemplateView, CreateView):
context.update({"errors": form.errors})
return self.render_to_response(context)
billing_address_id = form.cleaned_data.get("billing_address")
shipping_address_id = form.cleaned_data.get("shipping_address")
shipping_method_id = form.cleaned_data.get("shipping_method")
cart, created = get_or_create_cart(request)
billing_address = get_object_or_404(CustomerAddress, pk=billing_address_id)
shipping_address = get_object_or_404(CustomerAddress, pk=shipping_address_id)
billing_address_full_name = form.cleaned_data.get('billing_address_full_name')
billing_address_address = form.cleaned_data.get('billing_address')
billing_address_town = form.cleaned_data.get('billing_address_town')
billing_address_state = form.cleaned_data.get('billing_address_state')
billing_address_country = form.cleaned_data.get('billing_address_country')
billing_address_zip = form.cleaned_data.get('billing_address_zip')
shipping_address_full_name = form.cleaned_data.get('shipping_address_full_name')
shipping_address_address = form.cleaned_data.get('shipping_address')
shipping_address_town = form.cleaned_data.get('shipping_address_town')
shipping_address_state = form.cleaned_data.get('shipping_address_state')
shipping_address_country = form.cleaned_data.get('shipping_address_country')
shipping_address_zip = form.cleaned_data.get('shipping_address_zip')
shipping_address_phone = form.cleaned_data.get('shipping_address_phone')
shipping_method = get_object_or_404(ShippingMethod, pk=shipping_method_id)
order = create_order_from_cart(
cart=cart,
billing_address=billing_address,
shipping_address=shipping_address,
shipping_method=shipping_method,
billing_address_full_name=billing_address_full_name,
billing_address_address=billing_address_address,
billing_address_town=billing_address_town,
billing_address_state=billing_address_state,
billing_address_country=billing_address_country,
billing_address_zip=billing_address_zip,
shipping_address_full_name=shipping_address_full_name,
shipping_address_address=shipping_address_address,
shipping_address_town=shipping_address_town,
shipping_address_state=shipping_address_state,
shipping_address_country=shipping_address_country,
shipping_address_zip=shipping_address_zip,
shipping_address_phone=shipping_address_phone,
)
return redirect(reverse("web:order", kwargs={"pk": order.pk}))
return redirect(reverse("web:order", kwargs={"uuid": order.uuid}))
class OrderDetail(TemplateView):
template_name = "web/order.html"
def get_context_data(self, **kwargs):
order = get_object_or_404(Order, uuid=kwargs.get("uuid"))
items = OrderLine.objects.filter(order=order)
return {
"order": order,
"items": items,
}
class WishlistView(TemplateView):
@@ -237,4 +273,5 @@ index = IndexView.as_view()
category_view = CategoryView.as_view()
product_detail = ProductDetail.as_view()
cart_detail = CartDetail.as_view()
order_detail = OrderDetail.as_view()
wishlist = WishlistView.as_view()