Hi Good day Team,
I'm begginer and currently developing an eCommerce project.
I have found a tutorial on add/remove to cart from youtube. its works fine, but the problem i faced is, when completely remove / remove all the items from cart.
the cart value is become '0' and the iems is removed, means its working fine. but in Admin dashboard the Order Item and Order on the user is still there.
Can guide on this issue?
Here is the code below:
Model.py
class Products(models.Model):
productname = models.CharField(max_length=255, blank=True, null=True)
productprice = models.IntegerField(blank=True, null=True)
class OrderItem(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
item = models.ForeignKey(Products, on_delete=models.CASCADE)
quantity = models.IntegerField(default=1)
ordered = models.BooleanField(default=False)
def __str__(self):
return f"{self.quantity} of {self.item.productname}"
def get_total_item_price(self):
return self.quantity * self.item.productprice
def get_final_price(self):
return self.get_total_item_price()
class Order(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
ref_code = models.CharField(max_length=20, blank=True, null=True)
ordered = models.BooleanField(default=False)
items = models.ManyToManyField(OrderItem)
start_date = models.DateTimeField(auto_now_add=True)
ordered_date = models.DateTimeField()
def __str__(self):
return self.user.username
def get_total(self):
total = 0
for order_item in self.items.all():
total += order_item.get_final_price()
return total
View.py
@login_required
def addtocart(request, Proid):
#item = get_object_or_404(Products, id=id)
item = Products.objects.get(id=Proid)
order_item, created = OrderItem.objects.get_or_create(user=request.user, item=item, ordered=False)
order_qs = Order.objects.filter(user=request.user, ordered=False)
if order_qs.exists():
order = order_qs[0]
# check if the order item is in the order ==(order ache,for same-orderitem)
if order.items.filter(item__id=item.id).exists():
order_item.quantity += 1
order_item.save()
messages.info(request, "This product quantity was updated.")
return redirect('ProductDetail')
else:
order.items.add(order_item)
messages.info(request, "This product is added to your cart")
return redirect('ProductDetail')
else:
ordered_date = timezone.now()
order = Order.objects.create(user=request.user, ordered_date=ordered_date)
order.items.add(order_item)
messages.info(request, "This product is added to your cart")
return redirect('ProductDetail')
@login_required
def removefromcartdetail(request, Proid):
item = get_object_or_404(Products, id=Proid)
order_qs = Order.objects.filter(
user=request.user,
ordered=False
)
if order_qs.exists():
order = order_qs[0]
# check if the order item is in the order
if order.items.filter(item__id=item.id).exists():
order_item = OrderItem.objects.filter(
item=item,
user=request.user,
ordered=False
)[0]
order_item.quantity = 1
order_item.save()
order.items.remove(order_item)
messages.info(request, "This item was removed from your cart.")
return redirect('CartDetail')
else:
messages.info(request, "This item was not in your cart")
return redirect('CartDetail')
else:
messages.info(request, "You do not have an active order")
return redirect('CartDetail')
Did you check the removefromcartdetail view. when you remove from cart then you have to minus the quantity from Order Item model . But you are assigning as = 1. can you check that?
- VengatNoted with Many Thanks.. Apologize on code paste. Over look on Code Snippet and Image on Content.
- ronaldlouritson 4 years, 10 months agoHi Admin,
I tried removed this code order_item.quantity = 1. but its still the same..
- ronaldlouritson 4 years, 10 months agook from Admin dashboard its not getting removed right?.. If yes can you show me the Admin dashboard logic . Do you have separate Admin dashboard for showing the total number of items ?
- VengatAdmin dashboard its not getting removed right? - Yes, Do you have separate Admin dashboard for showing the total number of items ? - No,
admin.site.register(models.Products) admin.site.register(models.Order) admin.site.register(models.OrderItem)
- ronaldlouritson 4 years, 10 months agoso what i understand, logic should be , if order item = 2 and then remove the item from cart then it has to remove the Order item quantity from models. If my understanding is correct then may be trying the logic in removeitemcart view order_item.quantity = 0. So whenever user remove the item it should remove the number from OrderItem table as well. This is just for testing purpose. Thanks for your sponsor.
- VengatTried as order_item.quantity = 0 but its still the same. its just updating the quantity to 0. :/
- ronaldlouritson 4 years, 10 months agoyes, if remove item should remove the whole item, if - Minus quantity only change the quantity
- ronaldlouritson 4 years, 10 months agowhy cant you try delete the Order item if there is no item in the cart?
- Vengatcan you try this? if doesnt work then something else causing issue in your code.
- Vengati have this code to remove order.items.remove(order_item) but not removing
- ronaldlouritson 4 years, 10 months agonot from order.. from OrderItem
.If you remove from OrderItem, then it should remove from Order By default right
As we are not getting any update on this. We are closing the ticket from our end.
- VengatHi Admin, Good day. The issue is haven't solved. Was looking into other issues. Will update further. Thanks
- ronaldlouritson 4 years, 10 months ago
Acknowledged