Skip to content

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

Fix gift promotion calculation (#18592) (#18599)

Iga Karbowiakeb6b122b535a
diff --git a/saleor/discount/tests/test_utils/test_create_or_update_discount_objects_from_promotion_for_checkout.py b/saleor/discount/tests/test_utils/test_create_or_update_discount_objects_from_promotion_for_checkout.py
index ddb4ee920e..6a79bf292c 100644
--- a/saleor/discount/tests/test_utils/test_create_or_update_discount_objects_from_promotion_for_checkout.py
+++ b/saleor/discount/tests/test_utils/test_create_or_update_discount_objects_from_promotion_for_checkout.py
@@ -18,7 +18,7 @@ from ....product.models import (
VariantChannelListingPromotionRule,
)
from ....tests import race_condition
-from ... import DiscountType, RewardType, RewardValueType
+from ... import DiscountType, DiscountValueType, RewardType, RewardValueType
from ...models import CheckoutDiscount, CheckoutLineDiscount, PromotionRule
from ...utils.checkout import (
create_checkout_discount_objects_for_order_promotions,
@@ -2125,6 +2125,47 @@ def test_create_discount_objects_for_order_promotions_race_condition(
assert discounts[0].amount_value == reward_value
+def test_create_discount_objects_for_order_promotions_missing_rule_on_discount_object(
+ checkout_info,
+ checkout_lines_info,
+ gift_promotion_rule,
+):
+ # given
+ rule = gift_promotion_rule
+ checkout = checkout_info.checkout
+ channel = checkout_info.channel
+
+ gift_promotion_rule.channels.add(channel)
+ variant = gift_promotion_rule.gifts.first()
+ gift_promotion_rule.gifts.exclude(pk=variant.pk).delete()
+ gift_line = checkout.lines.create(
+ checkout=checkout,
+ variant=variant,
+ quantity=1,
+ is_gift=True,
+ undiscounted_unit_price_amount=10,
+ )
+ # create a discount object without promotion rule set
+ line_discount = CheckoutLineDiscount.objects.create(
+ promotion_rule=None,
+ line=gift_line,
+ type=DiscountType.ORDER_PROMOTION,
+ value_type=DiscountValueType.FIXED,
+ value=Decimal(5),
+ amount_value=Decimal(5),
+ currency=channel.currency_code,
+ )
+
+ # when
+ create_checkout_discount_objects_for_order_promotions(
+ checkout_info, checkout_lines_info
+ )
+
+ # then
+ line_discount.refresh_from_db()
+ assert line_discount.promotion_rule_id == rule.id
+
+
def test_create_or_update_order_discount_race_condition(
checkout_info,
checkout_lines_info,
diff --git a/saleor/discount/utils/promotion.py b/saleor/discount/utils/promotion.py
index 2d5c9f222c..dcc43d16fb 100644
--- a/saleor/discount/utils/promotion.py
+++ b/saleor/discount/utils/promotion.py
@@ -828,7 +828,7 @@ def _handle_gift_reward(
if line_discount.line_id != line.id:
line_discount.line = line
fields_to_update.append("line_id")
- promotion_rule = cast(PromotionRule, line_discount.promotion_rule)
+ promotion_rule = cast(PromotionRule, line_discount_data.promotion_rule)
update_promotion_discount(
promotion_rule,
rule_info,
diff --git a/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py b/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py
index e3e634702f..c3e917a52c 100644
--- a/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py
+++ b/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py
@@ -1320,7 +1320,7 @@ def test_add_checkout_lines_gift_discount_applies(
# when
user_api_client.ensure_access_token()
- with django_assert_num_queries(133):
+ with django_assert_num_queries(132):
response = user_api_client.post_graphql(MUTATION_CHECKOUT_LINES_ADD, variables)
# then