Skip to content

Conversation

@satishashilwar
Copy link

@satishashilwar satishashilwar commented Dec 7, 2025

Description (*)

Fixed an issue where calling setCurrentCurrencyCode() multiple times did not update the currency returned by getCurrentCurrency() due to internal caching.

Root Cause:
getCurrentCurrency() caches the currency object using the 'current_currency' key. When setCurrentCurrencyCode() changed the currency code, it didn't clear this cache, causing getCurrentCurrency() to return stale data.

Solution:
Added unsetData('current_currency') in setCurrentCurrencyCode() to clear the cached currency when the currency code changes, forcing getCurrentCurrency() to reload the currency based on the new code.

Changes:

  • Updated Store::setCurrentCurrencyCode() to clear cached currency
  • Added unit test testSetCurrentCurrencyCodeClearsCachedCurrency()

Related Pull Requests

None

Fixed Issues (if relevant)

  1. Fixes [Issue] Force current currency reload on current currency code change #40333

Manual testing scenarios (*)

  1. Create a store with multiple available currencies (USD, EUR)
  2. Call $store->setCurrentCurrencyCode('USD')
  3. Verify $store->getCurrentCurrency()->getCode() returns 'USD'
  4. Call $store->setCurrentCurrencyCode('EUR')
  5. Verify $store->getCurrentCurrency()->getCode() returns 'EUR' (without this fix, it would still return 'USD')

Questions or comments

None

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • README.md files for modified modules are updated and included in the pull request if any predefined sections require an update
  • All automated tests passed successfully (all builds are green)

@m2-assistant
Copy link

m2-assistant bot commented Dec 7, 2025

Hi @satishashilwar. Thank you for your contribution!
Here are some useful tips on how you can test your changes using Magento test environment.
❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

Fixed an issue where calling setCurrentCurrencyCode() multiple times
did not update the currency returned by getCurrentCurrency() due to
internal caching.

The issue occurred because getCurrentCurrency() caches the currency
object in the store's data using the 'current_currency' key. When
setCurrentCurrencyCode() was called to change the currency code, the
cached currency object was not cleared, causing getCurrentCurrency()
to return the old currency instead of loading the new one.

Solution:
- Added unsetData('current_currency') in setCurrentCurrencyCode()
  to clear the cached currency when the currency code changes
- This forces getCurrentCurrency() to reload the currency based on
  the new currency code on its next call

Changes:
- Updated Store::setCurrentCurrencyCode() to clear cached currency
- Added unit test to verify currency cache is cleared on code change

Test coverage:
- Added testSetCurrentCurrencyCodeClearsCachedCurrency() unit test
- Test verifies unsetData() is called when currency code changes
- Test verifies behavior with multiple currency code changes

Manual testing:
1. Create store with multiple available currencies (USD, EUR)
2. Call $store->setCurrentCurrencyCode('USD')
3. Verify $store->getCurrentCurrency()->getCode() returns 'USD'
4. Call $store->setCurrentCurrencyCode('EUR')
5. Verify $store->getCurrentCurrency()->getCode() returns 'EUR'
6. Without this fix, step 5 would return 'USD'

Fixes: magento#40333
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.

[Issue] Force current currency reload on current currency code change

1 participant