Skip to content

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

Fix shop query postal codes filtering (#6927)

Tomasz Szymańskia1c4c4538e6d
diff --git a/saleor/graphql/shop/resolvers.py b/saleor/graphql/shop/resolvers.py
index 8030a2b04a..09ce0fd1d1 100644
--- a/saleor/graphql/shop/resolvers.py
+++ b/saleor/graphql/shop/resolvers.py
@@ -1,5 +1,6 @@
from ...account.models import Address
from ...shipping.models import ShippingMethod, ShippingMethodChannelListing
+from ...shipping.postal_codes import filter_shipping_methods_by_postal_code_rules
from ..channel import ChannelContext
@@ -11,6 +12,9 @@ def resolve_available_shipping_methods(info, channel_slug: str, address):
available = available.filter(
shipping_zone__countries__contains=address.country,
)
+ available = filter_shipping_methods_by_postal_code_rules(
+ available, Address(**address)
+ )
# Address instance needed for apply_taxes_to_shipping method
address = Address(country=address.country)
else:
diff --git a/saleor/graphql/shop/tests/test_shop.py b/saleor/graphql/shop/tests/test_shop.py
index 97facbee8a..8323196514 100644
--- a/saleor/graphql/shop/tests/test_shop.py
+++ b/saleor/graphql/shop/tests/test_shop.py
@@ -7,6 +7,7 @@ from django_countries import countries
from ....account.models import Address
from ....core.error_codes import ShopErrorCode
from ....core.permissions import get_permissions_codename
+from ....shipping import PostalCodeRuleInclusionType
from ....shipping.models import ShippingMethod
from ....site.models import Site
from ...account.enums import CountryCodeEnum
@@ -765,6 +766,54 @@ def test_query_available_shipping_methods_for_given_address_vatlayer_set(
) not in {ship_meth["id"] for ship_meth in data}
+def test_query_available_shipping_methods_for_excluded_postal_code(
+ staff_api_client, channel_USD, shipping_method
+):
+ # given
+ query = AVAILABLE_SHIPPING_METHODS_QUERY
+ variables = {
+ "channel": channel_USD.slug,
+ "address": {"country": CountryCodeEnum.PL.name, "postalCode": "53-601"},
+ }
+ shipping_method.postal_code_rules.create(
+ start="53-600", end="54-600", inclusion_type=PostalCodeRuleInclusionType.EXCLUDE
+ )
+
+ # when
+ response = staff_api_client.post_graphql(query, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["shop"]["availableShippingMethods"]
+ assert graphene.Node.to_global_id("ShippingMethod", shipping_method.pk) not in {
+ ship_meth["id"] for ship_meth in data
+ }
+
+
+def test_query_available_shipping_methods_for_included_postal_code(
+ staff_api_client, channel_USD, shipping_method
+):
+ # given
+ query = AVAILABLE_SHIPPING_METHODS_QUERY
+ variables = {
+ "channel": channel_USD.slug,
+ "address": {"country": CountryCodeEnum.PL.name, "postalCode": "53-601"},
+ }
+ shipping_method.postal_code_rules.create(
+ start="53-600", end="54-600", inclusion_type=PostalCodeRuleInclusionType.INCLUDE
+ )
+
+ # when
+ response = staff_api_client.post_graphql(query, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["shop"]["availableShippingMethods"]
+ assert graphene.Node.to_global_id("ShippingMethod", shipping_method.pk) in {
+ ship_meth["id"] for ship_meth in data
+ }
+
+
MUTATION_SHOP_ADDRESS_UPDATE = """
mutation updateShopAddress($input: AddressInput){
shopAddressUpdate(input: $input){