Skip to content

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

Add ShippingMethodQueryset.for_channel method

IKarbowiak6a1f884168d2
diff --git a/saleor/graphql/shop/resolvers.py b/saleor/graphql/shop/resolvers.py
index 70ff4f4212..72c15e1cb8 100644
--- a/saleor/graphql/shop/resolvers.py
+++ b/saleor/graphql/shop/resolvers.py
@@ -5,10 +5,7 @@ from ..channel import ChannelContext
def resolve_available_shipping_methods(info, channel_slug: str, address):
- available = ShippingMethod.objects.filter(
- shipping_zone__channels__slug=channel_slug,
- channel_listings__channel__slug=channel_slug,
- )
+ available = ShippingMethod.objects.for_channel(channel_slug)
if address and address.country:
available = available.filter(
shipping_zone__countries__contains=address.country,
diff --git a/saleor/graphql/shop/tests/test_shop.py b/saleor/graphql/shop/tests/test_shop.py
index 07660733f4..f3c0f6917c 100644
--- a/saleor/graphql/shop/tests/test_shop.py
+++ b/saleor/graphql/shop/tests/test_shop.py
@@ -744,7 +744,7 @@ def test_query_available_shipping_methods_for_given_address(
data = content["data"]["shop"]["availableShippingMethods"]
assert len(data) == shipping_method_count - 1
assert graphene.Node.to_global_id(
- "ShippingMethod", shipping_zone_without_countries.pk
+ "ShippingMethod", shipping_zone_without_countries.shipping_methods.first().pk
) not in {ship_meth["id"] for ship_meth in data}
diff --git a/saleor/shipping/models.py b/saleor/shipping/models.py
index d37e3849e9..2d1f6c3d5e 100644
--- a/saleor/shipping/models.py
+++ b/saleor/shipping/models.py
@@ -98,6 +98,12 @@ class ShippingMethodQueryset(models.QuerySet):
def weight_based(self):
return self.filter(type=ShippingMethodType.WEIGHT_BASED)
+ def for_channel(self, channel_slug: str):
+ return self.filter(
+ shipping_zone__channels__slug=channel_slug,
+ channel_listings__channel__slug=channel_slug,
+ )
+
@staticmethod
def applicable_shipping_methods_by_channel(shipping_methods, channel_id):
query = ShippingMethodChannelListing.objects.filter(