Fix bogus store-not-found error on store switch when ___from_store is absent#40965
Open
TuVanDev wants to merge 1 commit into
Open
Fix bogus store-not-found error on store switch when ___from_store is absent#40965TuVanDev wants to merge 1 commit into
TuVanDev wants to merge 1 commit into
Conversation
… 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.
|
Hi @TuVanDev. Thank you for your contribution!
Allowed build names are:
You can find more information about the builds here For more details, review the Code Contributions documentation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description (*)
Magento\Store\Controller\Store\SwitchAction::execute()resolves the origin store from the___from_storerequest param, falling back to the store cookie:When neither
___from_storenor a store cookie is present,$fromStoreCodeisnull, soStoreRepository::get(null)throwsNoSuchEntityException. The controller catches it and shows the misleading error:…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_storewhen 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_storegets 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 (*)
storecookie (fresh/incognito session).https://<base-url>/stores/store/switch/?___store=default(a valid store code, no___from_store).?___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_storesemantically means "the store the visitor is switching from", i.e. the current request context.$this->storeManageris already used a few lines below in the same method.Contribution checklist (*)