Skip to content

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

Fixed crash when no stock info is available to know if the product is in stock

Mikail KOCAKbce08eb47cf1
diff --git a/saleor/warehouse/availability.py b/saleor/warehouse/availability.py
index f6b4eba9c0..cc66da90c0 100644
--- a/saleor/warehouse/availability.py
+++ b/saleor/warehouse/availability.py
@@ -7,7 +7,7 @@ from ..core.exceptions import InsufficientStock
from .models import Stock
if TYPE_CHECKING:
- from ...product.models import Product, ProductVariant
+ from ..product.models import Product, ProductVariant
def check_stock_quantity(variant: "ProductVariant", country_code: str, quantity: int):
@@ -44,8 +44,8 @@ def get_quantity_allocated(variant: "ProductVariant", country_code: str) -> int:
def is_variant_in_stock(variant: "ProductVariant", country_code: str) -> bool:
"""Check if variant is available in given country."""
- stock = Stock.objects.get_variant_stock_for_country(country_code, variant)
- return stock.is_available
+ quantity_available = get_available_quantity(variant, country_code)
+ return quantity_available > 0
def stocks_for_product(product: "Product", country_code: str):
diff --git a/saleor/warehouse/models.py b/saleor/warehouse/models.py
index 27d1f7eaa1..9b948f2f09 100644
--- a/saleor/warehouse/models.py
+++ b/saleor/warehouse/models.py
@@ -68,6 +68,10 @@ class StockQuerySet(models.QuerySet):
def get_variant_stock_for_country(
self, country_code: str, product_variant: ProductVariant
):
+ """Return the stock information about the a stock for a given country.
+
+ Note it will raise a 'Stock.DoesNotExist' exception if no such stock is found.
+ """
return self.for_country(country_code).get(product_variant=product_variant)
def get_or_create_for_country(
@@ -100,10 +104,6 @@ class Stock(models.Model):
def quantity_available(self) -> int:
return max(self.quantity - self.quantity_allocated, 0)
- @property
- def is_available(self):
- return self.quantity_available > 0
-
def check_quantity(self, quantity: int):
if quantity > self.quantity_available:
raise InsufficientStock(self)
diff --git a/tests/conftest.py b/tests/conftest.py
index b122d438ae..245b8a4aa3 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -770,7 +770,7 @@ def product_with_default_variant(product_type_without_variant, category, warehou
@pytest.fixture
-def variant(product):
+def variant(product) -> ProductVariant:
product_variant = ProductVariant.objects.create(
product=product, sku="SKU_A", cost_price=Money(1, "USD")
)
diff --git a/tests/test_data_feeds.py b/tests/test_data_feeds.py
index 1f0ee8c8b7..4469d25de7 100644
--- a/tests/test_data_feeds.py
+++ b/tests/test_data_feeds.py
@@ -7,6 +7,7 @@ from django.utils.encoding import smart_text
from saleor.data_feeds.google_merchant import (
get_feed_items,
item_attributes,
+ item_availability,
item_google_product_category,
write_feed,
)
@@ -38,6 +39,11 @@ def test_saleor_feed_items(product, site_settings):
assert attributes.get("availability") == "in stock"
+def test_saleor_get_feed_items_having_no_stock_info(variant, site_settings):
+ variant.stock.all().delete()
+ assert item_availability(variant) == "out of stock"
+
+
def test_category_formatter(db):
main_category = Category(name="Main", slug="main")
main_category.save()