Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Return product is_available based on available warehouses in given channel
IKarbowiak3501b7b4a4f7
diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.pyindex e2926a3a04..72661671ec 100644--- a/saleor/graphql/product/tests/test_product.py+++ b/saleor/graphql/product/tests/test_product.py@@ -1493,6 +1493,43 @@ def test_fetch_product_from_category_query(assert variant_channel_listing.cost_price.amount == variant_cost+def test_query_products_no_channel_shipping_zones(+ staff_api_client, product, permission_manage_products, stock, channel_USD+):+ channel_USD.shipping_zones.clear()+ category = Category.objects.first()+ product = category.products.first()+ query = """+ query CategoryProducts($id: ID, $channel: String, $address: AddressInput) {+ category(id: $id) {+ products(first: 20, channel: $channel) {+ edges {+ node {+ id+ name+ isAvailable(address: $address)+ }+ }+ }+ }+ }+ """+ staff_api_client.user.user_permissions.add(permission_manage_products)+ variables = {+ "id": graphene.Node.to_global_id("Category", category.id),+ "channel": channel_USD.slug,+ "address": {"country": "US"},+ }+ response = staff_api_client.post_graphql(query, variables)+ content = get_graphql_content(response)+ assert content["data"]["category"] is not None+ product_edges_data = content["data"]["category"]["products"]["edges"]+ assert len(product_edges_data) == category.products.count()+ product_data = product_edges_data[0]["node"]+ assert product_data["name"] == product.name+ assert product_data["isAvailable"] is False++def test_products_query_with_filter_attributes(query_products_with_filter,staff_api_client,diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.pyindex 05ec4dc530..974b2fe21e 100644--- a/saleor/graphql/product/types/products.py+++ b/saleor/graphql/product/types/products.py@@ -738,7 +738,7 @@ class Product(ChannelContextTypeWithMetadata, CountableDjangoObjectType):)def calculate_is_available(product_channel_listing):- in_stock = is_product_in_stock(root.node, country_code)+ in_stock = is_product_in_stock(root.node, country_code, channel_slug)is_visible = Falseif product_channel_listing:is_visible = product_channel_listing.is_available_for_purchase()diff --git a/saleor/warehouse/availability.py b/saleor/warehouse/availability.pyindex effc965193..a60f1bea77 100644--- a/saleor/warehouse/availability.py+++ b/saleor/warehouse/availability.py@@ -84,9 +84,11 @@ def get_available_quantity(variant: "ProductVariant", country_code: str) -> int:return _get_available_quantity(stocks)-def is_product_in_stock(product: "Product", country_code: str) -> bool:+def is_product_in_stock(+ product: "Product", country_code: str, channel_slug: str+) -> bool:"""Check if there is any variant of given product available in given country."""- stocks = Stock.objects.get_product_stocks_for_country(- country_code, product+ stocks = Stock.objects.get_product_stocks_for_country_and_channel(+ country_code, channel_slug, product).annotate_available_quantity()return any(stocks.values_list("available_quantity", flat=True))diff --git a/saleor/warehouse/models.py b/saleor/warehouse/models.pyindex c039bfed53..686d2a5380 100644--- a/saleor/warehouse/models.py+++ b/saleor/warehouse/models.py@@ -62,11 +62,12 @@ class StockQuerySet(models.QuerySet):- Coalesce(Sum("allocations__quantity_allocated"), 0))- def for_country(self, country_code: str):+ def for_country(self, country_code: str, channel_slug=None):+ filter_lookup = {"shipping_zones__countries__contains": country_code}+ if channel_slug is not None:+ filter_lookup["shipping_zones__channels__slug"] = channel_slugquery_warehouse = models.Subquery(- Warehouse.objects.filter(- shipping_zones__countries__contains=country_code- ).values("pk")+ Warehouse.objects.filter(**filter_lookup).values("pk"))return self.select_related("product_variant", "warehouse").filter(warehouse__in=query_warehouse@@ -81,8 +82,10 @@ class StockQuerySet(models.QuerySet):"""return self.for_country(country_code).filter(product_variant=product_variant)- def get_product_stocks_for_country(self, country_code: str, product: Product):- return self.for_country(country_code).filter(+ def get_product_stocks_for_country_and_channel(+ self, country_code: str, channel_slug: str, product: Product+ ):+ return self.for_country(country_code, channel_slug).filter(product_variant__product_id=product.pk)