Skip to content

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

Add product_updated webhook, plugin entry and unit tests

Tomasz Szymanski49dc9a6c26e6
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 210db68ea4..9e4ccbca6e 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -1055,6 +1055,7 @@ class ProductUpdate(ProductCreate):
attributes = cleaned_input.get("attributes")
if attributes:
AttributeAssignmentMixin.save(instance, attributes)
+ info.context.plugins.product_updated(instance)
class ProductDelete(ModelDeleteMutation):
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index e7eee51dc4..8f1fc39604 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -5600,6 +5600,7 @@ enum WebhookEventTypeEnum {
INVOICE_SENT
CUSTOMER_CREATED
PRODUCT_CREATED
+ PRODUCT_UPDATED
CHECKOUT_QUANTITY_CHANGED
CHECKOUT_CREATED
CHECKOUT_UPDATED
@@ -5622,6 +5623,7 @@ enum WebhookSampleEventTypeEnum {
INVOICE_SENT
CUSTOMER_CREATED
PRODUCT_CREATED
+ PRODUCT_UPDATED
CHECKOUT_QUANTITY_CHANGED
CHECKOUT_CREATED
CHECKOUT_UPDATED
diff --git a/saleor/graphql/webhook/tests/test_webhook.py b/saleor/graphql/webhook/tests/test_webhook.py
index 1db42e78a6..5bfa0873b4 100644
--- a/saleor/graphql/webhook/tests/test_webhook.py
+++ b/saleor/graphql/webhook/tests/test_webhook.py
@@ -570,6 +570,7 @@ SAMPLE_PAYLOAD_QUERY = """
(WebhookSampleEventTypeEnum.ORDER_FULFILLED, True),
(WebhookSampleEventTypeEnum.CUSTOMER_CREATED, False),
(WebhookSampleEventTypeEnum.PRODUCT_CREATED, False),
+ (WebhookSampleEventTypeEnum.PRODUCT_UPDATED, False),
(WebhookSampleEventTypeEnum.CHECKOUT_QUANTITY_CHANGED, False),
(WebhookSampleEventTypeEnum.CHECKOUT_CREATED, False),
(WebhookSampleEventTypeEnum.CHECKOUT_UPDATED, False),
@@ -610,6 +611,7 @@ def test_sample_payload_query_by_app(
(WebhookSampleEventTypeEnum.ORDER_FULFILLED, False),
(WebhookSampleEventTypeEnum.CUSTOMER_CREATED, True),
(WebhookSampleEventTypeEnum.PRODUCT_CREATED, True),
+ (WebhookSampleEventTypeEnum.PRODUCT_UPDATED, True),
(WebhookSampleEventTypeEnum.CHECKOUT_QUANTITY_CHANGED, True),
(WebhookSampleEventTypeEnum.CHECKOUT_CREATED, True),
(WebhookSampleEventTypeEnum.CHECKOUT_UPDATED, True),
diff --git a/saleor/plugins/base_plugin.py b/saleor/plugins/base_plugin.py
index 0575f17310..b8dd8d937c 100644
--- a/saleor/plugins/base_plugin.py
+++ b/saleor/plugins/base_plugin.py
@@ -289,6 +289,14 @@ class BasePlugin:
"""
return NotImplemented
+ def product_updated(self, product: "Product", previous_value: Any) -> Any:
+ """Trigger when product is updated.
+
+ Overwrite this method if you need to trigger specific logic after a product is
+ updated.
+ """
+ return NotImplemented
+
def order_fully_paid(self, order: "Order", previous_value: Any) -> Any:
"""Trigger when order is fully paid.
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.py
index bf23525cd8..b1d80450e3 100644
--- a/saleor/plugins/manager.py
+++ b/saleor/plugins/manager.py
@@ -251,6 +251,10 @@ class PluginsManager(PaymentInterface):
default_value = None
return self.__run_method_on_plugins("product_created", default_value, product)
+ def product_updated(self, product: "Product"):
+ default_value = None
+ return self.__run_method_on_plugins("product_updated", default_value, product)
+
def order_created(self, order: "Order"):
default_value = None
return self.__run_method_on_plugins("order_created", default_value, order)
diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.py
index 1e7f7632a2..9c78be7048 100644
--- a/saleor/plugins/webhook/plugin.py
+++ b/saleor/plugins/webhook/plugin.py
@@ -107,6 +107,12 @@ class WebhookPlugin(BasePlugin):
product_data = generate_product_payload(product)
trigger_webhooks_for_event.delay(WebhookEventType.PRODUCT_CREATED, product_data)
+ def product_updated(self, product: "Product", previous_value: Any) -> Any:
+ if not self.active:
+ return previous_value
+ product_data = generate_product_payload(product)
+ trigger_webhooks_for_event.delay(WebhookEventType.PRODUCT_UPDATED, product_data)
+
# Deprecated. This method will be removed in Saleor 3.0
def checkout_quantity_changed(
self, checkout: "Checkout", previous_value: Any
diff --git a/saleor/plugins/webhook/tests/test_webhook.py b/saleor/plugins/webhook/tests/test_webhook.py
index d8902b1dbd..fafbb2b105 100644
--- a/saleor/plugins/webhook/tests/test_webhook.py
+++ b/saleor/plugins/webhook/tests/test_webhook.py
@@ -122,6 +122,18 @@ def test_product_created(mocked_webhook_trigger, settings, product):
)
[email protected]("saleor.plugins.webhook.plugin.trigger_webhooks_for_event.delay")
+def test_product_updated(mocked_webhook_trigger, settings, product):
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+ manager = get_plugins_manager()
+ manager.product_updated(product)
+
+ expected_data = generate_product_payload(product)
+ mocked_webhook_trigger.assert_called_once_with(
+ WebhookEventType.PRODUCT_UPDATED, expected_data
+ )
+
+
@mock.patch("saleor.plugins.webhook.plugin.trigger_webhooks_for_event.delay")
def test_order_updated(mocked_webhook_trigger, settings, order_with_lines):
settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
diff --git a/saleor/webhook/event_types.py b/saleor/webhook/event_types.py
index a0ad1c979a..363afdf8de 100644
--- a/saleor/webhook/event_types.py
+++ b/saleor/webhook/event_types.py
@@ -23,6 +23,7 @@ class WebhookEventType:
CUSTOMER_CREATED = "customer_created"
PRODUCT_CREATED = "product_created"
+ PRODUCT_UPDATED = "product_updated"
# This event is deprecated. It will be removed in Saleor 3.0
CHECKOUT_QUANTITY_CHANGED = "checkout_quantity_changed"
@@ -42,6 +43,7 @@ class WebhookEventType:
INVOICE_SENT: "Invoice sent",
CUSTOMER_CREATED: "Customer created",
PRODUCT_CREATED: "Product created",
+ PRODUCT_UPDATED: "Product updated",
# CHECKOUT_QUANTITY_CHANGED is deprecated. It will be removed in Saleor 3.0
CHECKOUT_QUANTITY_CHANGED: "Checkout quantity changed",
CHECKOUT_CREATED: "Checkout created",
@@ -61,6 +63,7 @@ class WebhookEventType:
(INVOICE_SENT, DISPLAY_LABELS[INVOICE_SENT]),
(CUSTOMER_CREATED, DISPLAY_LABELS[CUSTOMER_CREATED]),
(PRODUCT_CREATED, DISPLAY_LABELS[PRODUCT_CREATED]),
+ (PRODUCT_UPDATED, DISPLAY_LABELS[PRODUCT_UPDATED]),
# CHECKOUT_QUANTITY_CHANGED is deprecated. It will be removed in Saleor 3.0
(CHECKOUT_QUANTITY_CHANGED, DISPLAY_LABELS[CHECKOUT_QUANTITY_CHANGED]),
(CHECKOUT_CREATED, DISPLAY_LABELS[CHECKOUT_CREATED]),
@@ -79,6 +82,7 @@ class WebhookEventType:
INVOICE_SENT: OrderPermissions.MANAGE_ORDERS,
CUSTOMER_CREATED: AccountPermissions.MANAGE_USERS,
PRODUCT_CREATED: ProductPermissions.MANAGE_PRODUCTS,
+ PRODUCT_UPDATED: ProductPermissions.MANAGE_PRODUCTS,
CHECKOUT_QUANTITY_CHANGED: CheckoutPermissions.MANAGE_CHECKOUTS,
CHECKOUT_CREATED: CheckoutPermissions.MANAGE_CHECKOUTS,
CHECKOUT_UPADTED: CheckoutPermissions.MANAGE_CHECKOUTS,