Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Include reserved stocks in product availability (#10965)
Piotr Zabieglik06d25ce29c98
diff --git a/saleor/graphql/product/filters.py b/saleor/graphql/product/filters.pyindex 27b19c870a..087badc7b7 100644--- a/saleor/graphql/product/filters.py+++ b/saleor/graphql/product/filters.py@@ -34,7 +34,7 @@ from ...product.models import (ProductVariantChannelListing,)from ...product.search import search_products-from ...warehouse.models import Allocation, Stock, Warehouse+from ...warehouse.models import Allocation, Reservation, Stock, Warehousefrom ..channel.filters import get_channel_slug_from_filter_datafrom ..core.descriptions import ADDED_IN_38from ..core.filters import (@@ -338,9 +338,23 @@ def filter_products_by_stock_availability(qs, stock_availability, channel_slug):)allocated_subquery = Subquery(queryset=allocations, output_field=IntegerField())+ reservations = (+ Reservation.objects.values("stock_id")+ .filter(+ quantity_reserved__gt=0,+ stock_id=OuterRef("pk"),+ reserved_until__gt=timezone.now(),+ )+ .values_list(Sum("quantity_reserved"))+ )+ reservation_subquery = Subquery(queryset=reservations, output_field=IntegerField())+stocks = (Stock.objects.for_channel_and_country(channel_slug)- .filter(quantity__gt=Coalesce(allocated_subquery, 0))+ .filter(+ quantity__gt=Coalesce(allocated_subquery, 0)+ + Coalesce(reservation_subquery, 0)+ ).values("product_variant_id"))variants = ProductVariant.objects.filter(diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.pyindex 2dfc1745d1..d4e297fff2 100644--- a/saleor/graphql/product/tests/test_product.py+++ b/saleor/graphql/product/tests/test_product.py@@ -53,7 +53,7 @@ from ....product.utils.availability import get_variant_availabilityfrom ....product.utils.costs import get_product_costs_datafrom ....tests.utils import dummy_editorjs, flush_post_commit_hooksfrom ....thumbnail.models import Thumbnail-from ....warehouse.models import Allocation, Stock, Warehouse+from ....warehouse.models import Allocation, Reservation, Stock, Warehousefrom ....webhook.event_types import WebhookEventAsyncTypefrom ....webhook.payloads import generate_product_deleted_payloadfrom ...core.enums import AttributeErrorCode, ReportingPeriod, ThumbnailFormatEnum@@ -3480,6 +3480,67 @@ def test_products_query_with_filter_stock_availability_as_staff(assert len(products) == 3+def test_products_query_with_filter_stock_availability_including_reservations(+ query_products_with_filter,+ staff_api_client,+ product_list,+ order_line,+ checkout_line,+ permission_manage_products,+ channel_USD,+ warehouse_JPY,+ stock,+):+ # given+ stocks = [product.variants.first().stocks.first() for product in product_list]+ stock.quantity = 50+ stock.product_variant = stocks[2].product_variant+ stock.warehouse_id = warehouse_JPY.id+ stocks[2].quantity = 50++ Allocation.objects.create(+ order_line=order_line, stock=stocks[0], quantity_allocated=50+ )+ Reservation.objects.bulk_create(+ [+ Reservation(+ checkout_line=checkout_line,+ stock=stocks[0],+ quantity_reserved=50,+ reserved_until=timezone.now() + timedelta(minutes=5),+ ),+ Reservation(+ checkout_line=checkout_line,+ stock=stocks[1],+ quantity_reserved=100,+ reserved_until=timezone.now() - timedelta(minutes=5),+ ),+ Reservation(+ checkout_line=checkout_line,+ stock=stocks[2],+ quantity_reserved=50,+ reserved_until=timezone.now() + timedelta(minutes=5),+ ),+ ]+ )+ variables = {+ "filter": {"stockAvailability": "OUT_OF_STOCK"},+ "channel": channel_USD.slug,+ }+ staff_api_client.user.user_permissions.add(permission_manage_products)++ # when+ response = staff_api_client.post_graphql(query_products_with_filter, variables)++ # then+ content = get_graphql_content(response)+ products = content["data"]["products"]["edges"]+ assert len(products) == 1+ assert products[0]["node"]["id"] == graphene.Node.to_global_id(+ "Product", product_list[0].id+ )++def test_products_query_with_filter_stock_availability_as_user(query_products_with_filter,user_api_client,