Skip to content

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

Fix failing quantityAvailable calculations for warehouse withous shipping zones (#10666)

Iga Karbowiakb88bf39c5504
diff --git a/saleor/graphql/product/tests/test_variant_availability.py b/saleor/graphql/product/tests/test_variant_availability.py
index 50de400fd9..e0dfe7ea3b 100644
--- a/saleor/graphql/product/tests/test_variant_availability.py
+++ b/saleor/graphql/product/tests/test_variant_availability.py
@@ -177,6 +177,104 @@ def test_variant_quantity_available_without_country_code_and_no_channel_shipping
assert variant_data["quantityAvailable"] == 0
+def test_variant_quantity_available_no_country_warehouse_without_zone(
+ api_client, variant_with_many_stocks, channel_USD, channel_PLN
+):
+ """Ensure the warehouse without shipping zones is not counted in variant available
+ quantity."""
+ # given
+ assert variant_with_many_stocks.stocks.count() == 2
+
+ stock_1, stock_2 = variant_with_many_stocks.stocks.all()
+ # clear shipping zones one of variant warehouses
+ stock_2.warehouse.shipping_zones.clear()
+
+ variables = {
+ "id": graphene.Node.to_global_id("ProductVariant", variant_with_many_stocks.pk),
+ "channel": channel_USD.slug,
+ }
+
+ # when
+ response = api_client.post_graphql(QUERY_QUANTITY_AVAILABLE, variables)
+
+ # then
+ content = get_graphql_content(response)
+ variant_data = content["data"]["productVariant"]
+ assert variant_data["quantityAvailable"] == stock_1.quantity
+
+
+def test_variant_quantity_available_no_channel_and_no_country_warehouse_without_zone(
+ staff_api_client,
+ variant_with_many_stocks,
+ channel_USD,
+ channel_PLN,
+ permission_manage_products,
+ permission_manage_discounts,
+ permission_manage_orders,
+):
+ """Ensure the warehouse without shipping zones is not counted in variant available
+ quantity."""
+ # given
+ assert variant_with_many_stocks.stocks.count() == 2
+
+ stock_1, stock_2 = variant_with_many_stocks.stocks.all()
+ # clear shipping zones one of variant warehouses
+ stock_2.warehouse.shipping_zones.clear()
+
+ staff_api_client.user.user_permissions.add(
+ permission_manage_products,
+ permission_manage_discounts,
+ permission_manage_orders,
+ )
+
+ variables = {
+ "id": graphene.Node.to_global_id("ProductVariant", variant_with_many_stocks.pk)
+ }
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_QUANTITY_AVAILABLE, variables)
+
+ # then
+ content = get_graphql_content(response)
+ variant_data = content["data"]["productVariant"]
+ assert variant_data["quantityAvailable"] == stock_1.quantity
+
+
+def test_variant_quantity_available_only_warehouse_without_zone_no_channel_no_country(
+ staff_api_client,
+ variant,
+ stock,
+ channel_USD,
+ channel_PLN,
+ permission_manage_products,
+ permission_manage_discounts,
+ permission_manage_orders,
+):
+ """Ensure the quantity available for variant if equal to 0 when there is only one
+ stock with warehouse without any shipping zone assigned.
+ """
+ # given
+ assert variant.stocks.count() == 1
+ # clear shipping zones for variant warehouses
+ stock.warehouse.shipping_zones.clear()
+
+ staff_api_client.user.user_permissions.add(
+ permission_manage_products,
+ permission_manage_discounts,
+ permission_manage_orders,
+ )
+
+ variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_QUANTITY_AVAILABLE, variables)
+
+ # then
+ content = get_graphql_content(response)
+ variant_data = content["data"]["productVariant"]
+ assert variant_data["quantityAvailable"] == 0
+
+
QUERY_VARIANT_AVAILABILITY = """
query variantAvailability(
$id: ID!, $country: CountryCode, $address: AddressInput, $channel: String
diff --git a/saleor/graphql/warehouse/dataloaders.py b/saleor/graphql/warehouse/dataloaders.py
index de5d0d461d..90cab86669 100644
--- a/saleor/graphql/warehouse/dataloaders.py
+++ b/saleor/graphql/warehouse/dataloaders.py
@@ -81,7 +81,6 @@ class AvailableQuantityByProductVariantIdCountryCodeAndChannelSlugLoader(
.using(self.database_connection_name)
.filter(product_variant_id__in=variant_ids)
)
- additional_warehouse_filter = True if country_code or channel_slug else False
warehouse_shipping_zones = self.get_warehouse_shipping_zones(
country_code, channel_slug
@@ -95,11 +94,11 @@ class AvailableQuantityByProductVariantIdCountryCodeAndChannelSlugLoader(
warehouse_shipping_zones_map[warehouse_shipping_zone.warehouse_id].append(
warehouse_shipping_zone.shippingzone_id
)
- if additional_warehouse_filter:
- stocks = stocks.filter(
- Q(warehouse_id__in=warehouse_shipping_zones_map.keys())
- | Q(warehouse_id__in=cc_warehouses.values("id"))
- )
+
+ stocks = stocks.filter(
+ Q(warehouse_id__in=warehouse_shipping_zones_map.keys())
+ | Q(warehouse_id__in=cc_warehouses.values("id"))
+ )
stocks = stocks.annotate_available_quantity()