Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Check variants availability per product

Mira Szewczyk661a40bf37a8
diff --git a/saleor/stock/models.py b/saleor/stock/models.py
index 276bdd3067..b1ad374748 100644
--- a/saleor/stock/models.py
+++ b/saleor/stock/models.py
@@ -55,7 +55,7 @@ class Stock(models.Model):
@property
def is_available(self):
- return self.quantity > 0
+ return self.quantity_available > 0
def check_quantity(self, quantity: int):
if quantity > self.quantity_available():
diff --git a/saleor/stock/utils/availability.py b/saleor/stock/utils/availability.py
index 2be2c70bd9..fca0b0e6a6 100644
--- a/saleor/stock/utils/availability.py
+++ b/saleor/stock/utils/availability.py
@@ -1,10 +1,11 @@
from typing import TYPE_CHECKING
from ...core.exceptions import InsufficientStock
+from ...product.models import Product
from ..models import Stock
if TYPE_CHECKING:
- from ...product.models import Product, ProductVariant
+ from ...product.models import ProductVariant
def check_stock_quantity(variant: "ProductVariant", country_code: str, quantity: int):
@@ -26,16 +27,34 @@ def get_available_quantity(variant: "ProductVariant", country_code: str) -> int:
def is_variant_in_stock(variant: "ProductVariant", country_code: str) -> bool:
- stocks = Stock.objects.for_country(country_code).filter(
- product_variant__pk=variant.pk
+ stock = Stock.objects.for_country(country_code).get(product_variant__pk=variant.pk)
+ return stock.is_available
+
+
+def stocks_for_product(product: "Product", country_code: str):
+ return (
+ Stock.objects.annotate_available_quantity()
+ .for_country(country_code)
+ .filter(product_variant__product_id=product.pk)
)
- return stocks.exists()
def is_product_in_stock(product: "Product", country_code: str) -> bool:
- stocks = (
- Stock.objects.select_related("product_variant__product")
- .for_country(country_code)
- .filter(product_variant__product=product)
+ return any(
+ stocks_for_product(product, country_code).values_list(
+ "available_quantity", flat=True
+ )
)
- return stocks.exists()
+
+
+def are_all_product_variants_in_stock(product: "Product", country_code: str) -> bool:
+ product_stocks = (
+ stocks_for_product(product, country_code)
+ .values_list("available_quantity", "product_variant_id")
+ .all()
+ )
+ are_all_available = all([elem[0] for elem in product_stocks])
+ variants_with_stocks = [elem[1] for elem in product_stocks]
+
+ product_variants = product.variants.exclude(id__in=variants_with_stocks).exists()
+ return are_all_available and not product_variants
diff --git a/tests/test_stock.py b/tests/test_stock.py
index 58797953c0..4a084f054a 100644
--- a/tests/test_stock.py
+++ b/tests/test_stock.py
@@ -2,7 +2,10 @@ import pytest
from saleor.core.exceptions import InsufficientStock
from saleor.stock.models import Stock
-from saleor.stock.utils.availability import check_stock_quantity
+from saleor.stock.utils.availability import (
+ are_all_product_variants_in_stock,
+ check_stock_quantity,
+)
COUNTRY_CODE = "US"
@@ -38,3 +41,20 @@ def test_check_stock_quantity_is_not_sufficient(product):
new_quantity = stock.quantity_available + 1
with pytest.raises(InsufficientStock):
check_stock_quantity(variant, COUNTRY_CODE, new_quantity)
+
+
+def test_are_all_product_variants_in_stock_all_in_stock(product):
+ assert are_all_product_variants_in_stock(product, COUNTRY_CODE)
+
+
+def test_are_all_product_variants_in_stock_stock_empty(product):
+ stock = Stock.objects.first()
+ stock.quantity_allocated = stock.quantity
+ stock.save(update_fields=["quantity_allocated"])
+
+ assert not are_all_product_variants_in_stock(product, COUNTRY_CODE)
+
+
+def test_are_all_product_variants_in_stock_lack_of_stocks(product):
+ Stock.objects.all().delete()
+ assert not are_all_product_variants_in_stock(product, COUNTRY_CODE)