[Answered ]-Django cart not updating quantity

2👍

Found the error 🙂 Indentation issues (stupid python) j/k first one is mine and second one is the correct one. Gotta get use to this. Was easy to overlook to the untrained eye….

def add_to_cart(request):    
postdata = request.POST.copy()
product_slug = postdata.get('product_slug', '')
quantity = postdata.get('quantity', 1)
p = get_object_or_404(Product, slug = product_slug)
cart_products = get_cart_items(request)
product_in_cart = False
for cart_item in cart_products:
    if cart_item.product.id == p.id:
        cart_item.augment_quantity(quantity)
        product_in_cart = True
    if not product_in_cart:
        ci =  CartItem()
        ci.product = p
        ci.quantity = quantity
        ci.cart_id = _cart_id(request)
        ci.save()

Here is the books and it works:

def add_to_cart(request):
postdata = request.POST.copy()
product_slug = postdata.get('product_slug','')
quantity = postdata.get('quantity',1)
p = get_object_or_404(Product, slug=product_slug)
cart_products = get_cart_items(request)
product_in_cart = False
for cart_item in cart_products:
    if cart_item.product.id == p.id:
        cart_item.augment_quantity(quantity)
        product_in_cart = True
if not product_in_cart:
    ci = CartItem()
    ci.product = p
    ci.quantity = quantity
    ci.cart_id = _cart_id(request)
    ci.save()
👤mcd

Leave a comment