Fixes: add_product for Order

This commit is contained in:
Egor Pavlovich Gorbunov 2025-06-07 17:27:12 +03:00
parent 44e8ad6307
commit 2d96ec9637

View file

@ -550,7 +550,10 @@ class Order(ExportModelOperationsMixin("order"), NiceModel):
def total_quantity(self) -> int:
return sum([op.quantity for op in self.order_products.all()])
def add_product(self, product_uuid: str | None = None, attributes: list = [], update_quantity: bool = True):
def add_product(self, product_uuid: str | None = None, attributes: list = None, update_quantity: bool = True):
if attributes is None:
attributes = []
if self.status not in ["PENDING", "MOMENTAL"]:
raise ValueError(_("you cannot add products to an order that is not a pending one"))
try:
@ -569,7 +572,7 @@ class Order(ExportModelOperationsMixin("order"), NiceModel):
order_product, is_created = OrderProduct.objects.get_or_create(
product=product,
order=self,
attributes=json.dumps(attributes) if attributes else {},
attributes=json.dumps(attributes),
defaults={"quantity": 1, "buy_price": product.price},
)
if not is_created and update_quantity: