Skip to content

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

Trigger product_updated webhook for categories

Damian Wfd8992d75836
diff --git a/saleor/graphql/product/bulk_mutations/products.py b/saleor/graphql/product/bulk_mutations/products.py
index 2d6b6cebea..3f7ce2162d 100644
--- a/saleor/graphql/product/bulk_mutations/products.py
+++ b/saleor/graphql/product/bulk_mutations/products.py
@@ -53,8 +53,19 @@ class CategoryBulkDelete(ModelBulkDeleteMutation):
error_type_field = "product_errors"
@classmethod
- def bulk_action(cls, queryset):
- delete_categories(queryset.values_list("pk", flat=True))
+ def perform_mutation(cls, _root, info, ids, **data):
+ response = super().perform_mutation(
+ _root,
+ info,
+ ids,
+ manager=info.context.plugins,
+ **data,
+ )
+ return response
+
+ @classmethod
+ def bulk_action(cls, queryset, manager):
+ delete_categories(queryset.values_list("pk", flat=True), manager)
class CollectionBulkDelete(ModelBulkDeleteMutation):
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 7354053a2a..0396a92eac 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -160,7 +160,7 @@ class CategoryDelete(ModelDeleteMutation):
db_id = instance.id
- delete_categories([db_id])
+ delete_categories([db_id], manager=info.context.plugins)
instance.id = db_id
return cls.success_response(instance)
diff --git a/saleor/graphql/product/tests/test_bulk_delete.py b/saleor/graphql/product/tests/test_bulk_delete.py
index 6465bbe595..b552f34d1b 100644
--- a/saleor/graphql/product/tests/test_bulk_delete.py
+++ b/saleor/graphql/product/tests/test_bulk_delete.py
@@ -66,6 +66,44 @@ def test_delete_categories(staff_api_client, category_list, permission_manage_pr
).exists()
+@patch("saleor.plugins.manager.PluginsManager.product_updated")
+def test_delete_categories_trigger_product_updated_webhook(
+ product_updated_mock,
+ staff_api_client,
+ category_list,
+ product_list,
+ permission_manage_products,
+):
+ first_product = product_list[0]
+ first_product.category = category_list[0]
+ first_product.save()
+
+ second_product = product_list[1]
+ second_product.category = category_list[1]
+ second_product.save()
+
+ variables = {
+ "ids": [
+ graphene.Node.to_global_id("Category", category.id)
+ for category in category_list
+ ]
+ }
+ response = staff_api_client.post_graphql(
+ MUTATION_CATEGORY_BULK_DELETE,
+ variables,
+ permissions=[permission_manage_products],
+ )
+ content = get_graphql_content(response)
+
+ assert content["data"]["categoryBulkDelete"]["count"] == 3
+ assert not Category.objects.filter(
+ id__in=[category.id for category in category_list]
+ ).exists()
+
+ # updated two categories with products
+ assert product_updated_mock.call_count == 2
+
+
@patch("saleor.product.utils.update_products_discounted_prices_task")
def test_delete_categories_with_subcategories_and_products(
mock_update_products_discounted_prices_task,
diff --git a/saleor/product/tests/test_category.py b/saleor/product/tests/test_category.py
index 13da34cce0..8ca052cfb6 100644
--- a/saleor/product/tests/test_category.py
+++ b/saleor/product/tests/test_category.py
@@ -1,5 +1,6 @@
from unittest.mock import patch
+from ...plugins.manager import get_plugins_manager
from ..models import Category
from ..utils import collect_categories_tree_products, delete_categories
@@ -26,7 +27,7 @@ def test_delete_categories(
child = parent.children.first()
product_list = [child.products.first(), parent.products.first()]
- delete_categories([parent.pk])
+ delete_categories([parent.pk], manager=get_plugins_manager())
assert not Category.objects.filter(
id__in=[category.id for category in [parent, child]]
@@ -43,3 +44,21 @@ def test_delete_categories(
for product_channel_listing in product.channel_listings.all():
assert not product_channel_listing.is_published
assert not product_channel_listing.publication_date
+
+
+@patch("saleor.plugins.manager.PluginsManager.product_updated")
+def test_delete_categories_trigger_product_updated_webhook(
+ product_updated_mock,
+ categories_tree_with_published_products,
+):
+ parent = categories_tree_with_published_products
+ child = parent.children.first()
+ product_list = [child.products.first(), parent.products.first()]
+
+ delete_categories([parent.pk], manager=get_plugins_manager())
+
+ assert not Category.objects.filter(
+ id__in=[category.id for category in [parent, child]]
+ ).exists()
+
+ assert len(product_list) == product_updated_mock.call_count
diff --git a/saleor/product/utils/__init__.py b/saleor/product/utils/__init__.py
index 6d08ab1d4e..c2328940da 100644
--- a/saleor/product/utils/__init__.py
+++ b/saleor/product/utils/__init__.py
@@ -33,7 +33,7 @@ def calculate_revenue_for_variant(
@transaction.atomic
-def delete_categories(categories_ids: List[str]):
+def delete_categories(categories_ids: List[str], manager):
"""Delete categories and perform all necessary actions.
Set products of deleted categories as unpublished, delete categories
@@ -51,8 +51,12 @@ def delete_categories(categories_ids: List[str]):
ProductChannelListing.objects.filter(product__in=products).update(
is_published=False, publication_date=None
)
- product_ids = list(products.values_list("id", flat=True))
+ products = list(products)
categories.delete()
+ product_ids = [product.id for product in products]
+ for product in products:
+ manager.product_updated(product)
+
update_products_discounted_prices_task.delay(product_ids=product_ids)