Skip to content

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

Fix getting stock from default warehouse

Marcin Gębala10e0cbed8141
diff --git a/saleor/warehouse/availability.py b/saleor/warehouse/availability.py
index 50ccea0cd6..d0cc87b515 100644
--- a/saleor/warehouse/availability.py
+++ b/saleor/warehouse/availability.py
@@ -32,7 +32,7 @@ def _get_available_quantity(stocks: StockQuerySet) -> int:
def check_stock_quantity(variant: "ProductVariant", country_code: str, quantity: int):
"""Validate if there is stock available for given variant in given country.
- If so - returns None. If there is less stock then required rise InsufficientStock
+ If so - returns None. If there is less stock then required raise InsufficientStock
exception.
"""
stocks = Stock.objects.get_variant_stocks_for_country(country_code, variant)
diff --git a/saleor/warehouse/models.py b/saleor/warehouse/models.py
index 26a7c30be6..c25743c7ef 100644
--- a/saleor/warehouse/models.py
+++ b/saleor/warehouse/models.py
@@ -3,7 +3,7 @@ import uuid
from typing import Set
from django.db import models
-from django.db.models import F, Sum
+from django.db.models import F, Q, Sum
from django.db.models.functions import Coalesce
from ..account.models import Address
@@ -29,12 +29,10 @@ class Warehouse(models.Model):
name = models.CharField(max_length=250)
slug = models.SlugField(max_length=255, unique=True)
company_name = models.CharField(blank=True, max_length=255)
-
shipping_zones = models.ManyToManyField(
ShippingZone, blank=True, related_name="warehouses"
)
address = models.ForeignKey(Address, on_delete=models.PROTECT)
-
email = models.EmailField(blank=True, default="")
objects = WarehouseQueryset.as_manager()
@@ -66,7 +64,8 @@ class StockQuerySet(models.QuerySet):
def for_country(self, country_code: str):
query_warehouse = models.Subquery(
Warehouse.objects.filter(
- shipping_zones__countries__contains=country_code
+ Q(shipping_zones__countries__contains=country_code)
+ | Q(shipping_zones__default=True)
).values("pk")
)
return self.select_related("product_variant", "warehouse").filter(
diff --git a/saleor/warehouse/tests/test_stock.py b/saleor/warehouse/tests/test_stock.py
index ab223a1bf7..cddabba133 100644
--- a/saleor/warehouse/tests/test_stock.py
+++ b/saleor/warehouse/tests/test_stock.py
@@ -18,6 +18,18 @@ def test_stocks_for_country(variant_with_many_stocks):
assert COUNTRY_CODE in warehouse2.countries
+def test_stocks_for_default_shipping_zone(variant, warehouse):
+ shipping_zone = warehouse.shipping_zones.get()
+ shipping_zone.countries = []
+ shipping_zone.default = True
+ shipping_zone.save(update_fields=["countries", "default"])
+ Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=1)
+ warehouse.refresh_from_db()
+
+ stock_qs = Stock.objects.for_country("PL")
+ assert stock_qs
+
+
def test_stock_for_country_does_not_exists(product, warehouse):
shipping_zone = warehouse.shipping_zones.first()
shipping_zone.countries = [COUNTRY_CODE]