Skip to content

Commit

Permalink
Merge pull request saleor#5019 from simonbru/fix_stock_filter
Browse files Browse the repository at this point in the history
Consider products with all their stock allocated as out of stock
  • Loading branch information
maarcingebala authored Nov 27, 2019
2 parents e31d7bf + dfb1d20 commit 5f1437b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 5 additions & 3 deletions saleor/graphql/product/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
3 changes: 2 additions & 1 deletion tests/api/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
],
)
Expand Down

0 comments on commit 5f1437b

Please sign in to comment.