Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix variant quantity available for no channel and country code (#10297)
Jakub2b4035f09af2
diff --git a/saleor/graphql/product/tests/test_variant_availability.py b/saleor/graphql/product/tests/test_variant_availability.pyindex 943def2eb6..57fd618126 100644--- a/saleor/graphql/product/tests/test_variant_availability.py+++ b/saleor/graphql/product/tests/test_variant_availability.py@@ -1,3 +1,4 @@+import warningsfrom datetime import timedeltaimport graphene@@ -5,6 +6,7 @@ import pytestfrom django.utils import timezonefrom django_countries import countries+from ....channel.utils import DEPRECATION_WARNING_MESSAGEfrom ....shipping.models import ShippingZonefrom ....warehouse.models import PreorderReservation, Reservationfrom ...tests.utils import get_graphql_content@@ -33,6 +35,22 @@ def test_variant_quantity_available_without_country_code(assert variant_data["quantityAvailable"] == 7+def test_variant_quantity_available_without_country_code_or_channel(+ api_client, variant_with_many_stocks, channel_USD+):+ variables = {+ "id": graphene.Node.to_global_id("ProductVariant", variant_with_many_stocks.pk)+ }+ with warnings.catch_warnings(record=True) as warns:+ response = api_client.post_graphql(QUERY_QUANTITY_AVAILABLE, variables)+ content = get_graphql_content(response)+ variant_data = content["data"]["productVariant"]+ assert variant_data["quantityAvailable"] == 7+ assert any(+ [str(warning.message) == DEPRECATION_WARNING_MESSAGE for warning in warns]+ )++def test_variant_quantity_available_when_one_stock_is_exceeded(api_client, variant_with_many_stocks, channel_USD):diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.pyindex 0f41691d86..1bbcb79265 100644--- a/saleor/graphql/product/types/products.py+++ b/saleor/graphql/product/types/products.py@@ -387,6 +387,8 @@ class ProductVariant(ChannelContextTypeWithMetadata, ModelObjectType):if address is not None:country_code = address.country+ channel_slug = str(root.channel_slug) if root.channel_slug else None+global_quantity_limit_per_checkout = (info.context.site.settings.limit_quantity_per_checkout)@@ -395,7 +397,7 @@ class ProductVariant(ChannelContextTypeWithMetadata, ModelObjectType):variant = root.nodechannel_listing = VariantChannelListingByVariantIdAndChannelSlugLoader(info.context- ).load((variant.id, str(root.channel_slug)))+ ).load((variant.id, channel_slug))def calculate_available_per_channel(channel_listing):if (@@ -490,7 +492,7 @@ class ProductVariant(ChannelContextTypeWithMetadata, ModelObjectType):return AvailableQuantityByProductVariantIdCountryCodeAndChannelSlugLoader(info.context- ).load((root.node.id, country_code, str(root.channel_slug)))+ ).load((root.node.id, country_code, channel_slug))@staticmethoddef resolve_digital_content(root: ChannelContext[models.ProductVariant], _info):