Skip to content

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

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.py
index 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 warnings
from datetime import timedelta
import graphene
@@ -5,6 +6,7 @@ import pytest
from django.utils import timezone
from django_countries import countries
+from ....channel.utils import DEPRECATION_WARNING_MESSAGE
from ....shipping.models import ShippingZone
from ....warehouse.models import PreorderReservation, Reservation
from ...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.py
index 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.node
channel_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))
@staticmethod
def resolve_digital_content(root: ChannelContext[models.ProductVariant], _info):