feat: cart detail
This commit is contained in:
+36
-2
@@ -1,11 +1,14 @@
|
||||
from decimal import Decimal
|
||||
|
||||
from django.db.models import Sum
|
||||
from django.shortcuts import render
|
||||
from django.views.generic import TemplateView
|
||||
|
||||
from shop.filters import ProductFilter
|
||||
from shop.models import Product
|
||||
from shop.models import CartItem, Product, ProductPrice
|
||||
from web.mixins import FilteredQuerysetMixin, PaginatedQuerysetMixin
|
||||
from web.settings import ANONYMOUS_CART_ID_COOKIE_NAME
|
||||
from web.utils import get_or_create_cart
|
||||
from web.utils import get_cart_total, get_or_create_cart
|
||||
|
||||
|
||||
class ListProducts(TemplateView, FilteredQuerysetMixin, PaginatedQuerysetMixin):
|
||||
@@ -42,4 +45,35 @@ def cart_dropdown(request, *args, **kwargs):
|
||||
return response
|
||||
|
||||
|
||||
def cart(request, *args, **kwargs):
|
||||
cart, created = get_or_create_cart(request)
|
||||
items = CartItem.objects.filter(cart=cart)
|
||||
|
||||
base_total = Decimal("0.00")
|
||||
tax_total = Decimal("0.00")
|
||||
|
||||
for item in items:
|
||||
price = ProductPrice.objects.filter(product=item.product, current=True).first()
|
||||
base_total += price.price * item.quantity
|
||||
tax_total += round(price.price * Decimal(price.tax.value / 100), 2)
|
||||
|
||||
total = base_total + tax_total
|
||||
|
||||
response = render(
|
||||
request,
|
||||
"components/cart/cart.html",
|
||||
context={
|
||||
"cart": cart,
|
||||
"items": items,
|
||||
"total": total,
|
||||
"base_total": base_total,
|
||||
"tax_total": tax_total,
|
||||
},
|
||||
)
|
||||
|
||||
if not request.user.is_authenticated:
|
||||
response.set_cookie(ANONYMOUS_CART_ID_COOKIE_NAME, cart.uuid)
|
||||
return response
|
||||
|
||||
|
||||
list_products = ListProducts.as_view()
|
||||
|
||||
Reference in New Issue
Block a user