-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from thokiller/master
[BUGFIX] fixed storeswitcher + non default navigation
- Loading branch information
Showing
6 changed files
with
285 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
/** | ||
* A Magento 2 module named Experius MultipleWebsiteStoreCodeUrl | ||
* Copyright (C) 2017 Experius | ||
* | ||
* This file is part of Experius MultipleWebsiteStoreCodeUrl. | ||
* | ||
* Experius MultipleWebsiteStoreCodeUrl is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
namespace Experius\MultipleWebsiteStoreCodeUrl\Helper; | ||
|
||
use Magento\Framework\App\Helper\AbstractHelper; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
|
||
/** | ||
* Class Data | ||
* @package Experius\MultipleWebsiteStoreCodeUrl\Helper | ||
*/ | ||
class Data extends AbstractHelper | ||
{ | ||
/** | ||
* @var StoreManagerInterface | ||
*/ | ||
protected $storeManager; | ||
|
||
/** | ||
* Data constructor. | ||
* @param StoreManagerInterface $storeManager | ||
*/ | ||
public function __construct( | ||
StoreManagerInterface $storeManager | ||
) { | ||
$this->storeManager = $storeManager; | ||
} | ||
|
||
/** | ||
* @param string $websiteCode | ||
* @param string $path | ||
* @param bool $isUrl | ||
* @return string | ||
*/ | ||
public function setCorrectWebsiteCodeUrl($websiteCode, $path, $isUrl = false) | ||
{ | ||
$element = ($isUrl) ? 3 : 0; | ||
$pathParts = explode('/', ltrim($path, '/'), 5); | ||
$storeCode = "{$websiteCode}_{$pathParts[$element]}"; | ||
|
||
if ($this->validateStore($storeCode)) { | ||
$pathParts[$element] = $storeCode; | ||
$path = implode('/', $pathParts); | ||
return $path; | ||
} | ||
//returns the original path if request havenot a valid store | ||
return $path; | ||
} | ||
|
||
/** | ||
* @param string $storeCode | ||
* @return bool | ||
*/ | ||
public function validateStore($storeCode) | ||
{ | ||
try { | ||
/** @var \Magento\Store\Api\Data\StoreInterface $store */ | ||
$this->storeManager->getStore($storeCode); | ||
return true; | ||
} catch (\Exception $e) { | ||
return false; | ||
} | ||
return false; | ||
} | ||
|
||
} | ||
|
96 changes: 96 additions & 0 deletions
96
Plugin/Magento/Store/App/Request/StorePathInfoValidator.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
|
||
namespace Experius\MultipleWebsiteStoreCodeUrl\Plugin\Magento\Store\App\Request; | ||
|
||
use Magento\Store\Api\StoreCookieManagerInterface; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
use Experius\MultipleWebsiteStoreCodeUrl\Helper\Settings; | ||
|
||
/** | ||
* Class StorePathInfoValidator | ||
* @package Experius\MultipleWebsiteStoreCodeUrl\Plugin\Magento\Store\App\Request | ||
*/ | ||
class StorePathInfoValidator | ||
{ | ||
|
||
/** | ||
* @var \Magento\Store\Model\StoreManagerInterface | ||
*/ | ||
private $storeManager; | ||
|
||
/** | ||
* @var \Experius\MultipleWebsiteStoreCodeUrl\Helper\Settings | ||
*/ | ||
private $settings; | ||
|
||
/** | ||
* @var StoreCookieManagerInterface | ||
*/ | ||
private $storeCookieManager; | ||
|
||
/** | ||
* @var \Magento\Framework\App\Request\PathInfo | ||
*/ | ||
private $pathInfo; | ||
|
||
|
||
/** | ||
* StorePathInfoValidator constructor. | ||
* @param StoreManagerInterface $storeManager | ||
* @param Settings $settings | ||
* @param StoreCookieManagerInterface $storeCookieManager | ||
* @param \Magento\Framework\App\Request\PathInfo $pathInfo | ||
*/ | ||
public function __construct( | ||
StoreManagerInterface $storeManager, | ||
Settings $settings, | ||
StoreCookieManagerInterface $storeCookieManager, | ||
\Magento\Framework\App\Request\PathInfo $pathInfo | ||
) | ||
{ | ||
$this->storeManager = $storeManager; | ||
$this->settings = $settings; | ||
$this->storeCookieManager = $storeCookieManager; | ||
$this->pathInfo = $pathInfo; | ||
} | ||
|
||
/** | ||
* @param \Magento\Store\App\Request\StorePathInfoValidator $subject | ||
* @param $result | ||
* @param $request | ||
* @param string $pathInfo | ||
* @return string | ||
*/ | ||
public function afterGetValidStoreCode( | ||
\Magento\Store\App\Request\StorePathInfoValidator $subject, | ||
$result, | ||
$request, | ||
$pathInfo = '' | ||
) | ||
{ | ||
if ($result != null || !$this->settings->shouldRemoveWebsiteCodeFromStoreUrl()) { | ||
return $result; | ||
} | ||
if (empty($pathInfo)) { | ||
$pathInfo = $this->pathInfo->getPathInfo( | ||
$request->getRequestUri(), | ||
$request->getBaseUrl() | ||
); | ||
} | ||
$websiteCode = $this->storeCookieManager->getStoreCodeFromCookie(); | ||
if (!$websiteCode) { | ||
return $result; | ||
} | ||
$pathParts = explode('/', ltrim($pathInfo, '/'), 2); | ||
$storeCode = "{$websiteCode}_{$pathParts[0]}"; | ||
try { | ||
/** @var \Magento\Store\Api\Data\StoreInterface $store */ | ||
$this->storeManager->getStore($storeCode); | ||
} catch (\Exception $e) { | ||
return $result; | ||
} | ||
return $storeCode; | ||
} | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
Plugin/Magento/UrlRewrite/Model/StoreSwitcher/RewriteUrl.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace Experius\MultipleWebsiteStoreCodeUrl\Plugin\Magento\UrlRewrite\Model\StoreSwitcher; | ||
|
||
use Experius\MultipleWebsiteStoreCodeUrl\Helper\Data; | ||
use Experius\MultipleWebsiteStoreCodeUrl\Helper\Settings; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
|
||
/** | ||
* Class RewriteUrl | ||
* @package Experius\MultipleWebsiteStoreCodeUrl\Plugin\Magento\UrlRewrite\Model\StoreSwitcher | ||
*/ | ||
class RewriteUrl | ||
{ | ||
/** | ||
* @var StoreManagerInterface | ||
*/ | ||
private $storeManager; | ||
|
||
/** | ||
* @var Settings | ||
*/ | ||
private $settings; | ||
|
||
/** | ||
* @var Data | ||
*/ | ||
private $data; | ||
|
||
/** | ||
* RewriteUrl constructor. | ||
* @param StoreManagerInterface $storeManager | ||
* @param Settings $settings | ||
* @param Data $data | ||
*/ | ||
public function __construct( | ||
StoreManagerInterface $storeManager, | ||
Settings $settings, | ||
Data $data | ||
) { | ||
$this->storeManager = $storeManager; | ||
$this->settings = $settings; | ||
$this->data = $data; | ||
} | ||
|
||
/** | ||
* @param \Magento\UrlRewrite\Model\StoreSwitcher\RewriteUrl $subject | ||
* @param $fromStore | ||
* @param $targetStore | ||
* @param $redirectUrl | ||
* @return array | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
*/ | ||
public function beforeSwitch( | ||
\Magento\UrlRewrite\Model\StoreSwitcher\RewriteUrl $subject, | ||
$fromStore, | ||
$targetStore, | ||
$redirectUrl | ||
) { | ||
$return = [$fromStore,$targetStore,$redirectUrl]; | ||
if (!$this->settings->shouldRemoveWebsiteCodeFromStoreUrl()) { | ||
return $return; | ||
} | ||
|
||
// to prevent a 404 when used on a home page | ||
$redirectUrlTemp = rtrim($redirectUrl,'/'); | ||
$temp = explode('/',$redirectUrlTemp,5); | ||
if (rtrim($temp[3],'/') == rtrim(end($temp),'/')) | ||
{ | ||
return $return; | ||
} | ||
unset($temp); | ||
unset($redirectUrlTemp); | ||
|
||
$website = $this->storeManager->getWebsite(); | ||
if (!$website) { | ||
return $return; | ||
} | ||
$websiteCode = $website->getCode(); | ||
$redirectUrl = $this->data->setCorrectWebsiteCodeUrl($websiteCode,$redirectUrl,true); | ||
|
||
return [$fromStore,$targetStore,$redirectUrl]; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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