I am trying to merge guest cart (session cart) items and existing user cart. if cart items repeating increase quantity else, create a new cart item.
In my case user already have a cart. in it three products [<CartItem: The Book Thief>, <CartItem: Sapiens: a Brief History of Human Kind>, <CartItem: The Forty Rules of Love>]
then the user logged out and add some products [<CartItem: The Alchemist>, <CartItem: Sapiens: a Brief History of Human Kind>] , then when he login again, the repeating product should add quantity, not as a repeating product. and the new product should show as new product in the cart.
but my code results always repeating product in the cart?
def user_login(request):
if request.user.is_authenticated :
return redirect('home')
else:
if request.method == 'POST':
email = request.POST['email']
password = request.POST['password']
user= authenticate(email =email, password = password)
if user is not None:
try:
cart = Cart.objects.get(cart_id = _cart_id(request))
is_cart_item_exists = CartItem.objects.filter(cart=cart).exists()
if is_cart_item_exists:
cart_items = CartItem.objects.filter(cart=cart)
new_cart_items = []
for cart_item in cart_items:
item = cart_item
new_cart_items.append(item)
print(new_cart_items)
cart_items = CartItem.objects.filter(user=user)
existing_cart_items = []
id =[]
for cart_item in cart_items:
item = cart_item
existing_cart_items.append(item)
id.append(cart_item.id)
print(existing_cart_items)
print(id)
for new_cart_item in new_cart_items:
print(new_cart_item)
if new_cart_item in existing_cart_items: #i think this if condition always returns false, even if new_cart_item is in existing_cart_item.
index=existing_cart_items.index(new_cart_item)
item_id=id[index]
item=CartItem.objects.get(id=item_id)
item.quantity += 1
item.user=user
item.save()
print('added to existing items')
else:
cart_items = CartItem.objects.filter(cart=cart)
for cart_item in cart_items:
cart_item.user = user
cart_item.save()
print('added as a new item')
except:
pass
login(request, user)
return redirect('home')
else:
messages.error(request, "Invalid login credentials")
return render(request, 'accounts/login.html')
Cart and CartItem model
class Cart(models.Model):
cart_id = models.CharField(max_length=255, blank=True)
date_created = models.DateField(auto_now_add=True)
def __str__(self):
return self.cart_id
class CartItem(models.Model):
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE, null=True)
cart = models.ForeignKey(Cart, on_delete=models.CASCADE, null=True)
product = models.ForeignKey(Products, on_delete=models.CASCADE)
quantity = models.IntegerField()
is_active = models.BooleanField(default=True)
modified_time = models.DateTimeField(auto_now=True)
def item_total(self):
return self.product.price * self.quantity
def __str__(self):
return self.product.name
terminal
[<CartItem: The Alchemist>, <CartItem: Sapiens: a Brief History of Human Kind>]
[<CartItem: The Book Thief>, <CartItem: Sapiens: a Brief History of Human Kind>, <CartItem: The Forty Rules of Love>]
[75, 73, 74]
The Alchemist
added as a new item
Sapiens: a Brief History of Human Kind
added as a new item
_cart_id function
def _cart_id(request):
cart_id = request.session.session_key
if not cart_id:
car_id = request.session.create()
return cart_id
I think if condition in above always returns false, even if new_cart_item is in existing_cart_item.