def form_valid(self, form, *args, **kwargs):
product = get_object_or_404(Product, pk=self.kwargs['product_id'])
quantity = form.cleaned_data['quantity']
cart = get_cart(self.request, create=True)
cart_item, cart_item_created = CartItem.objects.update_or_create(cart=cart, product=product)
# If Cart item object has not been created , amend quantity.
if cart_item_created is False:
cart_item.quantity += quantity
else:
cart_item.quantity = quantity
cart_item.save()
return super(AddToCartView, self).form_valid(form)
评论列表
文章目录