Skip to content

Commit 1919569

Browse files
author
Derrick Heesbeen
authored
Merge pull request #2 from thokiller/master
[BUGFIX] fixed storeswitcher + non default navigation
2 parents 5ab169f + bfb8f39 commit 1919569

6 files changed

Lines changed: 285 additions & 15 deletions

File tree

Helper/Data.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/**
3+
* A Magento 2 module named Experius MultipleWebsiteStoreCodeUrl
4+
* Copyright (C) 2017 Experius
5+
*
6+
* This file is part of Experius MultipleWebsiteStoreCodeUrl.
7+
*
8+
* Experius MultipleWebsiteStoreCodeUrl is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
22+
namespace Experius\MultipleWebsiteStoreCodeUrl\Helper;
23+
24+
use Magento\Framework\App\Helper\AbstractHelper;
25+
use Magento\Store\Model\StoreManagerInterface;
26+
27+
/**
28+
* Class Data
29+
* @package Experius\MultipleWebsiteStoreCodeUrl\Helper
30+
*/
31+
class Data extends AbstractHelper
32+
{
33+
/**
34+
* @var StoreManagerInterface
35+
*/
36+
protected $storeManager;
37+
38+
/**
39+
* Data constructor.
40+
* @param StoreManagerInterface $storeManager
41+
*/
42+
public function __construct(
43+
StoreManagerInterface $storeManager
44+
) {
45+
$this->storeManager = $storeManager;
46+
}
47+
48+
/**
49+
* @param string $websiteCode
50+
* @param string $path
51+
* @param bool $isUrl
52+
* @return string
53+
*/
54+
public function setCorrectWebsiteCodeUrl($websiteCode, $path, $isUrl = false)
55+
{
56+
$element = ($isUrl) ? 3 : 0;
57+
$pathParts = explode('/', ltrim($path, '/'), 5);
58+
$storeCode = "{$websiteCode}_{$pathParts[$element]}";
59+
60+
if ($this->validateStore($storeCode)) {
61+
$pathParts[$element] = $storeCode;
62+
$path = implode('/', $pathParts);
63+
return $path;
64+
}
65+
//returns the original path if request havenot a valid store
66+
return $path;
67+
}
68+
69+
/**
70+
* @param string $storeCode
71+
* @return bool
72+
*/
73+
public function validateStore($storeCode)
74+
{
75+
try {
76+
/** @var \Magento\Store\Api\Data\StoreInterface $store */
77+
$this->storeManager->getStore($storeCode);
78+
return true;
79+
} catch (\Exception $e) {
80+
return false;
81+
}
82+
return false;
83+
}
84+
85+
}
86+
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
4+
namespace Experius\MultipleWebsiteStoreCodeUrl\Plugin\Magento\Store\App\Request;
5+
6+
use Magento\Store\Api\StoreCookieManagerInterface;
7+
use Magento\Store\Model\StoreManagerInterface;
8+
use Experius\MultipleWebsiteStoreCodeUrl\Helper\Settings;
9+
10+
/**
11+
* Class StorePathInfoValidator
12+
* @package Experius\MultipleWebsiteStoreCodeUrl\Plugin\Magento\Store\App\Request
13+
*/
14+
class StorePathInfoValidator
15+
{
16+
17+
/**
18+
* @var \Magento\Store\Model\StoreManagerInterface
19+
*/
20+
private $storeManager;
21+
22+
/**
23+
* @var \Experius\MultipleWebsiteStoreCodeUrl\Helper\Settings
24+
*/
25+
private $settings;
26+
27+
/**
28+
* @var StoreCookieManagerInterface
29+
*/
30+
private $storeCookieManager;
31+
32+
/**
33+
* @var \Magento\Framework\App\Request\PathInfo
34+
*/
35+
private $pathInfo;
36+
37+
38+
/**
39+
* StorePathInfoValidator constructor.
40+
* @param StoreManagerInterface $storeManager
41+
* @param Settings $settings
42+
* @param StoreCookieManagerInterface $storeCookieManager
43+
* @param \Magento\Framework\App\Request\PathInfo $pathInfo
44+
*/
45+
public function __construct(
46+
StoreManagerInterface $storeManager,
47+
Settings $settings,
48+
StoreCookieManagerInterface $storeCookieManager,
49+
\Magento\Framework\App\Request\PathInfo $pathInfo
50+
)
51+
{
52+
$this->storeManager = $storeManager;
53+
$this->settings = $settings;
54+
$this->storeCookieManager = $storeCookieManager;
55+
$this->pathInfo = $pathInfo;
56+
}
57+
58+
/**
59+
* @param \Magento\Store\App\Request\StorePathInfoValidator $subject
60+
* @param $result
61+
* @param $request
62+
* @param string $pathInfo
63+
* @return string
64+
*/
65+
public function afterGetValidStoreCode(
66+
\Magento\Store\App\Request\StorePathInfoValidator $subject,
67+
$result,
68+
$request,
69+
$pathInfo = ''
70+
)
71+
{
72+
if ($result != null || !$this->settings->shouldRemoveWebsiteCodeFromStoreUrl()) {
73+
return $result;
74+
}
75+
if (empty($pathInfo)) {
76+
$pathInfo = $this->pathInfo->getPathInfo(
77+
$request->getRequestUri(),
78+
$request->getBaseUrl()
79+
);
80+
}
81+
$websiteCode = $this->storeCookieManager->getStoreCodeFromCookie();
82+
if (!$websiteCode) {
83+
return $result;
84+
}
85+
$pathParts = explode('/', ltrim($pathInfo, '/'), 2);
86+
$storeCode = "{$websiteCode}_{$pathParts[0]}";
87+
try {
88+
/** @var \Magento\Store\Api\Data\StoreInterface $store */
89+
$this->storeManager->getStore($storeCode);
90+
} catch (\Exception $e) {
91+
return $result;
92+
}
93+
return $storeCode;
94+
}
95+
96+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Experius\MultipleWebsiteStoreCodeUrl\Plugin\Magento\UrlRewrite\Model\StoreSwitcher;
4+
5+
use Experius\MultipleWebsiteStoreCodeUrl\Helper\Data;
6+
use Experius\MultipleWebsiteStoreCodeUrl\Helper\Settings;
7+
use Magento\Store\Model\StoreManagerInterface;
8+
9+
/**
10+
* Class RewriteUrl
11+
* @package Experius\MultipleWebsiteStoreCodeUrl\Plugin\Magento\UrlRewrite\Model\StoreSwitcher
12+
*/
13+
class RewriteUrl
14+
{
15+
/**
16+
* @var StoreManagerInterface
17+
*/
18+
private $storeManager;
19+
20+
/**
21+
* @var Settings
22+
*/
23+
private $settings;
24+
25+
/**
26+
* @var Data
27+
*/
28+
private $data;
29+
30+
/**
31+
* RewriteUrl constructor.
32+
* @param StoreManagerInterface $storeManager
33+
* @param Settings $settings
34+
* @param Data $data
35+
*/
36+
public function __construct(
37+
StoreManagerInterface $storeManager,
38+
Settings $settings,
39+
Data $data
40+
) {
41+
$this->storeManager = $storeManager;
42+
$this->settings = $settings;
43+
$this->data = $data;
44+
}
45+
46+
/**
47+
* @param \Magento\UrlRewrite\Model\StoreSwitcher\RewriteUrl $subject
48+
* @param $fromStore
49+
* @param $targetStore
50+
* @param $redirectUrl
51+
* @return array
52+
* @throws \Magento\Framework\Exception\LocalizedException
53+
*/
54+
public function beforeSwitch(
55+
\Magento\UrlRewrite\Model\StoreSwitcher\RewriteUrl $subject,
56+
$fromStore,
57+
$targetStore,
58+
$redirectUrl
59+
) {
60+
$return = [$fromStore,$targetStore,$redirectUrl];
61+
if (!$this->settings->shouldRemoveWebsiteCodeFromStoreUrl()) {
62+
return $return;
63+
}
64+
65+
// to prevent a 404 when used on a home page
66+
$redirectUrlTemp = rtrim($redirectUrl,'/');
67+
$temp = explode('/',$redirectUrlTemp,5);
68+
if (rtrim($temp[3],'/') == rtrim(end($temp),'/'))
69+
{
70+
return $return;
71+
}
72+
unset($temp);
73+
unset($redirectUrlTemp);
74+
75+
$website = $this->storeManager->getWebsite();
76+
if (!$website) {
77+
return $return;
78+
}
79+
$websiteCode = $website->getCode();
80+
$redirectUrl = $this->data->setCorrectWebsiteCodeUrl($websiteCode,$redirectUrl,true);
81+
82+
return [$fromStore,$targetStore,$redirectUrl];
83+
}
84+
}

Plugin/Store/App/Request/PathInfoProcessor.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121
namespace Experius\MultipleWebsiteStoreCodeUrl\Plugin\Store\App\Request;
2222

23+
use Experius\MultipleWebsiteStoreCodeUrl\Helper\Data;
2324
use Experius\MultipleWebsiteStoreCodeUrl\Helper\Settings;
2425
use Magento\Framework\App\RequestInterface;
2526
use Magento\Framework\Exception\NoSuchEntityException;
@@ -42,13 +43,16 @@ class PathInfoProcessor
4243
* PathInfoProcessor constructor.
4344
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
4445
* @param \Experius\MultipleWebsiteStoreCodeUrl\Helper\Settings $settings
46+
* @param \Experius\MultipleWebsiteStoreCodeUrl\Helper\Data $data
4547
*/
4648
public function __construct(
4749
StoreManagerInterface $storeManager,
48-
Settings $settings
50+
Settings $settings,
51+
Data $data
4952
) {
5053
$this->storeManager = $storeManager;
5154
$this->settings = $settings;
55+
$this->data = $data;
5256
}
5357

5458
public function aroundProcess(
@@ -65,18 +69,8 @@ public function aroundProcess(
6569
if (!$website) {
6670
return $proceed($request, $pathInfo);
6771
}
68-
$pathParts = explode('/', ltrim($pathInfo, '/'), 2);
6972
$websiteCode = $website->getCode();
70-
$storeCode = "{$websiteCode}_{$pathParts[0]}";
71-
72-
try {
73-
/** @var \Magento\Store\Api\Data\StoreInterface $store */
74-
$this->storeManager->getStore($storeCode);
75-
} catch (\Exception $e) {
76-
return $proceed($request, $pathInfo);
77-
}
78-
79-
$pathParts[0] = $storeCode;
80-
return $proceed($request, "/". implode('/', $pathParts));
73+
$newPath = $this->data->setCorrectWebsiteCodeUrl($websiteCode,$pathInfo,false);
74+
return $proceed($request, "/". $newPath);
8175
}
8276
}

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ This module enabled you to use the same store_code multiple times for different
33

44
The module has configuration to enable/disable it in the web/url section.
55

6-
**Important: the store code should include the website code otherwise it won't do anything.**
6+
**Important: the store code should include the website code otherwise it won't do anything.**
7+
8+
# Changelog:
9+
- 2.0.0 added compatibility for magento 2.3.*.
10+
- 2.0.1 fixed issues with not being able to navigate on a not default store + fixed storeswitcher url redirect

etc/di.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@
33
<type name="Magento\Store\App\Request\PathInfoProcessor">
44
<plugin name="Experius_MultipleWebsiteStoreCodeUrl_Plugin_Store_App_Request_PathInfoProcessor" type="Experius\MultipleWebsiteStoreCodeUrl\Plugin\Store\App\Request\PathInfoProcessor" sortOrder="1" />
55
</type>
6-
</config>
6+
<type name="Magento\Store\App\Request\StorePathInfoValidator">
7+
<plugin disabled="false" name="Experius_MultipleWebsiteStoreCodeUrl_Plugin_Magento_Store_App_Request_StorePathInfoValidator" sortOrder="10" type="Experius\MultipleWebsiteStoreCodeUrl\Plugin\Magento\Store\App\Request\StorePathInfoValidator"/>
8+
</type>
9+
<type name="Magento\UrlRewrite\Model\StoreSwitcher\RewriteUrl">
10+
<plugin disabled="false" name="Experius_MultipleWebsiteStoreCodeUrl_Plugin_Magento_UrlRewrite_Model_StoreSwitcher_RewriteUrl" sortOrder="10" type="Experius\MultipleWebsiteStoreCodeUrl\Plugin\Magento\UrlRewrite\Model\StoreSwitcher\RewriteUrl"/>
11+
</type>
12+
</config>

0 commit comments

Comments
 (0)