Skip to content

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

Do not return external shipping methods with different currency (#17350)

Maciej Korycinskib05633dae118
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 92a66e2de4..951178fd39 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -64,3 +64,4 @@ All notable, unreleased changes to this project will be documented in this file.
- Added support for the AWS_S3_URL_PROTOCOL environment variable - #17305 by @p-febis
- Fixed pycurl dependency and required system libraries to fix Celery worker issues when using SQS by @mariobrgomes
- Added [`alg`](https://datatracker.ietf.org/doc/html/rfc7517#section-4.4) key to JWKS available at `/.well-known/jwks.json` - #17363 by @lkostrowski
+- `checkout.shippingMethods` and `checkout.availableShippingMethods` will no longer return external shipping methods if their currency differs from the checkout's currency - #17350 by @korycins
diff --git a/saleor/graphql/checkout/mutations/checkout_delivery_method_update.py b/saleor/graphql/checkout/mutations/checkout_delivery_method_update.py
index d82abe0254..f553ab754d 100644
--- a/saleor/graphql/checkout/mutations/checkout_delivery_method_update.py
+++ b/saleor/graphql/checkout/mutations/checkout_delivery_method_update.py
@@ -158,16 +158,6 @@ class CheckoutDeliveryMethodUpdate(BaseMutation):
checkout_info, lines, shipping_method=delivery_method, collection_point=None
)
- if delivery_method and delivery_method.price.currency != checkout.currency:
- raise ValidationError(
- {
- "delivery_method_id": ValidationError(
- "Cannot choose shipping method with different currency than the checkout.",
- code=CheckoutErrorCode.DELIVERY_METHOD_NOT_APPLICABLE.value,
- )
- }
- )
-
cls._update_delivery_method(
manager,
checkout_info,
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_delivery_method_update.py b/saleor/graphql/checkout/tests/mutations/test_checkout_delivery_method_update.py
index 308051fdaa..02299e165f 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_delivery_method_update.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_delivery_method_update.py
@@ -419,11 +419,8 @@ def test_checkout_delivery_method_update_external_shipping_invalid_currency(
errors = data["errors"]
assert len(errors) == 1
assert errors[0]["field"] == "deliveryMethodId"
- assert errors[0]["code"] == CheckoutErrorCode.DELIVERY_METHOD_NOT_APPLICABLE.name
- assert (
- errors[0]["message"]
- == "Cannot choose shipping method with different currency than the checkout."
- )
+ assert errors[0]["code"] == CheckoutErrorCode.NOT_FOUND.name
+ assert errors[0]["message"] == f"Couldn't resolve to a node: ${method_id}"
@patch(
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.py
index 4e397eb105..4b17cf0e51 100644
--- a/saleor/plugins/manager.py
+++ b/saleor/plugins/manager.py
@@ -2583,7 +2583,12 @@ class PluginsManager(PaymentInterface):
# https://github.com/python/mypy/issues/9975
getattr(plugin, "get_shipping_methods_for_checkout")(checkout, None)
)
- return shipping_methods
+ return list(
+ filter(
+ lambda method: method.price.currency == checkout.currency,
+ shipping_methods,
+ )
+ )
def get_shipping_method(
self,
diff --git a/saleor/plugins/tests/sample_plugins.py b/saleor/plugins/tests/sample_plugins.py
index 472555adcb..00216a3f69 100644
--- a/saleor/plugins/tests/sample_plugins.py
+++ b/saleor/plugins/tests/sample_plugins.py
@@ -14,6 +14,7 @@ from ...payment.interface import (
TransactionSessionData,
TransactionSessionResult,
)
+from ...shipping.interface import ShippingMethodData
from ..base_plugin import BasePlugin, ConfigurationTypeField, ExternalAccessTokens
if TYPE_CHECKING:
@@ -366,6 +367,24 @@ class PluginSample(BasePlugin):
def payment_method_process_tokenization(self, request_data, previous_value):
return previous_value
+ def get_shipping_methods_for_checkout(
+ self, checkout: "Checkout", previous_value: Any
+ ) -> list["ShippingMethodData"]:
+ different_currency = "EUR"
+ assert checkout.currency != different_currency
+ return [
+ ShippingMethodData(
+ id="123",
+ price=Money(Decimal(10), currency=different_currency),
+ name="EUR shipping",
+ ),
+ ShippingMethodData(
+ id="123",
+ price=Money(Decimal(10), currency=checkout.currency),
+ name="Default shipping",
+ ),
+ ]
+
class ChannelPluginSample(PluginSample):
PLUGIN_ID = "channel.plugin.sample"
diff --git a/saleor/plugins/tests/test_manager.py b/saleor/plugins/tests/test_manager.py
index 13bf3ce02f..d62e05015e 100644
--- a/saleor/plugins/tests/test_manager.py
+++ b/saleor/plugins/tests/test_manager.py
@@ -1630,6 +1630,25 @@ def test_run_plugin_method_until_first_success_for_active_plugins_only(
assert mock_run_method.call_count == calls
+def test_manager_skips_external_shipping_with_different_currency_than_checkout_currency(
+ checkout_with_item,
+):
+ # given
+ plugins = ["saleor.plugins.tests.sample_plugins.PluginSample"]
+
+ # when
+ shipping_methods = PluginsManager(
+ plugins=plugins
+ ).list_shipping_methods_for_checkout(
+ checkout=checkout_with_item,
+ channel_slug=checkout_with_item.channel.slug,
+ )
+
+ # then
+ assert len(shipping_methods) == 1
+ assert shipping_methods[0].price.currency == checkout_with_item.currency
+
+
@mock.patch(
"saleor.plugins.manager.PluginsManager._PluginsManager__run_method_on_plugins"
)