Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Extend OrderLine with is_price_overridden field and update unit_discount_type field (#16105)

Iga Karbowiakca18be888874
diff --git a/saleor/checkout/complete_checkout.py b/saleor/checkout/complete_checkout.py
index 14adaeec96..9d5de4ae6a 100644
--- a/saleor/checkout/complete_checkout.py
+++ b/saleor/checkout/complete_checkout.py
@@ -330,6 +330,8 @@ def _create_line_for_order(
else:
tax_class = product.product_type.tax_class
+ is_price_overridden = checkout_line.price_override is not None
+
line = OrderLine( # type: ignore[misc] # see below:
product_name=product_name,
variant_name=variant_name,
@@ -351,8 +353,10 @@ def _create_line_for_order(
unit_discount=discount_amount, # money field not supported by mypy_django_plugin # noqa: E501
unit_discount_reason=unit_discount_reason,
unit_discount_value=discount_amount.amount, # we store value as fixed discount
+ unit_discount_type=DiscountValueType.FIXED,
base_unit_price=base_unit_price, # money field not supported by mypy_django_plugin # noqa: E501
undiscounted_base_unit_price=undiscounted_base_unit_price, # money field not supported by mypy_django_plugin # noqa: E501
+ is_price_overridden=is_price_overridden,
metadata=checkout_line.metadata,
private_metadata=checkout_line.private_metadata,
**get_tax_class_kwargs_for_order_line(tax_class),
@@ -394,17 +398,9 @@ def _get_unit_discount_reason(
) -> Optional[str]:
unit_discount_reason = None
if line_voucher_code:
- if unit_discount_reason:
- unit_discount_reason += f" & Voucher code: {line_voucher_code}"
- else:
- unit_discount_reason = f"Voucher code: {line_voucher_code}"
+ unit_discount_reason = f"Voucher code: {line_voucher_code}"
elif order_voucher_code:
- if unit_discount_reason:
- msg = f" & Entire order voucher code: {order_voucher_code}"
- unit_discount_reason += msg
- else:
- unit_discount_reason = f"Entire order voucher code: {order_voucher_code}"
-
+ unit_discount_reason = f"Entire order voucher code: {order_voucher_code}"
return unit_discount_reason
diff --git a/saleor/discount/tests/test_utils/test_copy_unit_discount_data_to_order_line.py b/saleor/discount/tests/test_utils/test_copy_unit_discount_data_to_order_line.py
index cd832bdb51..f014bc4ff8 100644
--- a/saleor/discount/tests/test_utils/test_copy_unit_discount_data_to_order_line.py
+++ b/saleor/discount/tests/test_utils/test_copy_unit_discount_data_to_order_line.py
@@ -92,5 +92,5 @@ def test_copy_unit_discount_data_to_order_line_no_discount(order_with_lines):
line = lines_info[0].line
assert line.unit_discount_amount == Decimal(0)
assert not line.unit_discount_reason
- assert line.unit_discount_type == DiscountValueType.FIXED
+ assert line.unit_discount_type is None
assert line.unit_discount_value == Decimal(0)
diff --git a/saleor/discount/tests/test_utils/test_create_or_update_discount_objects_from_promotion_for_order.py b/saleor/discount/tests/test_utils/test_create_or_update_discount_objects_from_promotion_for_order.py
index ff6ddc0f3a..6523652ac3 100644
--- a/saleor/discount/tests/test_utils/test_create_or_update_discount_objects_from_promotion_for_order.py
+++ b/saleor/discount/tests/test_utils/test_create_or_update_discount_objects_from_promotion_for_order.py
@@ -268,7 +268,7 @@ def test_create_order_discount_gift(
assert gift_line.unit_price_net_amount == Decimal(0)
assert gift_line.base_unit_price_amount == Decimal(0)
assert gift_line.unit_discount_amount == Decimal(0)
- assert gift_line.unit_discount_type == RewardValueType.FIXED
+ assert gift_line.unit_discount_type is None
assert gift_line.unit_discount_value == Decimal(0)
assert gift_line.product_name == product.name
assert gift_line.product_sku == variant.sku
diff --git a/saleor/discount/utils.py b/saleor/discount/utils.py
index 41ab416009..8b3da4dc0b 100644
--- a/saleor/discount/utils.py
+++ b/saleor/discount/utils.py
@@ -339,17 +339,17 @@ def get_products_voucher_discount(
def apply_discount_to_value(
value: Decimal,
- value_type: str,
+ value_type: Optional[str],
currency: str,
price_to_discount: Union[Money, TaxedMoney],
):
"""Calculate the price based on the provided values."""
- if value_type == DiscountValueType.FIXED:
- discount_method = fixed_discount
- discount_kwargs = {"discount": Money(value, currency)}
- else:
+ if value_type == DiscountValueType.PERCENTAGE:
discount_method = percentage_discount
discount_kwargs = {"percentage": value, "rounding": ROUND_HALF_UP}
+ else:
+ discount_method = fixed_discount
+ discount_kwargs = {"discount": Money(value, currency)}
discount = partial(
discount_method,
**discount_kwargs,
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py
index 8819c1ebda..8bf41182b5 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py
@@ -19,7 +19,7 @@ from .....checkout.models import Checkout, CheckoutLine
from .....core.exceptions import InsufficientStock, InsufficientStockData
from .....core.taxes import TaxError, zero_money, zero_taxed_money
from .....discount import DiscountType, DiscountValueType, RewardValueType
-from .....discount.models import CheckoutLineDiscount
+from .....discount.models import CheckoutLineDiscount, Promotion
from .....giftcard import GiftCardEvents
from .....giftcard.models import GiftCard, GiftCardEvent
from .....order import OrderOrigin, OrderStatus
@@ -1533,6 +1533,7 @@ def test_checkout_complete_product_on_promotion(
assert checkout_line_quantity == order_line.quantity
assert checkout_line_variant == order_line.variant
+ assert order_line.is_price_overridden is False
assert order.shipping_address == address
assert order.shipping_method == checkout.shipping_method
assert order.payments.exists()
@@ -1545,6 +1546,235 @@ def test_checkout_complete_product_on_promotion(
).exists(), "Checkout should have been deleted"
+def test_checkout_complete_product_on_promotion_deleted_promotion_instance(
+ user_api_client,
+ checkout_with_item,
+ catalogue_promotion_without_rules,
+ payment_dummy,
+ address,
+ shipping_method,
+):
+ # given
+ checkout = checkout_with_item
+ checkout.shipping_address = address
+ checkout.shipping_method = shipping_method
+ checkout.billing_address = address
+ checkout.metadata_storage.store_value_in_metadata(items={"accepted": "true"})
+ checkout.metadata_storage.store_value_in_private_metadata(
+ items={"accepted": "false"}
+ )
+ checkout.save()
+ checkout.metadata_storage.save()
+
+ checkout_line = checkout.lines.first()
+ checkout_line_quantity = checkout_line.quantity
+ checkout_line_variant = checkout_line.variant
+
+ channel = checkout.channel
+ reward_value = Decimal("5")
+ rule = catalogue_promotion_without_rules.rules.create(
+ catalogue_predicate={
+ "productPredicate": {
+ "ids": [
+ graphene.Node.to_global_id(
+ "Product", checkout_line_variant.product.id
+ )
+ ]
+ }
+ },
+ reward_value_type=RewardValueType.FIXED,
+ reward_value=reward_value,
+ )
+ rule.channels.add(channel)
+ promotion_id = graphene.Node.to_global_id(
+ "Promotion", catalogue_promotion_without_rules.id
+ )
+
+ variant_channel_listing = checkout_line_variant.channel_listings.get(
+ channel=channel
+ )
+
+ variant_channel_listing.discounted_price_amount = (
+ variant_channel_listing.price_amount - reward_value
+ )
+ variant_channel_listing.save(update_fields=["discounted_price_amount"])
+
+ variant_channel_listing.variantlistingpromotionrule.create(
+ promotion_rule=rule,
+ discount_amount=reward_value,
+ currency=channel.currency_code,
+ )
+ CheckoutLineDiscount.objects.create(
+ line=checkout_line,
+ type=DiscountType.PROMOTION,
+ value_type=DiscountValueType.FIXED,
+ amount_value=reward_value,
+ currency=channel.currency_code,
+ promotion_rule=rule,
+ )
+
+ manager = get_plugins_manager(allow_replica=False)
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, manager)
+
+ total = calculations.checkout_total(
+ manager=manager,
+ checkout_info=checkout_info,
+ lines=lines,
+ address=address,
+ )
+ payment = payment_dummy
+ payment.is_active = True
+ payment.order = None
+ payment.total = total.gross.amount
+ payment.currency = total.gross.currency
+ payment.checkout = checkout
+ payment.save()
+ assert not payment.transactions.exists()
+
+ orders_count = Order.objects.count()
+ checkout_id = to_global_id_or_none(checkout)
+ variables = {
+ "id": checkout_id,
+ "redirectUrl": "https://www.example.com",
+ }
+
+ def delete_promotion(*args, **kwargs):
+ Promotion.objects.get(id=catalogue_promotion_without_rules.id).delete()
+
+ # when
+ with before_after.before(
+ "saleor.checkout.complete_checkout.complete_checkout_with_payment",
+ delete_promotion,
+ ):
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutComplete"]
+ assert not data["errors"]
+
+ order_token = data["order"]["token"]
+ order_id = data["order"]["id"]
+ assert Order.objects.count() == orders_count + 1
+ order = Order.objects.first()
+ assert str(order.id) == order_token
+ assert order_id == graphene.Node.to_global_id("Order", order.id)
+ assert order.metadata == checkout.metadata_storage.metadata
+ assert order.private_metadata == checkout.metadata_storage.private_metadata
+
+ order_line = order.lines.first()
+ assert order.total == total
+ assert order.undiscounted_total == total + (
+ order_line.undiscounted_total_price - order_line.total_price
+ )
+ assert not order_line.sale_id
+ assert order_line.unit_discount_reason
+ assert order_line.unit_discount_reason == f"Promotion: {promotion_id}"
+ assert checkout_line_quantity == order_line.quantity
+ assert checkout_line_variant == order_line.variant
+
+ assert order.shipping_address == address
+ assert order.shipping_method == checkout.shipping_method
+ assert order.payments.exists()
+ order_payment = order.payments.first()
+ assert order_payment == payment
+ assert payment.transactions.count() == 1
+
+ assert not Checkout.objects.filter(
+ pk=checkout.pk
+ ).exists(), "Checkout should have been deleted"
+
+
+def test_checkout_complete_price_override(
+ user_api_client,
+ checkout_with_item,
+ catalogue_promotion_without_rules,
+ payment_dummy,
+ address,
+ shipping_method,
+):
+ # given
+ checkout = checkout_with_item
+ checkout.shipping_address = address
+ checkout.shipping_method = shipping_method
+ checkout.billing_address = address
+ checkout.metadata_storage.store_value_in_metadata(items={"accepted": "true"})
+ checkout.metadata_storage.store_value_in_private_metadata(
+ items={"accepted": "false"}
+ )
+ checkout.tax_exemption = True
+ checkout.save()
+ checkout.metadata_storage.save()
+
+ checkout_line = checkout.lines.first()
+ checkout_line.price_override = Decimal("2.0")
+ checkout_line.save(update_fields=["price_override"])
+
+ checkout_line_quantity = checkout_line.quantity
+ checkout_line_variant = checkout_line.variant
+
+ manager = get_plugins_manager(allow_replica=False)
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, manager)
+ total = calculations.calculate_checkout_total_with_gift_cards(
+ manager, checkout_info, lines, address
+ )
+ channel = checkout.channel
+ channel.automatically_confirm_all_new_orders = True
+ channel.save()
+ payment = payment_dummy
+ payment.is_active = True
+ payment.order = None
+ payment.total = total.gross.amount
+ payment.currency = total.gross.currency
+ payment.checkout = checkout
+ payment.save()
+ assert not payment.transactions.exists()
+
+ orders_count = Order.objects.count()
+ variables = {
+ "id": to_global_id_or_none(checkout),
+ "redirectUrl": "https://www.example.com",
+ }
+
+ # when
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutComplete"]
+ assert not data["errors"]
+
+ order_token = data["order"]["token"]
+ order_id = data["order"]["id"]
+ assert Order.objects.count() == orders_count + 1
+ order = Order.objects.first()
+ assert str(order.id) == order_token
+ assert order_id == graphene.Node.to_global_id("Order", order.id)
+ assert order.metadata == checkout.metadata_storage.metadata
+ assert order.private_metadata == checkout.metadata_storage.private_metadata
+
+ order_line = order.lines.first()
+ assert order.total == total
+ assert order.undiscounted_total == total + (
+ order_line.undiscounted_total_price - order_line.total_price
+ )
+
+ assert checkout_line_quantity == order_line.quantity
+ assert checkout_line_variant == order_line.variant
+ assert order_line.is_price_overridden is True
+ assert order.shipping_address == address
+ assert order.shipping_method == checkout.shipping_method
+ assert order.payments.exists()
+ order_payment = order.payments.first()
+ assert order_payment == payment
+ assert payment.transactions.count() == 1
+ assert not Checkout.objects.filter(
+ pk=checkout.pk
+ ).exists(), "Checkout should have been deleted"
+
+
def test_checkout_complete_product_on_old_sale(
user_api_client,
checkout_with_item,
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py
index 9a9f752789..c51c3c8f8e 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py
@@ -474,6 +474,7 @@ def test_checkout_with_charged(
assert order_line.tax_class_name == line_tax_class.name
assert order_line.tax_class_metadata == line_tax_class.metadata
assert order_line.tax_class_private_metadata == line_tax_class.private_metadata
+ assert order_line.is_price_overridden is False
assert order.shipping_address == address
assert order.shipping_method == checkout.shipping_method
@@ -488,6 +489,105 @@ def test_checkout_with_charged(
assert not len(Reservation.objects.all())
+def test_checkout_price_override(
+ user_api_client,
+ checkout_with_gift_card,
+ transaction_item_generator,
+ address,
+ shipping_method,
+):
+ # given
+ checkout = checkout_with_gift_card
+ checkout.shipping_address = address
+ checkout.shipping_method = shipping_method
+ checkout.billing_address = address
+ checkout.metadata_storage.store_value_in_metadata(items={"accepted": "true"})
+ checkout.metadata_storage.store_value_in_private_metadata(
+ items={"accepted": "false"}
+ )
+ checkout.tax_exemption = True
+ checkout.save()
+ checkout.metadata_storage.save()
+
+ checkout_line = checkout.lines.first()
+ checkout_line.price_override = Decimal("2.0")
+ checkout_line.save(update_fields=["price_override"])
+
+ checkout_line_quantity = checkout_line.quantity
+ checkout_line_variant = checkout_line.variant
+
+ channel = checkout.channel
+ channel.automatically_confirm_all_new_orders = True
+ channel.save()
+
+ manager = get_plugins_manager(allow_replica=False)
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, manager)
+ total = calculations.calculate_checkout_total_with_gift_cards(
+ manager, checkout_info, lines, address
+ )
+
+ transaction = transaction_item_generator(
+ checkout_id=checkout.pk, charged_value=total.gross.amount
+ )
+
+ update_checkout_payment_statuses(
+ checkout=checkout_info.checkout,
+ checkout_total_gross=total.gross,
+ checkout_has_lines=bool(lines),
+ )
+
+ redirect_url = "https://www.example.com"
+ variables = {"id": to_global_id_or_none(checkout), "redirectUrl": redirect_url}
+
+ # when
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
+
+ # then
+ order = Order.objects.get()
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutComplete"]
+ assert not data["errors"]
+ assert to_global_id_or_none(order) == data["order"]["id"]
+
+ assert order.total_charged_amount == transaction.charged_value
+ assert order.total_authorized == zero_money(order.currency)
+ assert order.charge_status == OrderChargeStatus.FULL
+ assert order.authorize_status == OrderAuthorizeStatus.FULL
+
+ assert order.status == OrderStatus.UNFULFILLED
+ assert order.origin == OrderOrigin.CHECKOUT
+
+ assert order.redirect_url == redirect_url
+ assert order.total.gross == total.gross
+ assert order.metadata == checkout.metadata_storage.metadata
+ assert order.private_metadata == checkout.metadata_storage.private_metadata
+
+ order_line = order.lines.first()
+ line_tax_class = order_line.tax_class
+ shipping_tax_class = shipping_method.tax_class
+
+ assert checkout_line_quantity == order_line.quantity
+ assert checkout_line_variant == order_line.variant
+
+ assert order_line.tax_class == line_tax_class
+ assert order_line.tax_class_name == line_tax_class.name
+ assert order_line.tax_class_metadata == line_tax_class.metadata
+ assert order_line.tax_class_private_metadata == line_tax_class.private_metadata
+ assert order_line.is_price_overridden is True
+
+ assert order.shipping_address == address
+ assert order.shipping_method == checkout.shipping_method
+ assert order.shipping_tax_rate is not None
+ assert order.shipping_tax_class_name == shipping_tax_class.name
+ assert order.shipping_tax_class_metadata == shipping_tax_class.metadata
+ assert (
+ order.shipping_tax_class_private_metadata == shipping_tax_class.private_metadata
+ )
+
+ assert not Checkout.objects.filter()
+
+
def test_checkout_paid_with_multiple_transactions(
user_api_client,
checkout_with_gift_card,
diff --git a/saleor/graphql/order/tests/mutations/test_draft_order_create.py b/saleor/graphql/order/tests/mutations/test_draft_order_create.py
index 57f3e43f99..c9a55c243d 100644
--- a/saleor/graphql/order/tests/mutations/test_draft_order_create.py
+++ b/saleor/graphql/order/tests/mutations/test_draft_order_create.py
@@ -282,6 +282,9 @@ def test_draft_order_create_with_voucher(
assert order_discount.value == voucher_listing.discount_value
assert order_discount.amount_value == voucher_listing.discount_value
+ for line in order_lines:
+ assert line.is_price_overridden is False
+
def test_draft_order_create_with_voucher_and_voucher_code(
staff_api_client,
@@ -2367,10 +2370,12 @@ def test_draft_order_create_with_custom_price_in_order_line(
order_line_0 = order.lines.get(variant=variant_0)
assert order_line_0.base_unit_price_amount == expected_price_variant_0
assert order_line_0.undiscounted_base_unit_price_amount == expected_price_variant_0
+ assert order_line_0.is_price_overridden is True
order_line_1 = order.lines.get(variant=variant_1)
assert order_line_1.base_unit_price_amount == expected_price_variant_1
assert order_line_1.undiscounted_base_unit_price_amount == expected_price_variant_1
+ assert order_line_1.is_price_overridden is True
def test_draft_order_create_product_catalogue_promotion(
diff --git a/saleor/graphql/order/tests/mutations/test_order_lines_create.py b/saleor/graphql/order/tests/mutations/test_order_lines_create.py
index 9c320976a7..1266e2ae75 100644
--- a/saleor/graphql/order/tests/mutations/test_order_lines_create.py
+++ b/saleor/graphql/order/tests/mutations/test_order_lines_create.py
@@ -60,9 +60,7 @@ ORDER_LINES_CREATE_MUTATION = """
unitDiscount {
amount
}
- unitDiscountType
- unitDiscountValue
- isGift
+ isPriceOverridden
}
order {
total {
@@ -1008,3 +1006,4 @@ def test_order_lines_create_with_custom_price(
assert data["orderLines"][0]["productSku"] == variant.sku
assert data["orderLines"][0]["productVariantId"] == variant.get_global_id()
assert data["orderLines"][0]["quantity"] == quantity
+ assert data["orderLines"][0]["isPriceOverridden"] is True
diff --git a/saleor/graphql/order/tests/queries/test_order.py b/saleor/graphql/order/tests/queries/test_order.py
index 20e2890993..38e81ca50f 100644
--- a/saleor/graphql/order/tests/queries/test_order.py
+++ b/saleor/graphql/order/tests/queries/test_order.py
@@ -88,6 +88,7 @@ query OrdersQuery {
}
lines {
id
+ isPriceOverridden
unitPrice{
gross{
amount
@@ -419,6 +420,30 @@ def test_order_query_denormalized_shipping_tax_class_data(
)
+def test_order_query_price_overridden(
+ staff_api_client,
+ permission_group_manage_orders,
+ permission_group_manage_shipping,
+ fulfilled_order,
+):
+ # given
+ order = fulfilled_order
+ permission_group_manage_orders.user_set.add(staff_api_client.user)
+ permission_group_manage_shipping.user_set.add(staff_api_client.user)
+ line = order.lines.first()
+ line.is_price_overridden = True
+ line.save(update_fields=["is_price_overridden"])
+
+ # when
+ response = staff_api_client.post_graphql(ORDERS_FULL_QUERY)
+ content = get_graphql_content(response)
+
+ # then
+ order_data = content["data"]["orders"]["edges"][0]["node"]
+ order_line = order_data["lines"][0]
+ assert order_line["isPriceOverridden"] is True
+
+
def test_order_query_total_price_is_0(
staff_api_client,
permission_group_manage_orders,
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.py
index 4a6c6e36c0..479956f36b 100644
--- a/saleor/graphql/order/types.py
+++ b/saleor/graphql/order/types.py
@@ -836,6 +836,10 @@ class OrderLine(ModelObjectType[models.OrderLine]):
description="Price of the order line without discounts.",
required=True,
)
+ is_price_overridden = graphene.Boolean(
+ description="Returns True, if the line unit price was overridden."
+ + ADDED_IN_314,
+ )
variant = graphene.Field(
ProductVariant,
required=False,
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index dcb77cdc14..ed04970d6b 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -12279,6 +12279,13 @@ type OrderLine implements Node & ObjectWithMetadata @doc(category: "Orders") {
"""Price of the order line without discounts."""
undiscountedTotalPrice: TaxedMoney!
+ """
+ Returns True, if the line unit price was overridden.
+
+ Added in Saleor 3.14.
+ """
+ isPriceOverridden: Boolean
+
"""
A purchased product variant. Note: this field may be null if the variant has been removed from stock at all. Requires one of the following permissions to include the unpublished items: MANAGE_ORDERS, MANAGE_DISCOUNTS, MANAGE_PRODUCTS.
"""
diff --git a/saleor/order/migrations/0173_alter_orderline_unit_discount_type_add_is_price_overridden.py b/saleor/order/migrations/0173_alter_orderline_unit_discount_type_add_is_price_overridden.py
new file mode 100644
index 0000000000..a191b42430
--- /dev/null
+++ b/saleor/order/migrations/0173_alter_orderline_unit_discount_type_add_is_price_overridden.py
@@ -0,0 +1,35 @@
+# Generated by Django 3.2.22 on 2024-05-22 09:59
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("order", "0172_update_order_cc_addresses"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="orderline",
+ name="unit_discount_type",
+ field=models.CharField(
+ blank=True,
+ choices=[("fixed", "fixed"), ("percentage", "%")],
+ max_length=10,
+ null=True,
+ ),
+ ),
+ migrations.RunSQL(
+ """
+ ALTER TABLE order_orderline
+ ALTER COLUMN unit_discount_type
+ SET DEFAULT null;
+ """,
+ migrations.RunSQL.noop,
+ ),
+ migrations.AddField(
+ model_name="orderline",
+ name="is_price_overridden",
+ field=models.BooleanField(blank=True, null=True),
+ ),
+ ]
diff --git a/saleor/order/migrations/0177_merge_20240606_1022.py b/saleor/order/migrations/0177_merge_20240606_1022.py
new file mode 100644
index 0000000000..76d80d41e7
--- /dev/null
+++ b/saleor/order/migrations/0177_merge_20240606_1022.py
@@ -0,0 +1,12 @@
+# Generated by Django 3.2.25 on 2024-06-06 10:22
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("order", "0173_alter_orderline_unit_discount_type_add_is_price_overridden"),
+ ("order", "0176_merge_20240325_1315"),
+ ]
+
+ operations = []
diff --git a/saleor/order/migrations/0178_merge_20240606_1036.py b/saleor/order/migrations/0178_merge_20240606_1036.py
new file mode 100644
index 0000000000..6a2eed7797
--- /dev/null
+++ b/saleor/order/migrations/0178_merge_20240606_1036.py
@@ -0,0 +1,12 @@
+# Generated by Django 3.2.25 on 2024-06-06 10:36
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("order", "0177_merge_20240325_1329"),
+ ("order", "0177_merge_20240606_1022"),
+ ]
+
+ operations = []
diff --git a/saleor/order/migrations/0181_merge_20240606_1325.py b/saleor/order/migrations/0181_merge_20240606_1325.py
new file mode 100644
index 0000000000..0b410ebd24
--- /dev/null
+++ b/saleor/order/migrations/0181_merge_20240606_1325.py
@@ -0,0 +1,12 @@
+# Generated by Django 3.2.25 on 2024-06-06 13:25
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("order", "0178_merge_20240606_1036"),
+ ("order", "0180_merge_20240325_1333"),
+ ]
+
+ operations = []
diff --git a/saleor/order/migrations/0183_merge_20240607_1222.py b/saleor/order/migrations/0183_merge_20240607_1222.py
new file mode 100644
index 0000000000..2d37f4cdfd
--- /dev/null
+++ b/saleor/order/migrations/0183_merge_20240607_1222.py
@@ -0,0 +1,12 @@
+# Generated by Django 3.2.22 on 2024-06-07 12:22
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("order", "0181_merge_20240606_1325"),
+ ("order", "0182_merge_20240325_1338"),
+ ]
+
+ operations = []
diff --git a/saleor/order/migrations/0185_merge_20240607_1231.py b/saleor/order/migrations/0185_merge_20240607_1231.py
new file mode 100644
index 0000000000..ca59199b69
--- /dev/null
+++ b/saleor/order/migrations/0185_merge_20240607_1231.py
@@ -0,0 +1,12 @@
+# Generated by Django 3.2.22 on 2024-06-07 12:31
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("order", "0181_merge_20240606_1325"),
+ ("order", "0184_merge_0182_merge_20240325_1338_0183_order_tax_error"),
+ ]
+
+ operations = []
diff --git a/saleor/order/migrations/0186_merge_20240607_1247.py b/saleor/order/migrations/0186_merge_20240607_1247.py
new file mode 100644
index 0000000000..229ab3b2e2
--- /dev/null
+++ b/saleor/order/migrations/0186_merge_20240607_1247.py
@@ -0,0 +1,13 @@
+# Generated by Django 3.2.25 on 2024-06-07 12:42
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("order", "0183_merge_20240607_1222"),
+ ("order", "0185_merge_20240607_1231"),
+ ("order", "0185_ordergrantedrefund_transaction_item"),
+ ]
+
+ operations = []
diff --git a/saleor/order/models.py b/saleor/order/models.py
index bbff37a07f..f3ba9addd4 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -565,7 +565,8 @@ class OrderLine(ModelWithMetadata):
unit_discount_type = models.CharField(
max_length=10,
choices=DiscountValueType.CHOICES,
- default=DiscountValueType.FIXED,
+ null=True,
+ blank=True,
)
unit_discount_reason = models.TextField(blank=True, null=True)
@@ -688,6 +689,8 @@ class OrderLine(ModelWithMetadata):
blank=True, null=True, default=dict, encoder=CustomJsonEncoder
)
+ is_price_overridden = models.BooleanField(null=True, blank=True)
+
# Fulfilled when voucher code was used for product in the line
voucher_code = models.CharField(max_length=255, null=True, blank=True)
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index e70b3f1f1f..c16620909d 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -15,7 +15,7 @@ from ..core.tracing import traced_atomic_transaction
from ..core.utils.country import get_active_country
from ..core.utils.translations import get_translation
from ..core.weight import zero_weight
-from ..discount import DiscountType
+from ..discount import DiscountType, DiscountValueType
from ..discount.models import (
NotApplicable,
OrderDiscount,
@@ -280,6 +280,7 @@ def create_order_line(
total_price=total_price,
undiscounted_total_price=undiscounted_total_price,
variant=variant,
+ is_price_overridden=price_override is not None,
**get_tax_class_kwargs_for_order_line(tax_class),
)
@@ -303,6 +304,7 @@ def create_order_line(
else:
discount_amount = unit_discount.net
line.unit_discount = discount_amount
+ line.unit_discount_type = DiscountValueType.FIXED
line.unit_discount_value = discount_amount.amount
line.save(
@@ -310,6 +312,7 @@ def create_order_line(
"unit_discount_amount",
"unit_discount_value",
"unit_discount_reason",
+ "unit_discount_type",
"sale_id",
]
)