Skip to content

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

Return only shipping methods with shipping zones for given channel in shop.availableShippingMethods

IKarbowiak916c1e29f265
diff --git a/saleor/graphql/shop/resolvers.py b/saleor/graphql/shop/resolvers.py
index 09ce0fd1d1..70ff4f4212 100644
--- a/saleor/graphql/shop/resolvers.py
+++ b/saleor/graphql/shop/resolvers.py
@@ -6,7 +6,8 @@ from ..channel import ChannelContext
def resolve_available_shipping_methods(info, channel_slug: str, address):
available = ShippingMethod.objects.filter(
- channel_listings__channel__slug=channel_slug
+ shipping_zone__channels__slug=channel_slug,
+ channel_listings__channel__slug=channel_slug,
)
if address and address.country:
available = available.filter(
diff --git a/saleor/graphql/shop/tests/benchmark/test_homepage.py b/saleor/graphql/shop/tests/benchmark/test_homepage.py
index 4fc6d489e1..9137e4746b 100644
--- a/saleor/graphql/shop/tests/benchmark/test_homepage.py
+++ b/saleor/graphql/shop/tests/benchmark/test_homepage.py
@@ -1,18 +1,24 @@
import pytest
+from ....order.tests.benchmark.test_order import FRAGMENT_AVAILABLE_SHIPPING_METHODS
from ....tests.utils import get_graphql_content
@pytest.mark.django_db
@pytest.mark.count_queries(autouse=False)
-def test_retrieve_shop(api_client, count_queries):
- query = """
- query getShop {
+def test_retrieve_shop(api_client, channel_USD, count_queries):
+ query = (
+ FRAGMENT_AVAILABLE_SHIPPING_METHODS
+ + """
+ query getShop($channel: String!) {
shop {
defaultCountry {
code
country
}
+ availableShippingMethods(channel: $channel) {
+ ...AvailableShippingMethods
+ }
countries {
country
code
@@ -20,5 +26,8 @@ def test_retrieve_shop(api_client, count_queries):
}
}
"""
+ )
- get_graphql_content(api_client.post_graphql(query))
+ get_graphql_content(
+ api_client.post_graphql(query, variables={"channel": channel_USD.slug})
+ )
diff --git a/saleor/graphql/shop/tests/test_shop.py b/saleor/graphql/shop/tests/test_shop.py
index e7d1639f05..07660733f4 100644
--- a/saleor/graphql/shop/tests/test_shop.py
+++ b/saleor/graphql/shop/tests/test_shop.py
@@ -695,14 +695,32 @@ def test_query_available_shipping_methods_no_address(
# then
content = get_graphql_content(response)
data = content["data"]["shop"]["availableShippingMethods"]
+ assert len(data) > 0
assert {ship_meth["id"] for ship_meth in data} == {
graphene.Node.to_global_id("ShippingMethod", ship_meth.pk)
for ship_meth in ShippingMethod.objects.filter(
- channel_listings__channel__slug=channel_USD.slug
+ shipping_zone__channels__slug=channel_USD.slug,
+ channel_listings__channel__slug=channel_USD.slug,
)
}
+def test_query_available_shipping_methods_no_channel_shipping_zones(
+ staff_api_client, shipping_method, shipping_method_channel_PLN, channel_USD
+):
+ # given
+ query = AVAILABLE_SHIPPING_METHODS_QUERY
+ channel_USD.shipping_zones.clear()
+
+ # when
+ response = staff_api_client.post_graphql(query, {"channel": channel_USD.slug})
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["shop"]["availableShippingMethods"]
+ assert len(data) == 0
+
+
def test_query_available_shipping_methods_for_given_address(
staff_api_client,
channel_USD,