feat: cart detail leads to order creation
This commit is contained in:
+19
-1
@@ -4,7 +4,14 @@ from django.shortcuts import get_object_or_404, render
|
||||
from django.views.generic import TemplateView
|
||||
|
||||
from shop.filters import ProductFilter
|
||||
from shop.models import CartItem, Product, ProductPrice, WishlistedProduct
|
||||
from shop.models import (
|
||||
CartItem,
|
||||
Product,
|
||||
ProductPrice,
|
||||
WishlistedProduct,
|
||||
CustomerAddress,
|
||||
ShippingMethod,
|
||||
)
|
||||
from web.mixins import FilteredQuerysetMixin, PaginatedQuerysetMixin
|
||||
from web.settings import ANONYMOUS_CART_ID_COOKIE_NAME
|
||||
from web.utils import get_or_create_cart
|
||||
@@ -84,6 +91,14 @@ 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
|
||||
)
|
||||
shipping_methods = ShippingMethod.objects.all()
|
||||
|
||||
response = render(
|
||||
request,
|
||||
"components/cart/cart.html",
|
||||
@@ -93,6 +108,9 @@ def cart(request, *args, **kwargs):
|
||||
"total": total,
|
||||
"base_total": base_total,
|
||||
"tax_total": tax_total,
|
||||
"billing_addresses": billing_addresses,
|
||||
"shipping_addresses": shipping_addresses,
|
||||
"shipping_methods": shipping_methods,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
+41
-4
@@ -1,10 +1,19 @@
|
||||
from django.http.response import HttpResponse, JsonResponse
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.shortcuts import get_object_or_404, redirect, reverse
|
||||
from django.utils.text import gettext_lazy as _
|
||||
from django.views.decorators.http import require_http_methods
|
||||
from django.views.generic import TemplateView
|
||||
from django.views.generic import TemplateView, CreateView
|
||||
|
||||
from shop.models import CartItem, Product, ProductCategory, WishlistedProduct
|
||||
from shop.models import (
|
||||
CartItem,
|
||||
Product,
|
||||
ProductCategory,
|
||||
WishlistedProduct,
|
||||
CustomerAddress,
|
||||
ShippingMethod,
|
||||
)
|
||||
from shop.utils import create_order_from_cart
|
||||
from web.forms import CreateOrderForm
|
||||
from web.models import WebSettings
|
||||
from web.settings import ANONYMOUS_CART_ID_COOKIE_NAME
|
||||
from web.utils import get_or_create_cart
|
||||
@@ -55,8 +64,9 @@ class ProductDetail(TemplateView):
|
||||
}
|
||||
|
||||
|
||||
class CartDetail(TemplateView):
|
||||
class CartDetail(TemplateView, CreateView):
|
||||
template_name = "web/cart_detail.html"
|
||||
form_class = CreateOrderForm
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
web_settings = WebSettings.load()
|
||||
@@ -70,6 +80,33 @@ class CartDetail(TemplateView):
|
||||
"description": _("resumen del carrito"),
|
||||
}
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
form = CreateOrderForm(request.POST)
|
||||
|
||||
if not form.is_valid():
|
||||
context = self.get_context_data(**kwargs)
|
||||
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)
|
||||
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,
|
||||
)
|
||||
|
||||
return redirect(reverse("web:order", kwargs={"pk": order.pk}))
|
||||
|
||||
|
||||
class WishlistView(TemplateView):
|
||||
template_name = "web/wishlist.html"
|
||||
|
||||
Reference in New Issue
Block a user