magento2: Store Switcher not working when customer is logged in - Magento 2.3.3

Preconditions (*)

Magento 2.3.3

Steps to reproduce (*)

  1. Create at least 2 store views in Magento.
  2. Create a customer account
  3. Go to a page on your shop and change the store with the store switcher while being logged in with the customer account.

Expected result (*)

  1. You a redirected to the same page of the other storeview

Actual result (*)

  1. The user is redirected to a 404 page because only the store code is changed in the URL.

Solution

I did some digging and found out that the bug is coming from the way the StoreSwitcher is currently implemented. For the moment the code is the following:

https://github.com/magento/magento2/blob/7aa94564d85e408baea01abc5315a0441401c375/app/code/Magento/Store/Model/StoreSwitcher.php#L48-L57

The elements and their position in the $this->storeSwitchers array is the following:

  • cleanTargetUrl
  • manageStoreCookie
  • managePrivateContent
  • hashGenerator
  • rewriteUrl

The problem is that the rewriteUrl Switcher has to receive the target url with the changed store code in order to find the equivalent url for the page on the other store. The problem is that when a customer is connected, the hashGenerator generates a url for the switchRequest and this url is then passed onto the rewriteUrl switcher. Because of that, the rewriteUrl switcher cannot find the equivalent Url and the user is redirected to a non existing url.

Their are 2 solutions for this problem:

  1. Change the position of the elements in the $this->storeSwitchers array in order to assure that the hashGenerator always is the last switcher to be applied.
  2. Rewrite the switch method of the rewriteUrl switcher in order to be able to change the encoded url in the switchRequest.

I think the first one would be the easiest to apply but I am wondering if this could add other bugs to the storeSwitcher.

Let me know which solution would be more appropriate and I will open a PR for this issue.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 38 (18 by maintainers)

Most upvoted comments

Alrighty, for the people searching after the fix, I took a couple of hours today to go searching for it and after a bunch of bisecting attempts I finally found the fix over here: https://github.com/magento/magento2/commit/f19c780fb7e31db89bfd9965e177d52ad03dacd7

Adding the sortOrder="1000" to that hashGenerator item fixes the problem in Magento 2.3.3!

@shikhamis11 this bug was introduced in 2.3.3 and will appear in 2.3-dev after merging 2.3.3 with 2.3-dev.

hello @jojotjebaby I checked on 2.3.3 there this issue was able to reproduced … but it seems it is already fixed in magento 2.3-develop instance

I opened another issue but it seems that 2.3.3 hasn’t been merged into 2.3-dev yet. So we have to wait for this merge before I can provide a PR …