Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In magento 2.2 Changing language twice gives error [bug] #11379

Closed
andy17612 opened this issue Oct 11, 2017 · 19 comments
Closed

In magento 2.2 Changing language twice gives error [bug] #11379

andy17612 opened this issue Oct 11, 2017 · 19 comments
Labels
duplicate Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed

Comments

@andy17612
Copy link

andy17612 commented Oct 11, 2017

If you change the language twice in a store this system won't redirect you back to the right store. Instead it gives a server error with the message: Requested store is not found

Preconditions

  1. In magento 2.2
  2. PHP 7.1

Steps to reproduce

  1. If you change the language twice in a store this system won't redirect you back to the right store. Instead it gives a server error with the message: Requested store is not found
  2. Setup 1 store with two store views
  3. Go to the website
  4. Change the language
  5. Change the language again.
  6. The error will display now

Expected result

  1. Just change back to the previous store view

Actual result

  1. Server error: Requested store is not found
  2. Gif showing the problem : https://cl.ly/3c2h2C0E3v1Q
@andy17612 andy17612 changed the title Changing language twice gives error: Requested store is not found (BUG)Changing language twice gives error: Requested store is not found Oct 11, 2017
@magento-engcom-team magento-engcom-team added Issue: Format is not valid Gate 1 Failed. Automatic verification of issue format is failed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed and removed Issue: Format is not valid Gate 1 Failed. Automatic verification of issue format is failed labels Oct 11, 2017
@andy17612 andy17612 changed the title (BUG)Changing language twice gives error: Requested store is not found In magento 2.2 Changing language twice gives error Oct 12, 2017
@andy17612 andy17612 changed the title In magento 2.2 Changing language twice gives error In magento 2.2 Changing language twice gives error [bug] Oct 12, 2017
@magento-engcom-team
Copy link
Contributor

@rigu12 thank you for your report.
We were not able to reproduce this issue by following the steps you provided. Please provide more detailed steps to reproduce or try to reproduce this issue on a clean installation.

@andy17612
Copy link
Author

andy17612 commented Oct 12, 2017 via email

@selusi
Copy link

selusi commented Oct 13, 2017

The issue is in the url building.
Precisly in the file vendor/magento/module-store/model/Store.php the function getCurrentUrl($fromStore = true).
In fact, so as it showed in your example file, if you click the language switch more times the result query string is: ?___store=en?___store=de that obviously get server error because the
store code "en?___store=de" doesn't exist.

To fix this problem, I added the following method to the Store class (Download the Complete Code):

public function cleanUrlQueryString($url){
	if(preg_match("/\?/", $url)){
		$arr = explode('?', $url);
		$queryArray = false;
		if(isset($arr[1])){
			$strArr = parse_str(html_entity_decode($arr[1]), $queryArray);
		}
		$cleanedUrl = $arr[0];
		$cleanedUrl .= ($queryArray) ? '?'.http_build_query($queryArray, '', '&') : '';
		return $cleanedUrl;
	} 
	return $url;		
}

Then I modified the function getCurrentUrl substituting the original lines from 1174 to 1182:

$currentUrl = $storeParsedUrl['scheme']
     . '://'
     . $storeParsedUrl['host']
      . (isset($storeParsedUrl['port']) ? ':' . $storeParsedUrl['port'] : '')
      . $storeParsedUrl['path']
     . $requestString
     . ($storeParsedQuery ? '?' . http_build_query($storeParsedQuery, '', '&') : '');
return $currentUrl;

with this

$currentUrl = $storeParsedUrl['scheme']
            . '://'
            . $storeParsedUrl['host']
            . (isset($storeParsedUrl['port']) ? ':' . $storeParsedUrl['port'] : '')
            . $storeParsedUrl['path']
            . $requestString;
					
if( preg_match("/\?/",$currentUrl) ){
     $currentUrl .= ($storeParsedQuery ? '&'.http_build_query($storeParsedQuery, '', '&') : '');
} else {
     $currentUrl .= ($storeParsedQuery ? '?' . http_build_query($storeParsedQuery, '', '&') : '');
}
return $this->cleanUrlQueryString($currentUrl);

By this way the function return a cleaned query string withouth error.

IMPORTANT: howewer this solution does not solve the bug interly, because if you change the language on page of the product details or on a category page the app give a not found page, because does not translate the urlrewrite. In the previous version Magento 2.1.9 the switcher worked fine.

@andy17612
Copy link
Author

andy17612 commented Oct 13, 2017 via email

@andy17612
Copy link
Author

andy17612 commented Oct 13, 2017 via email

@selusi
Copy link

selusi commented Oct 13, 2017

@rigu12 Right! The path of source code is vendor/magento/module-store/model/Store.php
However, the bug is much more complex. My solution is just a palliative.

I understand they want to improve the structure, but for now serious bugs are so many that it's difficult to use Magento version 2.2.0 for a site in production.

@selusi
Copy link

selusi commented Oct 13, 2017

@rigu12 To install sample data you can use the command line:
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento indexer:reindex
php bin/magento cache:clean

@hostep
Copy link
Contributor

hostep commented Oct 13, 2017

This is a duplicate of #10908 btw 😉

@andy17612
Copy link
Author

