views.py
from django.shortcuts import render, redirect, get_object_or_404
from orgapp.models import Product
from .models import Cart,CartItem
from django.core.exceptions import ObjectDoesNotExist
def _cart_id(request):
cart = request.session.session_key
if not cart:
cart = request.session.create()
return cart
def add_cart(request,product_id,total=0,counter=0):
product = Product.objects.get(id=product_id)
try:
cart = Cart.objects.get(cart_id=_cart_id(request))
except Cart.DoesNotExist:
cart=Cart.objects.create(
cart_id=_cart_id(request)
)
cart.save()
try:
cart_item=CartItem.objects.get(product=product,cart=cart)
if cart_item.quantity < cart_item.product.stock:
cart_item.quantity +=1
cart_item.save()
except CartItem.DoesNotExist:
cart_item = CartItem.objects.create(
product=product,
quantity=1,
cart=cart
)
cart_item.save()
cart = Cart.objects.get(cart_id=_cart_id(request))
cart_items = CartItem.objects.filter(cart=cart, active=True)
for cart_item in cart_items:
total += (cart_item.product.price * cart_item.quantity)
counter += cart_item.quantity
return render(request, 'cart.html', dict(cart_items=cart_items, total=total, counter=counter))
def cart_remove(request,product_id,cart_item):
cart=Cart.objects.get(cart_id=_cart_id(request))
product=get_object_or_404(Product,id=product_id)
cart_item=CartItem.objects.get(product=product,cart=cart)
if cart_item.quantity > 1:
cart_item.quantity -= 1
cart_item.save()
else:
cart_item.delete()
return render(request, "cart.html", {'cart_item':cart_item})
def full_remove(request,product_id,cart_item):
cart = Cart.objects.get(cart_id=_cart_id(request))
product = get_object_or_404(Product, id=product_id)
cart_item = CartItem.objects.get(product=product, cart=cart)
cart_item.delete()
return render(request, "cart.html", {'cart_item':cart_item})
models
from django.db import models
from orgapp.models import Product
# Create your models here.
class Cart(models.Model):
objects = None
cart_id=models.CharField(max_length=250, blank=True)
date_added=models.DateField(auto_now_add=True)
class Meta:
db_table = 'Cart'
ordering = ['date_added']
def __str__(self):
return '{}'.format(self.cart_id)
class CartItem(models.Model):
product = models.ForeignKey(Product,on_delete=models.CASCADE)
cart=models.ForeignKey(Cart,on_delete=models.CASCADE)
quantity=models.IntegerField()
active=models.BooleanField(default=True)
class Meta:
db_table='CartItem'
def sub_total(self):
return self.product.price * self.quantity
def __str__(self):
return '{}'.format(self.product)
cart.html
{% if not cart_items %}
<div>
<div class="text-center">
<br>
<h1 class="text-center my_title"> Your Shopping Cart Is Empty </h1>
<br>
<p class="text-center">
Please Click <a href="{% url 'orgapp:allProdCat' %}">Here</a>to continue
</p>
</div>
</div>
{% else %}
<div>
<div class="text-center">
<br>
<h1 class="text-center my_title">
Your Shopping Cart
</h1>
<br>
</div>
<div class="row mx-auto">
<div class="col-12 col-sm-12 col-md-12 col-lg-6 text-center">
<table class="table my_custom_table">
<thead class="my_custom_thead">
<tr>
<th colspan="5">
Your Items
</th>
</tr>
</thead>
<tbody>
{% for cart_item in cart_items %}
<tr>
<td>
<a href="cart_item.product.get_absolute_url"><img src="{{cart_item.product.image.url}}" alt="" style="width:100px;height:100px;" class="float-left rounded custom_image"></a>
</td>
<td class="text-left">
{{cart_item.product.name}}
<br>
SKU:{{cart_item.product.id}}
<br>
Price: ${{cart_item.product.price}}
<br>
Quantity: {{cart_item.quantity}} x Rs. {{cart_item.product.price}}
</td>
<td>
Rs.{{cart_item.sub_total}}
</td>
{% if cart_item.quantity < cart_item.product.stock %}
<td>
<a href="{% url 'cart:add_cart' cart_item.product.id %}" class="custom_a"><i class="fa fa-plus custom_icon"></i></a>
<a href="{% url 'cart:cart_remove' cart_item.product_id %}" class="custom_a"><i class="fa fa-minus custom_icon"></i></a>
<a href="{% url 'cart:full_remove' cart_item.product_id %}" class="custom_icon"><i class="fa-solid fa-trash custom_icon"></i></a>
</td>
{% else %}
<td>
<a href="{% url 'cart:cart_remove' cart_item.product.id %}" class="custom_a"><i class="fa fa-minus custom_icon"></i></a>
<a href="{% url 'cart:full_remove' cart_item.product.id %}" class="custom_icon"><i class="fa-solid fa-trash custom_icon"></i></a>
</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="col-12 col-sm-12 col-md-12 col-lg-6 text-center">
<table class="table my_custom_table">
<thead class="my_custom_thead">
<tr>
<th>
CHECKOUT
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
Please review your items before proceeding with the payment
</td>
</tr>
<td class="text-left">
Your total is: <strong>{{total}}</strong>
</td>
</tbody>
</table>
<div class="mx-auto">
<a href="{% url 'orgapp:allProdCat' %}" class="btn btn-secondary btn-block my_custom_button">Continue Shopping</a>
</div>
</div>
{% endif %}
</div>
</div>
{% endblock %}
My issue:
I can add products to cart. I can increment the qty with ‘+’ but when I click - or trash, “Your cart is empty” is displayed but cart on navbar shows the correct qty within ( ). Kindly help me to solve this issue.
Thanks