forked from Hevelop/magento2-patches
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Patch-Magento_Store-2.2-language-switcher.patch
122 lines (114 loc) · 4.5 KB
/
Patch-Magento_Store-2.2-language-switcher.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
--- a/Model/Store.php
+++ b/Model/Store.php
@@ -1166,6 +1166,9 @@ public function getCurrentUrl($fromStore = true)
if (!$this->isUseStoreInUrl()) {
$storeParsedQuery['___store'] = $this->getCode();
}
+ if ($this->getCode() !== $this->_storeManager->getStore()->getCode()) {
+ $fromStore = true;
+ }
if ($fromStore !== false) {
$storeParsedQuery['___from_store'] = $fromStore ===
true ? $this->_storeManager->getStore()->getCode() : $fromStore;
@@ -1175,9 +1178,14 @@ public function getCurrentUrl($fromStore = true)
. '://'
. $storeParsedUrl['host']
. (isset($storeParsedUrl['port']) ? ':' . $storeParsedUrl['port'] : '')
- . $storeParsedUrl['path']
- . $requestString
- . ($storeParsedQuery ? '?' . http_build_query($storeParsedQuery, '', '&') : '');
+ . $storeParsedUrl['path'];
+
+ //avoid query params duplication
+ if (!preg_match('/___store=(.*?)&___from_store=(.*?)/', $requestString)) {
+ $currentUrl .= $requestString;
+ }
+
+ $currentUrl .= ($storeParsedQuery ? '?' . http_build_query($storeParsedQuery, '', '&') : '');
return $currentUrl;
}
--- a/Model/Store.php
+++ b/Model/Store.php
@@ -1166,26 +1166,29 @@ public function getCurrentUrl($fromStore = true)
if (!$this->isUseStoreInUrl()) {
$storeParsedQuery['___store'] = $this->getCode();
}
- if ($this->getCode() !== $this->_storeManager->getStore()->getCode()) {
- $fromStore = true;
- }
+
if ($fromStore !== false) {
$storeParsedQuery['___from_store'] = $fromStore ===
true ? $this->_storeManager->getStore()->getCode() : $fromStore;
}
+ $requestStringParts = explode('?', $requestString, 2);
+ $requestStringPath = $requestStringParts[0];
+ if (isset($requestStringParts[1])) {
+ parse_str($requestStringParts[1], $requestString);
+ } else {
+ $requestString = [];
+ }
+
+ $currentUrlQueryParams = array_merge($requestString, $storeParsedQuery);
+
$currentUrl = $storeParsedUrl['scheme']
. '://'
. $storeParsedUrl['host']
. (isset($storeParsedUrl['port']) ? ':' . $storeParsedUrl['port'] : '')
- . $storeParsedUrl['path'];
-
- //avoid query params duplication
- if (!preg_match('/___store=(.*?)&___from_store=(.*?)/', $requestString)) {
- $currentUrl .= $requestString;
- }
-
- $currentUrl .= ($storeParsedQuery ? '?' . http_build_query($storeParsedQuery, '', '&') : '');
+ . $storeParsedUrl['path']
+ . $requestStringPath
+ . ($currentUrlQueryParams ? '?' . http_build_query($currentUrlQueryParams, '', '&') : '');
return $currentUrl;
}
--- a/Block/Switcher.php
+++ b/Block/Switcher.php
@@ -10,7 +10,9 @@
namespace Magento\Store\Block;
use Magento\Directory\Helper\Data;
+use Magento\Store\Api\StoreResolverInterface;
use Magento\Store\Model\Group;
+use Magento\Store\Model\Store;
/**
* @api
@@ -217,15 +219,18 @@ public function getStoreName()
/**
* Returns target store post data
*
- * @param \Magento\Store\Model\Store $store
+ * @param Store $store
* @param array $data
* @return string
*/
- public function getTargetStorePostData(\Magento\Store\Model\Store $store, $data = [])
+ public function getTargetStorePostData(Store $store, $data = [])
{
- $data[\Magento\Store\Api\StoreResolverInterface::PARAM_NAME] = $store->getCode();
+ $data[StoreResolverInterface::PARAM_NAME] = $store->getCode();
+
+ //We need to set fromStore argument as true because
+ //it will enable proper URL rewriting during store switching.
return $this->_postDataHelper->getPostData(
- $store->getCurrentUrl(false),
+ $store->getCurrentUrl(true),
$data
);
}
--- a/Test/Unit/Block/SwitcherTest.php
+++ b/Test/Unit/Block/SwitcherTest.php
@@ -53,7 +53,7 @@ public function testGetTargetStorePostData()
$storeSwitchUrl = 'http://domain.com/stores/store/switch';
$store->expects($this->atLeastOnce())
->method('getCurrentUrl')
- ->with(false)
+ ->with(true)
->willReturn($storeSwitchUrl);
$this->corePostDataHelper->expects($this->any())
->method('getPostData')