andy17612 commented Oct 13, 2017 via email

@selusi
Copy link

selusi commented Oct 14, 2017

@rigu12
If you installed extensions by composer you can run the command:

  1. php bin/magento module:uninstall -r Vendor_Module (NOTE: -r flag removes module data.)
  2. php bin/magento setup:upgrade

Alternativly you can uninstall modules manually:

  1. Remove the module Vendor_% from app/etc/config.php
  2. Drop module tables or columns from database, please check app/code/Vendor/Module/Setup folder for more information
  3. Remove the folder app/code/vendor/%
  4. Remove module configuration settings from core_config_data table by running the following query
    DELETE FROM core_config_data WHERE path LIKE 'vendor%';
  5. Remove module from setup_module table by running the following query
    DELETE FROM setup_module WHERE module LIKE 'vendor_%';
  6. Run the following command by logging onto your SSH server
    php bin/magento setup:upgrade

Before It is better if you make an application and database backup.

@selusi
Copy link

selusi commented Oct 14, 2017

The discussion continues at the following link:
Language switcher is broken when using multiple times #10908

@andy17612
Copy link
Author

andy17612 commented Oct 15, 2017 via email

@andy17612
Copy link
Author

andy17612 commented Oct 15, 2017 via email

@selusi
Copy link

selusi commented Oct 16, 2017

@rigu12
To update the index You have to run the command:
php bin/magento indexer:reindex
In this regard, you also need to configure a cron job to update indexes at predefined deadlines.
Visit the below page for details:
http://devdocs.magento.com/guides/v2.0/config-guide/cli/config-cli-subcommands-cron.html

@vutrankien
Copy link

Yes, i'm confirm this error, it happen by replace language on end of link

@andy17612
Copy link
Author

@selusi
Hello ,can u ask you how to hide/remove the code url "?___store="
xxxx.com/?___store=XX >> xxxx.com/
or
#11963 <<
thank you

@selusi
Copy link

selusi commented Nov 29, 2017

@rigu12
Under the code to add in vendor /magento/module-store/model/Store.php to clean the URL.

    public function getCurrentUrl($fromStore = true)
    {
        $sidQueryParam = $this->_sidResolver->getSessionIdQueryParam($this->_getSession());
        $requestString = $this->_url->escape(ltrim($this->_request->getRequestString(), '/'));

        $storeUrl = $this->getUrl('', ['_secure' => $this->_storeManager->getStore()->isCurrentlySecure()]);

        if (!filter_var($storeUrl, FILTER_VALIDATE_URL)) {
            return $storeUrl;
        }

        $storeParsedUrl = parse_url($storeUrl);

        $storeParsedQuery = [];
        if (isset($storeParsedUrl['query'])) {
            parse_str($storeParsedUrl['query'], $storeParsedQuery);
        }

        $currQuery = $this->_request->getQueryValue();
        if (isset($currQuery[$sidQueryParam])
            && !empty($currQuery[$sidQueryParam])
            && $this->_getSession()->getSessionIdForHost($storeUrl) != $currQuery[$sidQueryParam]
        ) {
            unset($currQuery[$sidQueryParam]);
        }

        foreach ($currQuery as $key => $value) {
            $storeParsedQuery[$key] = $value;
        }

        if (!$this->isUseStoreInUrl()) {
            $storeParsedQuery['___store'] = $this->getCode();
        }
        if ($fromStore !== false) {
            $storeParsedQuery['___from_store'] = $fromStore ===
                true ? $this->_storeManager->getStore()->getCode() : $fromStore;
        }

        $currentUrl = $storeParsedUrl['scheme']
            . '://'
            . $storeParsedUrl['host']
            . (isset($storeParsedUrl['port']) ? ':' . $storeParsedUrl['port'] : '')
            . $storeParsedUrl['path']
            . $requestString;
					
		if( preg_match("/\?/",$currentUrl) ){
			$currentUrl .= ($storeParsedQuery ? '&amp;'.http_build_query($storeParsedQuery, '', '&amp;') : '');
		} else {
			$currentUrl .= ($storeParsedQuery ? '?' . http_build_query($storeParsedQuery, '', '&amp;') : '');
		}
		
        return $this->cleanUrlQueryString($currentUrl);
    } 
    /**
     * To clean URL from wrong query string
     *
     * @return Cleaned url
     */
    public function cleanUrlQueryString($url)
    {
		if(preg_match("/\?/", $url)){
			$arr = explode('?', $url);
			$queryArray = false;
			if(isset($arr[1])){
				$strArr = parse_str(html_entity_decode($arr[1]), $queryArray);
			}
			$cleanedUrl = $arr[0];
			$cleanedUrl .= ($queryArray) ? '?'.http_build_query($queryArray, '', '&amp;') : '';
			return $cleanedUrl;
		} 
		return $url;		
    }

@magento-engcom-team magento-engcom-team added the Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed label Dec 1, 2017
@magento-engcom-team
Copy link
Contributor

@rigu12 , thank you for your report.
This is duplicate of #10908

@andy17612
Copy link
Author

@selusi Before you give me the code not working on 2.2.3 for hide/remove the code url "?___store="

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed
Projects
None yet
Development

No branches or pull requests

5 participants