Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Use channel in filtering by product availability
IKarbowiakd853433f2784
diff --git a/saleor/graphql/product/filters.py b/saleor/graphql/product/filters.pyindex 71ea1f1218..c5d7c7e68d 100644--- a/saleor/graphql/product/filters.py+++ b/saleor/graphql/product/filters.py@@ -137,9 +137,10 @@ def filter_products_by_collections(qs, collections):return qs.filter(collections__in=collections)-def filter_products_by_stock_availability(qs, stock_availability):+def filter_products_by_stock_availability(qs, stock_availability, channel_slug):total_stock = (- Stock.objects.select_related("product_variant")+ Stock.objects.for_channel(channel_slug)+ .select_related("product_variant").values("product_variant__product_id").annotate(total_quantity_allocated=Coalesce(Sum("allocations__quantity_allocated"), 0)@@ -211,9 +212,9 @@ def _filter_minimal_price(qs, _, value, channel_slug):return qs-def _filter_stock_availability(qs, _, value):+def _filter_stock_availability(qs, _, value, channel_slug):if value:- qs = filter_products_by_stock_availability(qs, value)+ qs = filter_products_by_stock_availability(qs, value, channel_slug)return qs@@ -359,9 +360,6 @@ class ProductFilter(django_filters.FilterSet):def filter_attributes(self, queryset, name, value):return _filter_attributes(queryset, name, value)- def filter_stock_availability(self, queryset, name, value):- return _filter_stock_availability(queryset, name, value)-def filter_variant_price(self, queryset, name, value):channel_slug = get_channel_slug_from_filter_data(self.data)return _filter_variant_price(queryset, name, value, channel_slug)@@ -374,6 +372,10 @@ class ProductFilter(django_filters.FilterSet):channel_slug = get_channel_slug_from_filter_data(self.data)return _filter_is_published(queryset, name, value, channel_slug)+ def filter_stock_availability(self, queryset, name, value):+ channel_slug = get_channel_slug_from_filter_data(self.data)+ return _filter_stock_availability(queryset, name, value, channel_slug)+class ProductVariantFilter(django_filters.FilterSet):search = django_filters.CharFilter(diff --git a/saleor/graphql/product/resolvers.py b/saleor/graphql/product/resolvers.pyindex 94f949d9c1..7adda5c9af 100644--- a/saleor/graphql/product/resolvers.py+++ b/saleor/graphql/product/resolvers.py@@ -68,7 +68,7 @@ def resolve_products() -> ChannelQsContext:qs = models.Product.objects.visible_to_user(requestor, channel_slug)if stock_availability:- qs = filter_products_by_stock_availability(qs, stock_availability)+ qs = filter_products_by_stock_availability(qs, stock_availability, channel_slug)if not requestor_is_staff_member_or_app(requestor):qs = qs.annotate_visible_in_listings(channel_slug).exclude(visible_in_listings=Falsediff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.pyindex 72661671ec..1f6e16fb92 100644--- a/saleor/graphql/product/tests/test_product.py+++ b/saleor/graphql/product/tests/test_product.py@@ -1807,7 +1807,8 @@ def test_products_query_with_filter(channel=channel_USD,is_published=False,)- variables = {"filter": products_filter, "channel": channel_USD.slug}+ products_filter["channel"] = channel_USD.slug+ variables = {"filter": products_filter}staff_api_client.user.user_permissions.add(permission_manage_products)response = staff_api_client.post_graphql(query_products_with_filter, variables)content = get_graphql_content(response)@@ -1909,7 +1910,6 @@ def test_products_query_with_filter_stock_availability_as_staff(product.variants.first().channel_listings.filter(channel=channel_USD).update(price_amount=None)-variables = {"filter": {"stockAvailability": "OUT_OF_STOCK", "channel": channel_USD.slug}}@@ -1957,6 +1957,30 @@ def test_products_query_with_filter_stock_availability_as_user(assert products[1]["node"]["name"] == product_list[2].name+def test_products_query_with_filter_stock_availability_channel_without_shipping_zones(+ query_products_with_filter,+ staff_api_client,+ product,+ order_line,+ permission_manage_products,+ channel_USD,+):+ channel_USD.shipping_zones.clear()+ stock = product.variants.first().stocks.first()+ Allocation.objects.create(+ order_line=order_line, stock=stock, quantity_allocated=stock.quantity+ )+ variables = {+ "filter": {"stockAvailability": "OUT_OF_STOCK", "channel": channel_USD.slug}+ }+ staff_api_client.user.user_permissions.add(permission_manage_products)+ response = staff_api_client.post_graphql(query_products_with_filter, variables)+ content = get_graphql_content(response)+ products = content["data"]["products"]["edges"]++ assert len(products) == 0++@pytest.mark.parametrize("quantity_input, warehouse_indexes, count, indexes_of_products_in_result",[diff --git a/saleor/graphql/product/tests/test_product_pagination.py b/saleor/graphql/product/tests/test_product_pagination.pyindex 78e6206e06..b81300a11f 100644--- a/saleor/graphql/product/tests/test_product_pagination.py+++ b/saleor/graphql/product/tests/test_product_pagination.py@@ -698,9 +698,11 @@ def test_products_pagination_with_filtering(staff_api_client,permission_manage_products,products_for_pagination,+ channel_USD,):page_size = 2+ filter_by["channel"] = channel_USD.slugvariables = {"first": page_size, "after": None, "filter": filter_by}response = staff_api_client.post_graphql(QUERY_PRODUCTS_PAGINATION,