Skip to content

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

Fix external shipping method calls on checkout lines add (#15550)

Tomasz Szymańskid564234577dc
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index 4afff8687c..614ab54745 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -993,9 +993,11 @@ def get_external_shipping_id(container: Union["Checkout", "Order"]):
)
-def delete_external_shipping_id(checkout: Checkout):
+def delete_external_shipping_id(checkout: Checkout, save: bool = False):
metadata = get_or_create_checkout_metadata(checkout)
metadata.delete_value_from_private_metadata(PRIVATE_META_APP_SHIPPING_ID)
+ if save:
+ metadata.save(update_fields=["private_metadata"])
def get_or_create_checkout_metadata(checkout: "Checkout") -> CheckoutMetadata:
diff --git a/saleor/graphql/checkout/mutations/checkout_lines_add.py b/saleor/graphql/checkout/mutations/checkout_lines_add.py
index 3fc8700c02..0d86735a1d 100644
--- a/saleor/graphql/checkout/mutations/checkout_lines_add.py
+++ b/saleor/graphql/checkout/mutations/checkout_lines_add.py
@@ -29,6 +29,7 @@ from .utils import (
get_checkout,
get_variants_and_total_quantities,
group_lines_input_on_add,
+ update_checkout_external_shipping_method_if_invalid,
update_checkout_shipping_method_if_invalid,
validate_variants_are_published,
validate_variants_available_for_purchase,
@@ -212,6 +213,7 @@ class CheckoutLinesAdd(BaseMutation):
replace,
)
+ update_checkout_external_shipping_method_if_invalid(checkout_info, lines)
update_checkout_shipping_method_if_invalid(checkout_info, lines)
invalidate_checkout(checkout_info, lines, manager, save=True)
cls.call_event(manager.checkout_updated, checkout)
diff --git a/saleor/graphql/checkout/mutations/utils.py b/saleor/graphql/checkout/mutations/utils.py
index 3d1d3afa89..d34ecfe24f 100644
--- a/saleor/graphql/checkout/mutations/utils.py
+++ b/saleor/graphql/checkout/mutations/utils.py
@@ -25,6 +25,8 @@ from ....checkout.fetch import CheckoutInfo, CheckoutLineInfo
from ....checkout.utils import (
calculate_checkout_quantity,
clear_delivery_method,
+ delete_external_shipping_id,
+ get_external_shipping_id,
is_shipping_required,
)
from ....core.exceptions import InsufficientStock, PermissionDenied
@@ -96,6 +98,21 @@ def clean_delivery_method(
return method in valid_methods
+def _is_external_shipping_valid(checkout_info: "CheckoutInfo") -> bool:
+ if external_shipping_id := get_external_shipping_id(checkout_info.checkout):
+ return external_shipping_id in [
+ method.id for method in checkout_info.valid_delivery_methods
+ ]
+ return True
+
+
+def update_checkout_external_shipping_method_if_invalid(
+ checkout_info: "CheckoutInfo", lines: Iterable[CheckoutLineInfo]
+):
+ if not _is_external_shipping_valid(checkout_info):
+ delete_external_shipping_id(checkout_info.checkout, save=True)
+
+
def update_checkout_shipping_method_if_invalid(
checkout_info: "CheckoutInfo", lines: Iterable[CheckoutLineInfo]
):
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py
index 061ee23be6..1b20246740 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py
@@ -6,11 +6,13 @@ import graphene
import pytest
import pytz
from django.utils import timezone
+from prices import Money
from .....checkout.error_codes import CheckoutErrorCode
from .....checkout.fetch import fetch_checkout_info, fetch_checkout_lines
from .....checkout.models import Checkout
from .....checkout.utils import (
+ PRIVATE_META_APP_SHIPPING_ID,
calculate_checkout_quantity,
invalidate_checkout,
recalculate_checkout_discount,
@@ -18,6 +20,7 @@ from .....checkout.utils import (
from .....discount import RewardType, RewardValueType
from .....plugins.manager import get_plugins_manager
from .....product.models import ProductChannelListing, ProductVariantChannelListing
+from .....shipping.interface import ShippingMethodData
from .....warehouse import WarehouseClickAndCollectOption
from .....warehouse.models import Reservation, Stock
from .....warehouse.tests.utils import get_available_quantity_for_stock
@@ -1044,6 +1047,90 @@ def test_checkout_lines_add_existing_variant_override_previous_custom_price(
assert line.price_override == price
[email protected]("saleor.plugins.manager.PluginsManager.list_shipping_methods_for_checkout")
+def test_checkout_lines_add_deletes_external_shipping_method_if_invalid(
+ mocked_webhook, app_api_client, checkout_with_item, permission_handle_checkouts
+):
+ # given
+ mocked_webhook.return_value = []
+ checkout = checkout_with_item
+ line = checkout.lines.first()
+ metadata = checkout.metadata_storage
+ metadata.private_metadata = {PRIVATE_META_APP_SHIPPING_ID: "QXBwOjEzMzc="}
+ metadata.save(update_fields=["private_metadata"])
+
+ variant_id = graphene.Node.to_global_id("ProductVariant", line.variant.pk)
+ price = Decimal("13.11")
+
+ variables = {
+ "id": to_global_id_or_none(checkout),
+ "lines": [{"variantId": variant_id, "quantity": 7, "price": price}],
+ "channelSlug": checkout.channel.slug,
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ MUTATION_CHECKOUT_LINES_ADD,
+ variables,
+ permissions=[permission_handle_checkouts],
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutLinesAdd"]
+ assert not data["errors"]
+ metadata.refresh_from_db()
+ assert metadata.private_metadata == {}
+
+
[email protected]("saleor.plugins.manager.PluginsManager.list_shipping_methods_for_checkout")
+def test_checkout_lines_add_doesnt_delete_external_shipping_method_if_valid(
+ mocked_webhook,
+ app_api_client,
+ checkout_with_item,
+ permission_handle_checkouts,
+ address,
+):
+ # given
+ external_method_data = ShippingMethodData(
+ id=graphene.Node.to_global_id("App", app_api_client.app.id),
+ price=Money(Decimal(10), checkout_with_item.currency),
+ active=True,
+ )
+ mocked_webhook.return_value = [external_method_data]
+ checkout = checkout_with_item
+ checkout.shipping_address = address
+ checkout.save()
+ line = checkout.lines.first()
+ metadata = checkout.metadata_storage
+ metadata.private_metadata = {PRIVATE_META_APP_SHIPPING_ID: external_method_data.id}
+ metadata.save(update_fields=["private_metadata"])
+
+ variant_id = graphene.Node.to_global_id("ProductVariant", line.variant.pk)
+ price = Decimal("13.11")
+
+ variables = {
+ "id": to_global_id_or_none(checkout),
+ "lines": [{"variantId": variant_id, "quantity": 7, "price": price}],
+ "channelSlug": checkout.channel.slug,
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ MUTATION_CHECKOUT_LINES_ADD,
+ variables,
+ permissions=[permission_handle_checkouts],
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutLinesAdd"]
+ assert not data["errors"]
+ assert metadata.private_metadata == {
+ PRIVATE_META_APP_SHIPPING_ID: external_method_data.id
+ }
+
+
def test_checkout_lines_add_custom_price_and_order_fixed_discount(
app_api_client,
checkout_with_item_and_order_discount,