def set_product_count_in_cart(product: models.Product, cart: models.Cart, count: int) -> Optional[models.Cart]: if count == 0: return remove_products_from_cart([product], cart) relation = models.ProductCartRelation.objects.get(product=product, cart=cart) relation.count = count relation.save() cart.refresh_from_db() return cart
def add_products_to_cart(products: List[models.Product], cart: models.Cart) -> models.Cart: for product in products: relation, is_created = models.ProductCartRelation.objects.get_or_create( product=product, cart=cart) if not is_created: relation.count += 1 relation.save() cart.refresh_from_db() return cart
def remove_products_from_cart(products: List[models.Product], cart: models.Cart) -> Optional[models.Cart]: for product in products: try: relation = models.ProductCartRelation.objects.get(product=product, cart=cart) except models.ProductCartRelation.DoesNotExist: raise ValidationError(f'Product {product.slug} is not in the cart', code=400) else: relation.delete() # relation.save() cart.refresh_from_db() return cart