Skip to content

Remove adjustments from cart orders when promotion is discarded - #6557

Merged
mamhoff merged 2 commits into
solidusio:mainfrom
mamhoff:fix-promotion-discard-adjustments
Aug 28, 2026
Merged

Remove adjustments from cart orders when promotion is discarded#6557
mamhoff merged 2 commits into
solidusio:mainfrom
mamhoff:fix-promotion-discard-adjustments

Conversation

@mamhoff

@mamhoff mamhoff commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Summary

Fix orphaned adjustments from discarded promotions.

Problem

When a promotion is soft-deleted (discarded), it cleans up order_promotions associations for incomplete orders but leaves behind the adjustments that were created by the promotion's benefits. These orphaned adjustments cause crashes during order recalculation because the code tries to access adjustment.source.promotion.lane, but the promotion association returns nil for discarded promotions.

Error example:

undefined method 'lane' for nil
  adjustment.source.promotion.lane.in?(lanes)
                             ^^^^^

Solution

  1. Association fix: Add with_discarded scope to the benefit → promotion association so discarded promotions can be accessed (matches pattern in OrderPromotion and PromotionCode)
  2. Proactive cleanup: When a promotion is discarded, call remove_adjustments_from_incomplete_orders on each benefit to immediately clean up orphaned adjustments

Changes

  • Added with_discarded to Benefit#promotion association
  • Modified Promotion#delete_cart_connections to remove adjustments from incomplete orders when promotion is discarded
  • Added test coverage for the discard behavior

Checklist

  • Added test coverage
  • Ran specs locally and they pass
  • Followed Solidus coding standards

@mamhoff
mamhoff requested a review from a team as a code owner August 27, 2026 09:34
@mamhoff mamhoff added backport-v4.5 Backport this pull-request to v4.5 backport-v4.6 Backport this pull-request to v4.6 backport-v4.7 Backport this pull-request to v4.7 labels Aug 27, 2026
@github-actions github-actions Bot added the changelog:solidus_promotions Changes to the solidus_promotions gem label Aug 27, 2026
@codecov

codecov Bot commented Aug 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.16%. Comparing base (106ad9d) to head (8c3c033).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6557   +/-   ##
=======================================
  Coverage   92.16%   92.16%           
=======================================
  Files        1037     1037           
  Lines       21210    21211    +1     
=======================================
+ Hits        19549    19550    +1     
  Misses       1661     1661           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@tvdeyen tvdeyen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

@mamhoff
mamhoff force-pushed the fix-promotion-discard-adjustments branch 2 times, most recently from 350d97f to 06536ea Compare August 27, 2026 11:04
Benefits need access to their promotion even when it's discarded to
properly handle cleanup and checks. This matches the pattern used in
OrderPromotion and PromotionCode associations.
@mamhoff
mamhoff force-pushed the fix-promotion-discard-adjustments branch from 06536ea to 8c3c033 Compare August 27, 2026 11:53
@mamhoff
mamhoff merged commit f79ddaa into solidusio:main Aug 28, 2026
75 of 76 checks passed
@solidus-bot

solidus-bot Bot commented Aug 28, 2026

Copy link
Copy Markdown

💔 Some backports could not be created

Status Branch Result
v4.5 Backport failed because of merge conflicts
v4.6 Backport failed because of merge conflicts
v4.7

Manual backport

To create the backport manually run:

backport --pr 6557

Questions ?

Please refer to the Backport tool documentation and see the Github Action logs for details

@mamhoff

mamhoff commented Aug 28, 2026

Copy link
Copy Markdown
Contributor Author

💚 All backports created successfully

Status Branch Result
v4.6

Questions ?

Please refer to the Backport tool documentation

@mamhoff

mamhoff commented Aug 28, 2026

Copy link
Copy Markdown
Contributor Author

💚 All backports created successfully

Status Branch Result
v4.5

Questions ?

Please refer to the Backport tool documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-v4.5 Backport this pull-request to v4.5 backport-v4.6 Backport this pull-request to v4.6 backport-v4.7 Backport this pull-request to v4.7 changelog:solidus_promotions Changes to the solidus_promotions gem

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants