Skip to content

Fix bogus store-not-found error on store switch when ___from_store is absent#40965

Open
TuVanDev wants to merge 1 commit into
magento:2.4-developfrom
TuVanDev:fix-store-switch-missing-from-store
Open

Fix bogus store-not-found error on store switch when ___from_store is absent#40965
TuVanDev wants to merge 1 commit into
magento:2.4-developfrom
TuVanDev:fix-store-switch-missing-from-store

Conversation

@TuVanDev

@TuVanDev TuVanDev commented Jul 9, 2026

Copy link
Copy Markdown
Member

Description (*)

Magento\Store\Controller\Store\SwitchAction::execute() resolves the origin store from the ___from_store request param, falling back to the store cookie:

$fromStoreCode = $this->_request->getParam(
    '___from_store',
    $this->storeCookieManager->getStoreCodeFromCookie()
);
...
$fromStore = $this->storeRepository->get($fromStoreCode);

When neither ___from_store nor a store cookie is present, $fromStoreCode is null, so StoreRepository::get(null) throws NoSuchEntityException. The controller catches it and shows the misleading error:

The store that was requested wasn't found. Verify the store and try again.

…even though the target store (___store) is perfectly valid. The message appears as a flash message and disappears on the next request, which makes it look intermittent and leaves no log entry.

A concrete real-world trigger is the admin CMS → Pages → Select → View ("View Page") preview of a page assigned to the default store view. Core's Magento\Cms\ViewModel\Page\Grid\UrlBuilder::prepareRequestQuery() deliberately omits ___from_store when the target equals the default store view, so a staff member previewing such a page (with no store cookie yet) always hits this error.

This PR falls back to the current store as the origin store when none is provided. A store-to-itself switch then simply sets the cookie and redirects with no error, and a genuine cross-store switch with a missing ___from_store gets the correct origin. Genuinely invalid target stores still raise the error as before.

Related Pull Requests

None.

Fixed Issues (if relevant)

N/A — reproducible on 2.4-develop (see manual testing below).

Manual testing scenarios (*)

  1. Ensure the browser has no store cookie (fresh/incognito session).
  2. Visit https://<base-url>/stores/store/switch/?___store=default (a valid store code, no ___from_store).
    • Before: you are redirected back, and the page shows the error "The store that was requested wasn't found. Verify the store and try again."
    • After: you are redirected back with no error message.
  3. Alternatively, in Admin go to Content → Pages, and for a page assigned to the default store view click Select → View.
    • Before: the storefront page shows the error banner.
    • After: no error banner.
  4. Regression — a real cross-store switch (?___from_store=default&___store=<other>) and a genuinely invalid target (?___store=does_not_exist) behave exactly as before (valid switch works; invalid target still errors).

Integration coverage added in SwitchActionTest::testSwitchWithoutFromStoreDoesNotError().

Questions or comments

The current store is used as the origin store because ___from_store semantically means "the store the visitor is switching from", i.e. the current request context. $this->storeManager is already used a few lines below in the same method.

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 (added integration test)
  • All automated tests passed successfully (all affected tests pass)

… absent

When ___from_store is not supplied and no store cookie is set, $fromStoreCode
is null and StoreRepository::get(null) throws NoSuchEntityException, so the
store switch surfaces the misleading 'The store that was requested wasn't found.
Verify the store and try again.' error even though the target store is valid.

This happens, for example, with the admin CMS 'View Page' preview of a page
assigned to the default store view: the generated switch URL omits
___from_store, and a first-time visitor has no store cookie yet.

Fall back to the current store as the origin store when none is provided, so a
store-to-itself switch simply sets the cookie and redirects with no error.
Genuinely invalid target stores still raise the error.
@m2-assistant

m2-assistant Bot commented Jul 9, 2026

Copy link
Copy Markdown

Hi @TuVanDev. 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.

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.

1 participant