Skip to content

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

Fix stock events triggers (#11714)

Jakubded69f9c7097
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7e19f3770b..b51ed8ae19 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -43,6 +43,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Remove `CUSTOMER_UPDATED` webhook trigger from address mutations - #11395 by @jakubkuc
- Drop `Django.Auth` - #11305 by @fowczarek
- Propagate voucher discount between checkout lines when charge_taxes is disabled - #11632 by @maarcingebala
+- Fix stock events triggers - #11714 by @jakubkuc
- Accept the gift card code provided in the input - by @mociepka
# 3.9.0
diff --git a/saleor/graphql/product/bulk_mutations/products.py b/saleor/graphql/product/bulk_mutations/products.py
index 97c40eb55e..f8fddc5a1b 100644
--- a/saleor/graphql/product/bulk_mutations/products.py
+++ b/saleor/graphql/product/bulk_mutations/products.py
@@ -890,13 +890,18 @@ class ProductVariantStocksUpdate(ProductVariantStocksCreate):
stock, is_created = warehouse_models.Stock.objects.get_or_create(
product_variant=variant, warehouse=warehouse
)
-
- if is_created or (stock.quantity <= 0 and stock_data["quantity"] > 0):
+ if is_created or (
+ (stock.quantity - stock.quantity_allocated)
+ <= 0
+ < stock_data["quantity"]
+ ):
transaction.on_commit(
lambda: manager.product_variant_back_in_stock(stock)
)
- if stock_data["quantity"] <= 0:
+ if stock_data["quantity"] <= 0 or (
+ stock_data["quantity"] - stock.quantity_allocated <= 0
+ ):
transaction.on_commit(
lambda: manager.product_variant_out_of_stock(stock)
)
diff --git a/saleor/graphql/product/tests/mutations/test_product_variant_stocks_update.py b/saleor/graphql/product/tests/mutations/test_product_variant_stocks_update.py
index 153ac81b8c..23abf88a22 100644
--- a/saleor/graphql/product/tests/mutations/test_product_variant_stocks_update.py
+++ b/saleor/graphql/product/tests/mutations/test_product_variant_stocks_update.py
@@ -360,6 +360,72 @@ def test_update_or_create_variant_with_back_in_stock_webhooks_only_failed(
)
+@patch("saleor.plugins.manager.PluginsManager.product_variant_back_in_stock")
+@patch("saleor.plugins.manager.PluginsManager.product_variant_out_of_stock")
+def test_update_or_create_variant_with_back_in_stock_webhooks_with_allocations(
+ product_variant_stock_out_of_stock_webhook,
+ product_variant_back_in_stock_webhook,
+ settings,
+ variant,
+ warehouse,
+):
+ stock_quantity = 4
+ stock = Stock.objects.create(
+ warehouse=warehouse, product_variant=variant, quantity=stock_quantity
+ )
+
+ # given
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+ plugins = get_plugins_manager()
+ stock.quantity_allocated = stock_quantity
+ stock.save(update_fields=["quantity_allocated"])
+ stocks_data = [
+ {"quantity": stock.quantity_allocated + 1},
+ ]
+
+ # when
+ ProductVariantStocksUpdate.update_or_create_variant_stocks(
+ variant, stocks_data, [warehouse], plugins
+ )
+ # then
+ flush_post_commit_hooks()
+ product_variant_back_in_stock_webhook.assert_called_once_with(stock)
+ product_variant_stock_out_of_stock_webhook.assert_not_called()
+
+
+@patch("saleor.plugins.manager.PluginsManager.product_variant_back_in_stock")
+@patch("saleor.plugins.manager.PluginsManager.product_variant_out_of_stock")
+def test_update_or_create_variant_with_out_of_stock_webhooks_with_allocations(
+ product_variant_stock_out_of_stock_webhook,
+ product_variant_back_in_stock_webhook,
+ settings,
+ variant,
+ warehouse,
+):
+ stock_quantity = 4
+ stock = Stock.objects.create(
+ warehouse=warehouse, product_variant=variant, quantity=stock_quantity
+ )
+
+ # given
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+ plugins = get_plugins_manager()
+ stock.quantity_allocated = stock_quantity - 1
+ stock.save(update_fields=["quantity_allocated"])
+ stocks_data = [
+ {"quantity": stock.quantity_allocated},
+ ]
+
+ # when
+ ProductVariantStocksUpdate.update_or_create_variant_stocks(
+ variant, stocks_data, [warehouse], plugins
+ )
+ # then
+ flush_post_commit_hooks()
+ product_variant_stock_out_of_stock_webhook.assert_called_once_with(stock)
+ product_variant_back_in_stock_webhook.assert_not_called()
+
+
@patch("saleor.plugins.manager.PluginsManager.product_variant_back_in_stock")
@patch("saleor.plugins.manager.PluginsManager.product_variant_out_of_stock")
def test_update_or_create_variant_stocks_with_out_of_stock_webhook_only(