Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix setting `undiscounted_base_shipping_price` on draft oders (#16481)
Iga Karbowiak085c3803483c
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.pyindex bab58543df..7436c27c6b 100644--- a/saleor/core/utils/random_data.py+++ b/saleor/core/utils/random_data.py@@ -803,6 +803,7 @@ def create_fake_order(max_order_lines=5, create_preorder_lines=False):"shipping_method_name": shipping_method.name,"shipping_price": shipping_price,"base_shipping_price": shipping_method_channel_listing.price,+ "undiscounted_base_shipping_price": shipping_method_channel_listing.price,})if will_be_unconfirmed:diff --git a/saleor/graphql/order/mutations/utils.py b/saleor/graphql/order/mutations/utils.pyindex 4d75e42f40..5102460376 100644--- a/saleor/graphql/order/mutations/utils.py+++ b/saleor/graphql/order/mutations/utils.py@@ -27,6 +27,7 @@ SHIPPING_METHOD_UPDATE_FIELDS = ["shipping_price_net_amount","shipping_price_gross_amount","base_shipping_price_amount",+ "undiscounted_base_shipping_price_amount","shipping_method_name","shipping_tax_class","shipping_tax_class_name",diff --git a/saleor/graphql/order/tests/mutations/test_draft_order_create.py b/saleor/graphql/order/tests/mutations/test_draft_order_create.pyindex 41ed1679b6..6b519b8f8f 100644--- a/saleor/graphql/order/tests/mutations/test_draft_order_create.py+++ b/saleor/graphql/order/tests/mutations/test_draft_order_create.py@@ -250,6 +250,7 @@ def test_draft_order_create_with_voucher_entire_order(channel_id=order.channel_id).get_total()assert order.base_shipping_price == shipping_total+ assert order.undiscounted_base_shipping_price == shipping_total# Ensure the correct event was createdcreated_draft_event = OrderEvent.objects.get(@@ -546,6 +547,7 @@ def test_draft_order_create_with_voucher_code(assert order.search_vectorassert order.external_reference == external_referenceassert order.base_shipping_price == shipping_total+ assert order.undiscounted_base_shipping_price == shipping_total# Ensure the correct event was createdcreated_draft_event = OrderEvent.objects.get(@@ -665,6 +667,7 @@ def test_draft_order_create_percentage_voucher(assert order.shipping_method == shipping_methodassert order.shipping_method_name == shipping_method.nameassert order.base_shipping_price == shipping_total+ assert order.undiscounted_base_shipping_price == shipping_total# Ensure the correct event was createdcreated_draft_event = OrderEvent.objects.get(@@ -795,6 +798,7 @@ def test_draft_order_create_with_voucher_specific_product(channel_id=order.channel_id).get_total()assert order.base_shipping_price == shipping_total+ assert order.undiscounted_base_shipping_price == shipping_totallines_data = data["lines"]discounted_line_data, line_1_data = lines_data@@ -936,6 +940,7 @@ def test_draft_order_create_with_voucher_apply_once_per_order(channel_id=order.channel_id).get_total()assert order.base_shipping_price == shipping_total+ assert order.undiscounted_base_shipping_price == shipping_totallines_data = data["lines"]discounted_line_data, line_1_data = lines_data@@ -1517,6 +1522,7 @@ def test_draft_order_create_with_same_variant_and_force_new_line(channel_id=order.channel_id).get_total()assert order.base_shipping_price == shipping_total+ assert order.undiscounted_base_shipping_price == shipping_total# Ensure the correct event was createdcreated_draft_event = OrderEvent.objects.get(@@ -1612,6 +1618,7 @@ def test_draft_order_create_with_inactive_channel(channel_id=order.channel_id).get_total()assert order.base_shipping_price == shipping_total+ assert order.undiscounted_base_shipping_price == shipping_total# Ensure the correct event was createdcreated_draft_event = OrderEvent.objects.get(@@ -1700,6 +1707,7 @@ def test_draft_order_create_without_sku(channel_id=order.channel_id).get_total()assert order.base_shipping_price == shipping_total+ assert order.undiscounted_base_shipping_price == shipping_totalorder_line = order.lines.get(variant=variant)assert order_line.product_sku is None@@ -1771,6 +1779,7 @@ def test_draft_order_create_variant_with_0_price(channel_id=order.channel_id).get_total()assert order.base_shipping_price == shipping_total+ assert order.undiscounted_base_shipping_price == shipping_total# Ensure the correct event was createdcreated_draft_event = OrderEvent.objects.get(@@ -2242,6 +2251,7 @@ def test_draft_order_create_with_channel(channel_id=order.channel_id).get_total()assert order.base_shipping_price == shipping_total+ assert order.undiscounted_base_shipping_price == shipping_total# Ensure the correct event was createdcreated_draft_event = OrderEvent.objects.get(@@ -2316,6 +2326,7 @@ def test_draft_order_create_product_without_shipping(assert order.shipping_method == shipping_methodassert order.shipping_address.first_name == graphql_address_data["firstName"]assert order.base_shipping_price == Money(0, "USD")+ assert order.undiscounted_base_shipping_price == Money(0, "USD")# Ensure the correct event was createdcreated_draft_event = OrderEvent.objects.get(@@ -3462,6 +3473,7 @@ def test_draft_order_create_create_no_shipping_method(order = Order.objects.get(id=order_pk)assert order.undiscounted_base_shipping_price_amount == 0assert order.base_shipping_price_amount == 0+ assert order.undiscounted_base_shipping_price_amount == 0def test_draft_order_create_voucher_exceed_usage_limit(diff --git a/saleor/graphql/order/tests/mutations/test_draft_order_update.py b/saleor/graphql/order/tests/mutations/test_draft_order_update.pyindex 384dc76b81..54cba5387b 100644--- a/saleor/graphql/order/tests/mutations/test_draft_order_update.py+++ b/saleor/graphql/order/tests/mutations/test_draft_order_update.py@@ -1504,6 +1504,7 @@ def test_draft_order_update_shipping_method_from_different_channel(# givenpermission_group_manage_orders.user_set.add(staff_api_client.user)order = draft_order+ base_shipping_price = order.base_shipping_priceorder.shipping_address = address_usaorder.save(update_fields=["shipping_address"])query = DRAFT_ORDER_UPDATE_SHIPPING_METHOD_MUTATION@@ -1525,6 +1526,10 @@ def test_draft_order_update_shipping_method_from_different_channel(assert error["code"] == OrderErrorCode.SHIPPING_METHOD_NOT_APPLICABLE.nameassert error["field"] == "shippingMethod"+ order.refresh_from_db()+ assert order.undiscounted_base_shipping_price == base_shipping_price+ assert order.base_shipping_price == base_shipping_price+def test_draft_order_update_shipping_method_prices_updates(staff_api_client,@@ -1541,14 +1546,17 @@ def test_draft_order_update_shipping_method_prices_updates(order.shipping_method = shipping_methodorder.save(update_fields=["shipping_address", "shipping_method"])assert shipping_method.channel_listings.first().price_amount == 10++ shipping_price = 15method_2 = shipping_method_weight_basedm2_channel_listing = method_2.channel_listings.first()- m2_channel_listing.price_amount = 15+ m2_channel_listing.price_amount = shipping_pricem2_channel_listing.save(update_fields=["price_amount"])query = DRAFT_ORDER_UPDATE_SHIPPING_METHOD_MUTATIONorder_id = graphene.Node.to_global_id("Order", order.id)shipping_method_id = graphene.Node.to_global_id("ShippingMethod", method_2.id)variables = {"id": order_id, "shippingMethod": shipping_method_id}+# whenresponse = staff_api_client.post_graphql(query, variables)@@ -1561,6 +1569,10 @@ def test_draft_order_update_shipping_method_prices_updates(assert data["order"]["shippingMethodName"] == method_2.nameassert data["order"]["shippingPrice"]["net"]["amount"] == 15.0+ order.refresh_from_db()+ assert order.undiscounted_base_shipping_price_amount == shipping_price+ assert order.base_shipping_price_amount == shipping_price+def test_draft_order_update_shipping_method_clear_with_none(staff_api_client,@@ -1597,6 +1609,9 @@ def test_draft_order_update_shipping_method_clear_with_none(assert data["order"]["shippingTaxRate"] == 0.0assert order.shipping_method is None+ assert order.undiscounted_base_shipping_price == zero_money(order.currency)+ assert order.base_shipping_price == zero_money(order.currency)+def test_draft_order_update_shipping_method(staff_api_client, permission_group_manage_orders, draft_order, shipping_method@@ -1641,7 +1656,7 @@ def test_draft_order_update_shipping_method(assert order.base_shipping_price == shipping_totalassert order.shipping_method == shipping_method- assert order.base_shipping_price == shipping_total+ assert order.undiscounted_base_shipping_price == shipping_totalassert order.shipping_price_net == shipping_price.netassert order.shipping_price_gross == shipping_price.grossdiff --git a/saleor/graphql/order/tests/mutations/test_order_bulk_create.py b/saleor/graphql/order/tests/mutations/test_order_bulk_create.pyindex f2606a0c87..c476353add 100644--- a/saleor/graphql/order/tests/mutations/test_order_bulk_create.py+++ b/saleor/graphql/order/tests/mutations/test_order_bulk_create.py@@ -578,6 +578,7 @@ def test_order_bulk_create(assert db_order.shipping_price_gross_amount == 120assert db_order.shipping_price_net_amount == 100assert db_order.base_shipping_price_amount == 100+ assert db_order.undiscounted_base_shipping_price_amount == 100assert db_order.total_gross_amount == 120assert db_order.total_net_amount == 100assert db_order.undiscounted_total_gross_amount == 120diff --git a/saleor/graphql/order/tests/mutations/test_order_update_shipping.py b/saleor/graphql/order/tests/mutations/test_order_update_shipping.pyindex 2ef1da1663..efa69581a3 100644--- a/saleor/graphql/order/tests/mutations/test_order_update_shipping.py+++ b/saleor/graphql/order/tests/mutations/test_order_update_shipping.py@@ -48,6 +48,7 @@ def test_order_update_shipping(permission_group_manage_orders.user_set.add(staff_api_client.user)order = order_with_linesorder.base_shipping_price = zero_money(order.currency)+ order.undiscounted_base_shipping_price = zero_money(order.currency)order.status = statusorder.save()assert order.shipping_method != shipping_method@@ -69,6 +70,7 @@ def test_order_update_shipping(assert order.status == statusassert order.shipping_method == shipping_methodassert order.base_shipping_price == shipping_total+ assert order.undiscounted_base_shipping_price == shipping_totalassert order.shipping_price_net == shipping_price.netassert order.shipping_price_gross == shipping_price.grossassert order.shipping_tax_rate == Decimal("0.0")diff --git a/saleor/order/migrations/0192_set_undiscounted_base_shipping_price_on_draft_orders.py b/saleor/order/migrations/0192_set_undiscounted_base_shipping_price_on_draft_orders.pynew file mode 100644index 0000000000..6866a826b8--- /dev/null+++ b/saleor/order/migrations/0192_set_undiscounted_base_shipping_price_on_draft_orders.py@@ -0,0 +1,63 @@+# Generated by Django 3.2.25 on 2024-08-05 07:27+from django.db import migrations, transaction+from django.db.models import F++# The batch of size 250 takes ~0.2 second and consumes ~20MB memory at peak+ORDER_SET_SHIPPING_PRICE_BATCH_SIZE = 250+++def set_udniscounted_base_shipping_price_on_draft_orders(apps, _schema_editor):+ Order = apps.get_model("order", "Order")+ qs = Order.objects.filter(+ undiscounted_base_shipping_price_amount=0,+ base_shipping_price_amount__gt=0,+ status="draft",+ )+ for batch_numbers in queryset_in_batches(qs):+ orders = Order.objects.filter(number__in=batch_numbers)+ _set_undiscounted_base_shipping_price(orders)+++def _set_undiscounted_base_shipping_price(orders):+ with transaction.atomic():+ _orders = list(orders.select_for_update(of=(["self"])))+ orders.update(+ undiscounted_base_shipping_price_amount=F("base_shipping_price_amount")+ )+++def queryset_in_batches(queryset):+ """Slice a queryset into batches.++ Input queryset should be sorted be pk.+ """+ start_number = None++ while True:+ lookup = {}+ if start_number is not None:+ lookup["number__gt"] = start_number+ qs = queryset.order_by("number").filter(**lookup)[+ :ORDER_SET_SHIPPING_PRICE_BATCH_SIZE+ ]+ numbers = list(qs.values_list("number", flat=True))++ if not numbers:+ break++ yield numbers++ start_number = numbers[-1]+++class Migration(migrations.Migration):+ dependencies = [+ ("order", "0191_merge_20240726_1508"),+ ]++ operations = [+ migrations.RunPython(+ set_udniscounted_base_shipping_price_on_draft_orders,+ reverse_code=migrations.RunPython.noop,+ )+ ]diff --git a/saleor/order/migrations/0193_merge_20240805_0810.py b/saleor/order/migrations/0193_merge_20240805_0810.pynew file mode 100644index 0000000000..b45c373bbc--- /dev/null+++ b/saleor/order/migrations/0193_merge_20240805_0810.py@@ -0,0 +1,12 @@+# Generated by Django 3.2.25 on 2024-08-05 08:10++from django.db import migrations+++class Migration(migrations.Migration):+ dependencies = [+ ("order", "0192_merge_20240726_1513"),+ ("order", "0192_set_undiscounted_base_shipping_price_on_draft_orders"),+ ]++ operations = []diff --git a/saleor/order/migrations/0194_merge_20240805_0820.py b/saleor/order/migrations/0194_merge_20240805_0820.pynew file mode 100644index 0000000000..8b2c9b988c--- /dev/null+++ b/saleor/order/migrations/0194_merge_20240805_0820.py@@ -0,0 +1,12 @@+# Generated by Django 3.2.25 on 2024-08-05 08:20++from django.db import migrations+++class Migration(migrations.Migration):+ dependencies = [+ ("order", "0193_merge_20240726_1516"),+ ("order", "0193_merge_20240805_0810"),+ ]++ operations = []