Skip to content

Commit

Permalink
Merge pull request #2 from thokiller/master
Browse files Browse the repository at this point in the history
[BUGFIX] fixed storeswitcher + non default navigation
  • Loading branch information
Derrick Heesbeen authored Dec 18, 2019
2 parents 5ab169f + bfb8f39 commit 1919569
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 15 deletions.
86 changes: 86 additions & 0 deletions Helper/Data.php
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 Plugin/Magento/Store/App/Request/StorePathInfoValidator.php
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 Plugin/Magento/UrlRewrite/Model/StoreSwitcher/RewriteUrl.php
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];
}
}
20 changes: 7 additions & 13 deletions Plugin/Store/App/Request/PathInfoProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
namespace Experius\MultipleWebsiteStoreCodeUrl\Plugin\Store\App\Request;

use Experius\MultipleWebsiteStoreCodeUrl\Helper\Data;
use Experius\MultipleWebsiteStoreCodeUrl\Helper\Settings;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\NoSuchEntityException;
Expand All @@ -42,13 +43,16 @@ class PathInfoProcessor
* PathInfoProcessor constructor.
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Experius\MultipleWebsiteStoreCodeUrl\Helper\Settings $settings
* @param \Experius\MultipleWebsiteStoreCodeUrl\Helper\Data $data
*/
public function __construct(
StoreManagerInterface $storeManager,
Settings $settings
Settings $settings,
Data $data
) {
$this->storeManager = $storeManager;
$this->settings = $settings;
$this->data = $data;
}

public function aroundProcess(
Expand All @@ -65,18 +69,8 @@ public function aroundProcess(
if (!$website) {
return $proceed($request, $pathInfo);
}
$pathParts = explode('/', ltrim($pathInfo, '/'), 2);
$websiteCode = $website->getCode();
$storeCode = "{$websiteCode}_{$pathParts[0]}";

try {
/** @var \Magento\Store\Api\Data\StoreInterface $store */
$this->storeManager->getStore($storeCode);
} catch (\Exception $e) {
return $proceed($request, $pathInfo);
}

$pathParts[0] = $storeCode;
return $proceed($request, "/". implode('/', $pathParts));
$newPath = $this->data->setCorrectWebsiteCodeUrl($websiteCode,$pathInfo,false);
return $proceed($request, "/". $newPath);
}
}
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ This module enabled you to use the same store_code multiple times for different

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

**Important: the store code should include the website code otherwise it won't do anything.**
**Important: the store code should include the website code otherwise it won't do anything.**

# Changelog:
- 2.0.0 added compatibility for magento 2.3.*.
- 2.0.1 fixed issues with not being able to navigate on a not default store + fixed storeswitcher url redirect
8 changes: 7 additions & 1 deletion etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@
<type name="Magento\Store\App\Request\PathInfoProcessor">
<plugin name="Experius_MultipleWebsiteStoreCodeUrl_Plugin_Store_App_Request_PathInfoProcessor" type="Experius\MultipleWebsiteStoreCodeUrl\Plugin\Store\App\Request\PathInfoProcessor" sortOrder="1" />
</type>
</config>
<type name="Magento\Store\App\Request\StorePathInfoValidator">
<plugin disabled="false" name="Experius_MultipleWebsiteStoreCodeUrl_Plugin_Magento_Store_App_Request_StorePathInfoValidator" sortOrder="10" type="Experius\MultipleWebsiteStoreCodeUrl\Plugin\Magento\Store\App\Request\StorePathInfoValidator"/>
</type>
<type name="Magento\UrlRewrite\Model\StoreSwitcher\RewriteUrl">
<plugin disabled="false" name="Experius_MultipleWebsiteStoreCodeUrl_Plugin_Magento_UrlRewrite_Model_StoreSwitcher_RewriteUrl" sortOrder="10" type="Experius\MultipleWebsiteStoreCodeUrl\Plugin\Magento\UrlRewrite\Model\StoreSwitcher\RewriteUrl"/>
</type>
</config>

0 comments on commit 1919569

Please sign in to comment.