Skip to content

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

Move `product_created` execution after M2M saving

dzkb651df7c8893b
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index e1b03334ba..5940254bf1 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -881,7 +881,6 @@ class ProductCreate(ModelMutation):
@transaction.atomic
def save(cls, info, instance, cleaned_input):
instance.save()
- info.context.extensions.product_created(instance)
if not instance.product_type.has_variants:
site_settings = info.context.site.settings
track_inventory = cleaned_input.get(
@@ -902,6 +901,12 @@ class ProductCreate(ModelMutation):
if collections is not None:
instance.collections.set(collections)
+ @classmethod
+ def perform_mutation(cls, _root, info, **data):
+ response = super().perform_mutation(_root, info, **data)
+ info.context.extensions.product_created(response.product)
+ return response
+
class ProductUpdate(ProductCreate):
class Arguments:
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index 63edbbf357..f09d760150 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -1082,6 +1082,68 @@ def test_product_create_without_category_and_true_is_published_value(
assert errors[0]["message"] == "You must select a category to be able to publish"
+def test_product_create_with_collections_webhook(
+ staff_api_client,
+ permission_manage_products,
+ collection,
+ product_type,
+ category,
+ monkeypatch,
+):
+ query = """
+ mutation createProduct($productTypeId: ID!, $collectionId: ID!, $categoryId: ID!) {
+ productCreate(input: {
+ name: "Product",
+ basePrice: "2.5",
+ productType: $productTypeId,
+ isPublished: true,
+ collections: [$collectionId],
+ category: $categoryId
+ }) {
+ product {
+ id,
+ collections {
+ slug
+ },
+ category {
+ slug
+ }
+ }
+ errors {
+ message
+ field
+ }
+ }
+ }
+
+ """
+
+ def assert_product_has_collections(product):
+ assert product.collections.count() > 0
+ assert product.collections.first() == collection
+
+ monkeypatch.setattr(
+ "saleor.extensions.manager.ExtensionsManager.product_created",
+ lambda cls, product: assert_product_has_collections(product),
+ )
+
+ product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
+ category_id = graphene.Node.to_global_id("Category", category.pk)
+ collection_id = graphene.Node.to_global_id("Collection", collection.pk)
+
+ response = staff_api_client.post_graphql(
+ query,
+ {
+ "productTypeId": product_type_id,
+ "categoryId": category_id,
+ "collectionId": collection_id,
+ },
+ permissions=[permission_manage_products],
+ )
+
+ get_graphql_content(response)
+
+
def test_update_product(
staff_api_client,
category,