From b13c715eed59a703fa3fc65d496d129b31abae75 Mon Sep 17 00:00:00 2001 From: vishnuvardhancoder Date: Fri, 18 Oct 2024 12:31:37 +0530 Subject: [PATCH] updated cart functionality. --- products/templates/cart.html | 37 ++++++++++++++++++ products/templates/index.html | 74 ++++++++++++++++++++++++----------- products/urls.py | 11 +++--- products/views.py | 38 ++++++++++++++++++ templates/Cart.html | 37 ++++++++++++++++++ templates/base.html | 7 ++-- 6 files changed, 174 insertions(+), 30 deletions(-) create mode 100644 products/templates/cart.html create mode 100644 templates/Cart.html diff --git a/products/templates/cart.html b/products/templates/cart.html new file mode 100644 index 0000000..251e0e9 --- /dev/null +++ b/products/templates/cart.html @@ -0,0 +1,37 @@ +{% extends 'base.html' %} + +{% block content %} +
+

Your Cart

+ {% if cart_items %} + + + + + + + + + + + + {% for item in cart_items %} + + + + + + + + {% endfor %} + +
ProductQuantityPriceTotalAction
{{ item.product.name }}{{ item.quantity }}${{ item.product.price }}${{ item.total_price }} + Remove +
+

Total: ${{ total_amount }}

+ Proceed to Checkout + {% else %} +

Your cart is empty.

+ {% endif %} +
+{% endblock %} diff --git a/products/templates/index.html b/products/templates/index.html index 1f31d42..8e144cd 100644 --- a/products/templates/index.html +++ b/products/templates/index.html @@ -1,28 +1,58 @@ {% extends 'base.html' %} {% block content %} -
-
-

All Products

-

This is a modified jumbotron that occupies the entire horizontal space of its parent.

-
-
+
+
+

All Products

+

This is a modified jumbotron that occupies the entire horizontal space of its parent.

+
+
-
-
- {% for product in products %} -
-
-
-
-
{{ product.name }} - ${{ product.price }}
-

Some quick example text to build on the card title and make up the bulk of the - card's content.

- Add to cart -
-
+
+
+ {% for product in products %} +
+
+
- {% endfor %} +
+
{{ product.name }} - ${{ product.price }}
+

Some quick example text to build on the card title and make up the bulk of the + card's content.

+ +
+
-
-{% endblock %} \ No newline at end of file + {% endfor %} +
+
+ + + +{% endblock %} \ No newline at end of file diff --git a/products/urls.py b/products/urls.py index 0b96a45..3367e11 100644 --- a/products/urls.py +++ b/products/urls.py @@ -1,8 +1,9 @@ from django.urls import path -from . import views - +from .views import index, new, add_to_cart, cart urlpatterns = [ - path('', views.index), - path('new/', views.new) -] \ No newline at end of file + path('', index, name='index'), + path('new/', new, name='new'), + path('add-to-cart//', add_to_cart, name='add-to-cart'), + path('cart/', cart, name='cart'), +] diff --git a/products/views.py b/products/views.py index 4aa7037..1ff3dcb 100644 --- a/products/views.py +++ b/products/views.py @@ -1,6 +1,9 @@ from django.http import HttpResponse +from django.http import JsonResponse from django.shortcuts import render from .models import Product +from django.shortcuts import redirect +from .models import Product # Create your views here. @@ -14,3 +17,38 @@ def index(request): def new(request): return HttpResponse('Welcome to PyShop New Arrivals') +# views.py + + +def add_to_cart(request, product_id): + if request.headers.get('x-requested-with') == 'XMLHttpRequest': + cart = request.session.get('cart', {}) + cart[product_id] = cart.get(product_id, 0) + 1 # Increment quantity + request.session['cart'] = cart + return JsonResponse({'success': True, 'message': 'Product added to cart'}) + else: + return redirect('index') # Redirect to index if not an AJAX request + + +def cart(request): + cart = request.session.get('cart', {}) + cart_items = [] + + for product_id, quantity in cart.items(): + try: + product = Product.objects.get(id=product_id) + cart_items.append({ + 'product': product, + 'quantity': quantity, + 'total_price': product.price * quantity + }) + except Product.DoesNotExist: + pass + + # Calculate the total amount for the cart + total_amount = sum(item['total_price'] for item in cart_items) + + return render(request, 'cart.html', { + 'cart_items': cart_items, + 'total_amount': total_amount + }) diff --git a/templates/Cart.html b/templates/Cart.html new file mode 100644 index 0000000..251e0e9 --- /dev/null +++ b/templates/Cart.html @@ -0,0 +1,37 @@ +{% extends 'base.html' %} + +{% block content %} +
+

Your Cart

+ {% if cart_items %} + + + + + + + + + + + + {% for item in cart_items %} + + + + + + + + {% endfor %} + +
ProductQuantityPriceTotalAction
{{ item.product.name }}{{ item.quantity }}${{ item.product.price }}${{ item.total_price }} + Remove +
+

Total: ${{ total_amount }}

+ Proceed to Checkout + {% else %} +

Your cart is empty.

+ {% endif %} +
+{% endblock %} diff --git a/templates/base.html b/templates/base.html index be2b497..fad9913 100644 --- a/templates/base.html +++ b/templates/base.html @@ -28,6 +28,9 @@ +
@@ -38,7 +41,6 @@
{% block content %} - {% endblock %}
@@ -48,7 +50,6 @@ Back to top

PyShop is a © of Thisishaykins, but please download and customize it for yourself!

-
@@ -57,4 +58,4 @@ - \ No newline at end of file +