Skip to content

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

fix updating product discounted price update all products (#13859)

Filip Owczarek28102aba0d7c
diff --git a/saleor/product/tests/test_product_minimal_variant_price.py b/saleor/product/tests/test_product_minimal_variant_price.py
index e2f7bb5484..1a975da5bb 100644
--- a/saleor/product/tests/test_product_minimal_variant_price.py
+++ b/saleor/product/tests/test_product_minimal_variant_price.py
@@ -102,6 +102,77 @@ def test_update_products_discounted_prices_of_catalogues_for_product(
assert variant_channel_listing.discounted_price == variant_channel_listing.price
+@patch("saleor.product.utils.variant_prices.update_products_discounted_prices")
+def test_update_products_discounted_prices_of_catalogues_via_products(
+ mock_update_products_discounted_prices, product, product_list
+):
+ # given
+
+ # when
+ update_products_discounted_prices_of_catalogues(
+ product_ids=[product.pk, product_list[0].pk]
+ )
+
+ # then
+ mock_update_products_discounted_prices.assert_called_once()
+ args, _kwargs = mock_update_products_discounted_prices.call_args
+ assert set(args[0].values_list("pk", flat=True)) == {product.pk, product_list[0].pk}
+
+
+@patch("saleor.product.utils.variant_prices.update_products_discounted_prices")
+def test_update_products_discounted_prices_of_catalogues_via_category(
+ mock_update_products_discounted_prices,
+ product,
+ product_list,
+ category_with_image,
+ category,
+):
+ # given
+ assert category.pk != category_with_image.pk
+ product.category = category_with_image
+ product.save()
+
+ # when
+ update_products_discounted_prices_of_catalogues(category_ids=[category.pk])
+
+ # then
+ mock_update_products_discounted_prices.assert_called_once()
+ args, _kwargs = mock_update_products_discounted_prices.call_args
+ assert set(args[0].values_list("pk", flat=True)) == {p.pk for p in product_list}
+
+
+@patch("saleor.product.utils.variant_prices.update_products_discounted_prices")
+def test_update_products_discounted_prices_of_catalogues_via_collection(
+ mock_update_products_discounted_prices, product, product_list, collection
+):
+ # given
+ collection.products.add(*product_list)
+
+ # when
+ update_products_discounted_prices_of_catalogues(collection_ids=[collection.pk])
+
+ # then
+ mock_update_products_discounted_prices.assert_called_once()
+ args, _kwargs = mock_update_products_discounted_prices.call_args
+ assert set(args[0].values_list("pk", flat=True)) == {p.pk for p in product_list}
+
+
+@patch("saleor.product.utils.variant_prices.update_products_discounted_prices")
+def test_update_products_discounted_prices_of_catalogues_via_variants(
+ mock_update_products_discounted_prices, product, product_list
+):
+ # given
+ variant_ids = [p.variants.first().pk for p in product_list]
+
+ # when
+ update_products_discounted_prices_of_catalogues(variant_ids=variant_ids)
+
+ # then
+ mock_update_products_discounted_prices.assert_called_once()
+ args, _kwargs = mock_update_products_discounted_prices.call_args
+ assert set(args[0].values_list("pk", flat=True)) == {p.pk for p in product_list}
+
+
def test_update_products_discounted_prices_of_catalogues_for_category(
category, product, channel_USD
):
diff --git a/saleor/product/utils/variant_prices.py b/saleor/product/utils/variant_prices.py
index ca92c12d15..8aa245ae3a 100644
--- a/saleor/product/utils/variant_prices.py
+++ b/saleor/product/utils/variant_prices.py
@@ -188,7 +188,8 @@ def update_products_discounted_prices_of_catalogues(
)
lookup |= Q(Exists(collection_products.filter(product_id=OuterRef("id"))))
if variant_ids:
- lookup |= Q(Exists(ProductVariant.objects.filter(product_id=OuterRef("id"))))
+ variants = ProductVariant.objects.filter(id__in=variant_ids)
+ lookup |= Q(Exists(variants.filter(product_id=OuterRef("id"))))
if lookup:
products = Product.objects.filter(lookup)
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index b46beb766e..5c5cd573be 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -2281,7 +2281,7 @@ def category(category_generator): # pylint: disable=W0613
@pytest.fixture
def category_with_image(db, image, media_root): # pylint: disable=W0613
return Category.objects.create(
- name="Default", slug="default", background_image=image
+ name="Default2", slug="default2", background_image=image
)