Skip to content

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

Fix promotionCreate mutation to allow creating rules with no channels specified. (#15220)

Piotr Zabieglikda485ca186b1
diff --git a/saleor/graphql/discount/mutations/promotion/promotion_create.py b/saleor/graphql/discount/mutations/promotion/promotion_create.py
index 60b25d02b2..f5db32c2f0 100644
--- a/saleor/graphql/discount/mutations/promotion/promotion_create.py
+++ b/saleor/graphql/discount/mutations/promotion/promotion_create.py
@@ -182,7 +182,8 @@ class PromotionCreate(ModelMutation):
models.PromotionRule.objects.bulk_create(rules)
for rule, channels in rules_with_channels_to_add:
- rule.channels.set(channels)
+ if channels:
+ rule.channels.set(channels)
return rules
diff --git a/saleor/graphql/discount/tests/mutations/test_promotion_create.py b/saleor/graphql/discount/tests/mutations/test_promotion_create.py
index c647abddc7..538f3b4479 100644
--- a/saleor/graphql/discount/tests/mutations/test_promotion_create.py
+++ b/saleor/graphql/discount/tests/mutations/test_promotion_create.py
@@ -1184,6 +1184,52 @@ def test_promotion_create_invalid_catalogue_predicate(
assert errors[0]["index"] == 1
+@patch("saleor.product.tasks.update_products_discounted_prices_of_promotion_task.delay")
+@patch("saleor.plugins.manager.PluginsManager.promotion_started")
+@patch("saleor.plugins.manager.PluginsManager.promotion_created")
+def test_promotion_create_rules_without_channels_and_percentage_reward(
+ promotion_created_mock,
+ promotion_started_mock,
+ update_products_discounted_prices_of_promotion_task_mock,
+ staff_api_client,
+ permission_group_manage_discounts,
+ variant,
+):
+ # given
+ permission_group_manage_discounts.user_set.add(staff_api_client.user)
+ start_date = timezone.now() - timedelta(days=30)
+ end_date = timezone.now() + timedelta(days=30)
+ catalogue_predicate = {
+ "variantPredicate": {
+ "ids": [graphene.Node.to_global_id("ProductVariant", variant.id)]
+ }
+ }
+ variables = {
+ "input": {
+ "name": "test promotion",
+ "startDate": start_date.isoformat(),
+ "endDate": end_date.isoformat(),
+ "rules": [
+ {
+ "name": "test promotion rule 1",
+ "rewardValueType": RewardValueTypeEnum.PERCENTAGE.name,
+ "rewardValue": Decimal("10"),
+ "cataloguePredicate": catalogue_predicate,
+ }
+ ],
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(PROMOTION_CREATE_MUTATION, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["promotionCreate"]
+ assert not data["errors"]
+ assert data["promotion"]["rules"]
+
+
PROMOTION_CREATE_WITH_EVENTS = """
mutation promotionCreate($input: PromotionCreateInput!) {
promotionCreate(input: $input) {