diff --git a/core/__pycache__/urls.cpython-311.pyc b/core/__pycache__/urls.cpython-311.pyc index 3518efb..2e2dd18 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 4fc467c..c28a742 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 new file mode 100644 index 0000000..5587fcc --- /dev/null +++ b/core/templates/core/cart.html @@ -0,0 +1,44 @@ +{% extends "base.html" %} + +{% block title %}Shopping Cart | Ethio-gebeya{% endblock %} + +{% block content %} +
+

Shopping Cart

+ + {% if items %} + + + + + + + + + + + + {% for item in items %} + + + + + + + + {% endfor %} + +
ProductQuantityPriceSubtotalAction
{{ item.product.name }}{{ item.quantity }}{{ item.product.price }} ETB{{ item.subtotal }} ETBRemove
+
+

Total: {{ total_price }} ETB

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

Your cart is empty.

+ {% endif %} + +
+ Continue Shopping +
+
+{% endblock %} diff --git a/core/templates/core/index.html b/core/templates/core/index.html index 1d9f8c7..6b5a5d3 100644 --- a/core/templates/core/index.html +++ b/core/templates/core/index.html @@ -15,6 +15,7 @@

{{ product.description|truncatewords:20 }}

Price: {{ product.price }} ETB

View Details + Add to Cart @@ -27,6 +28,7 @@ {% else %} Sign In {% endif %} + View Cart -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/core/templates/core/product_detail.html b/core/templates/core/product_detail.html index e2fa822..3a4e041 100644 --- a/core/templates/core/product_detail.html +++ b/core/templates/core/product_detail.html @@ -11,13 +11,11 @@

Price: {{ product.price }} ETB

Stock: {{ product.stock }}

-
- {% csrf_token %} - -
+ Add to Cart Back to catalog + View Cart -{% endblock %} +{% endblock %} \ No newline at end of file diff --git a/core/urls.py b/core/urls.py index 1a873df..4b180d5 100644 --- a/core/urls.py +++ b/core/urls.py @@ -1,7 +1,16 @@ from django.urls import path from django.contrib.auth.views import LoginView, LogoutView -from .views import buyer_dashboard, dashboard_redirect, home, product_detail, seller_dashboard +from .views import ( + add_to_cart, + buyer_dashboard, + cart_view, + dashboard_redirect, + home, + product_detail, + remove_from_cart, + seller_dashboard, +) urlpatterns = [ path("", home, name="home"), @@ -11,4 +20,7 @@ urlpatterns = [ path("dashboard/", dashboard_redirect, name="dashboard"), path("dashboard/buyer/", buyer_dashboard, name="buyer_dashboard"), path("dashboard/seller/", seller_dashboard, name="seller_dashboard"), -] \ No newline at end of file + 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"), +] diff --git a/core/views.py b/core/views.py index 6c06c6d..f2dc7d0 100644 --- a/core/views.py +++ b/core/views.py @@ -87,4 +87,37 @@ def seller_dashboard(request): "total_revenue": sales_summary["total_revenue"] or 0, "recent_sales": recent_sales, } - return render(request, "core/seller_dashboard.html", context) \ No newline at end of file + return render(request, "core/seller_dashboard.html", context) + + +def add_to_cart(request, pk): + product = get_object_or_404(Product, pk=pk) + cart = request.session.get("cart", {}) + product_id = str(pk) + cart[product_id] = cart.get(product_id, 0) + 1 + request.session["cart"] = cart + return redirect("home") + + +def remove_from_cart(request, pk): + cart = request.session.get("cart", {}) + product_id = str(pk) + if product_id in cart: + del cart[product_id] + request.session["cart"] = cart + return redirect("cart_view") + + +def cart_view(request): + cart = request.session.get("cart", {}) + items = [] + total_price = 0 + for product_id, quantity in cart.items(): + try: + product = Product.objects.get(pk=product_id) + items.append({"product": product, "quantity": quantity, "subtotal": product.price * quantity}) + total_price += product.price * quantity + except Product.DoesNotExist: + continue + context = {"items": items, "total_price": total_price} + return render(request, "core/cart.html", context)