Skip to content

fix(checkout): apply new payment method fee immediately on selection - #138

Open
obinnaelviso wants to merge 1 commit into
tastyigniter:4.xfrom
obinnaelviso:fix/checkout-payment-fee-not-applying
Open

fix(checkout): apply new payment method fee immediately on selection#138
obinnaelviso wants to merge 1 commit into
tastyigniter:4.xfrom
obinnaelviso:fix/checkout-payment-fee-not-applying

Conversation

@obinnaelviso

Copy link
Copy Markdown
Contributor

Problem

Switching payment methods during checkout didn't apply the new method's fee until the page was fully refreshed, and a previously-applied fee could linger after switching to a method with no fee.

Root causes:

  • PaymentFee::beforeApply() cached the resolved Payment model on first use and never refreshed it when the selected code changed within the same request.
  • CartCondition::apply() left passed/calculatedValue stale when beforeApply() opted out, so a previously-applied condition kept reporting itself as applied with its old value.
  • OrderManager::applyRequiredAttributes() computed order_total before the fee condition was applied, so the stored total could be out of sync with the selected payment method.

Fix

  • Refetch the Payment model whenever the metadata code changes, not just when it's null.
  • Reset passed/calculatedValue when a condition opts out via beforeApply(), so stale state doesn't persist.
  • Compute order_total after the fee condition is applied.

Existing test suite passes (vendor/bin/pest), including PaymentFeeTest, CartConditionTest, CartTest, and OrderManagerTest.

The payment fee cart condition cached the resolved Payment model on
first use and never refreshed it, so switching payment methods mid
checkout kept using the previously selected method's fee until a full
page reload. It also left a condition's passed/calculatedValue state
stale once beforeApply() opted out, so a previously applied fee stuck
around after switching to a method with no fee. Order totals were
persisted before the fee was applied, so the stored order_total could
be out of sync with the selected payment method.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes checkout recalculation so switching payment methods immediately applies (or removes) the payment fee without requiring a full refresh, and ensures the persisted order_total matches the cart total after conditions are applied.

Changes:

  • Refresh PaymentFee’s resolved Payment model when the selected payment code changes within the same request.
  • Reset CartCondition internal state (passed / calculatedValue) when beforeApply() opts out to prevent stale “applied” state.
  • Recompute OrderManager’s order_total after applying the current payment fee condition.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/Classes/OrderManager.php Applies payment fee before assigning order_total so the stored total stays in sync with the selected payment method.
src/CartConditions/PaymentFee.php Refetches the Payment model when metadata code changes to avoid stale fee calculations.
src/CartCondition.php Clears stale condition state when beforeApply() returns false, preventing lingering applied/value data.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants