From dfb1d2075b8c281e58cb9ed9133abdcf75666fcc Mon Sep 17 00:00:00 2001 From: Simon Brulhart Date: Wed, 27 Nov 2019 13:24:36 +0100 Subject: [PATCH] Consider products with all their stock allocated as out-of-stock --- CHANGELOG.md | 1 + saleor/graphql/product/filters.py | 8 +++++--- tests/api/test_product.py | 3 ++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bdcfb4dcf0..460de9c99ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ All notable, unreleased changes to this project will be documented in this file. - Fixed serialization error on weight fields when running `loaddata` and `dumpdb` - #5005 by @NyanKiyoshi - Fixed JSON encoding error on Google Analytics reporting - #5004 by @NyanKiyoshi - Create custom field to translation, use new translation types in translations query - #5007 by @fowczarek +- Take allocated stock in account in `StockAvailability` filter - #5019 by @simonbru ## 2.9.0 diff --git a/saleor/graphql/product/filters.py b/saleor/graphql/product/filters.py index 359813e4d1f..2ea8b83322a 100644 --- a/saleor/graphql/product/filters.py +++ b/saleor/graphql/product/filters.py @@ -99,11 +99,13 @@ def sort_qs(qs, sort_by): def filter_products_by_stock_availability(qs, stock_availability): - qs = qs.annotate(total_quantity=Sum("variants__quantity")) + qs = qs.annotate( + total_available=Sum("variants__quantity") - Sum("variants__quantity_allocated") + ) if stock_availability == StockAvailability.IN_STOCK: - qs = qs.filter(total_quantity__gt=0) + qs = qs.filter(total_available__gt=0) elif stock_availability == StockAvailability.OUT_OF_STOCK: - qs = qs.filter(total_quantity=0) + qs = qs.filter(total_available__lte=0) return qs diff --git a/tests/api/test_product.py b/tests/api/test_product.py index fc2224713e2..9ded6c1ad92 100644 --- a/tests/api/test_product.py +++ b/tests/api/test_product.py @@ -216,7 +216,8 @@ def test_product_query(staff_api_client, product, permission_manage_products): [ ("IN_STOCK", 5, 1), ("OUT_OF_STOCK", 0, 1), - ("OUT_OF_STOCK", 1, 0), + ("OUT_OF_STOCK", 1, 1), + ("OUT_OF_STOCK", 2, 0), ("IN_STOCK", 0, 0), ], )