diff --git a/core/__pycache__/urls.cpython-311.pyc b/core/__pycache__/urls.cpython-311.pyc index 2e2dd18..f66bd5f 100644 Binary files a/core/__pycache__/urls.cpython-311.pyc and b/core/__pycache__/urls.cpython-311.pyc differ diff --git a/core/__pycache__/views.cpython-311.pyc b/core/__pycache__/views.cpython-311.pyc index c28a742..08e8713 100644 Binary files a/core/__pycache__/views.cpython-311.pyc and b/core/__pycache__/views.cpython-311.pyc differ diff --git a/core/templates/core/cart.html b/core/templates/core/cart.html index 5587fcc..9f54e29 100644 --- a/core/templates/core/cart.html +++ b/core/templates/core/cart.html @@ -31,7 +31,10 @@

Total: {{ total_price }} ETB

- Proceed to Checkout +
+ {% csrf_token %} + +
{% else %}

Your cart is empty.

@@ -41,4 +44,4 @@ Continue Shopping -{% endblock %} +{% endblock %} \ No newline at end of file diff --git a/core/urls.py b/core/urls.py index 4b180d5..6037293 100644 --- a/core/urls.py +++ b/core/urls.py @@ -5,6 +5,7 @@ from .views import ( add_to_cart, buyer_dashboard, cart_view, + checkout, dashboard_redirect, home, product_detail, @@ -23,4 +24,5 @@ urlpatterns = [ path("cart/", cart_view, name="cart_view"), path("cart/add//", add_to_cart, name="add_to_cart"), path("cart/remove//", remove_from_cart, name="remove_from_cart"), -] + path("checkout/", checkout, name="checkout"), +] \ No newline at end of file diff --git a/core/views.py b/core/views.py index f2dc7d0..b43a46b 100644 --- a/core/views.py +++ b/core/views.py @@ -3,6 +3,7 @@ import platform from django import get_version as django_version from django.contrib.auth.decorators import login_required +from django.db import transaction from django.db.models import Count, DecimalField, ExpressionWrapper, F, Sum from django.shortcuts import get_object_or_404, redirect, render from django.utils import timezone @@ -121,3 +122,25 @@ def cart_view(request): continue context = {"items": items, "total_price": total_price} return render(request, "core/cart.html", context) + + +@login_required +@transaction.atomic +def checkout(request): + cart = request.session.get("cart", {}) + if not cart: + return redirect("cart_view") + + order = Order.objects.create(buyer=request.user) + + for product_id, quantity in cart.items(): + product = get_object_or_404(Product, pk=product_id) + OrderItem.objects.create( + order=order, + product=product, + quantity=quantity, + price=product.price + ) + + request.session["cart"] = {} + return redirect("buyer_dashboard") \ No newline at end of file