Autosave: 20260313-193504
This commit is contained in:
parent
fc0fcfb24f
commit
fe4c03a06a
Binary file not shown.
Binary file not shown.
@ -31,7 +31,10 @@
|
||||
</table>
|
||||
<div class="mt-3">
|
||||
<h4>Total: {{ total_price }} ETB</h4>
|
||||
<a href="#" class="btn btn-primary">Proceed to Checkout</a>
|
||||
<form method="post" action="{% url 'checkout' %}">
|
||||
{% csrf_token %}
|
||||
<button type="submit" class="btn btn-primary">Proceed to Checkout</button>
|
||||
</form>
|
||||
</div>
|
||||
{% else %}
|
||||
<p>Your cart is empty.</p>
|
||||
|
||||
@ -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/<int:pk>/", add_to_cart, name="add_to_cart"),
|
||||
path("cart/remove/<int:pk>/", remove_from_cart, name="remove_from_cart"),
|
||||
path("checkout/", checkout, name="checkout"),
|
||||
]
|
||||
@ -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")
|
||||
Loading…
x
Reference in New Issue
Block a user