Skip to content

Commit

Permalink
Changed defaultStoreCode lookup in URL
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-79 committed Jun 26, 2023
1 parent 9deab4e commit 81dece4
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions Observer/RedirectWithoutStoreCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,36 @@ public function __construct(
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$websiteId = $this->storeManager->getStore()->getWebsiteId();
$defaultStore = $this->storeManager->getWebsite($websiteId)->getDefaultStore();
if ($this->config->isHideDefaultStoreCode() && $code = $this->config->getRedirectCode()) {
$websiteId = $this->storeManager->getStore()->getWebsiteId();
$defaultStore = $this->storeManager->getWebsite($websiteId)->getDefaultStore();

if (!is_null($defaultStore)) {
$url = $this->url->getCurrentUrl();
$pos = strpos($url, $this->storeManager->getStore()->getBaseUrl() . $defaultStore->getCode());
if (!is_null($defaultStore)) {
$url = $this->url->getCurrentUrl();
if ($this->isUrlWithDefaultCode($url, $defaultStore->getCode())) {
$controller = $observer->getData('controller_action');
$url = str_replace('/' . $defaultStore->getCode() . '/', '/', $url);
$this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$controller->getResponse()->setRedirect($url, $code);
}
}
}
}

if (
$this->config->isHideDefaultStoreCode() &&
$pos !== false &&
$code = $this->config->getRedirectCode()
) {
$controller = $observer->getData('controller_action');
$url = str_replace('/' . $defaultStore->getCode() . '/', '/', $url);
$this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$controller->getResponse()->setRedirect($url, $code);
/**
* @param string $url
* @param string $defaultCode
* @return bool
*/
private function isUrlWithDefaultCode($url, $defaultCode)
{
$urlParts = parse_url($url);
if (isset($urlParts['path'])) {
$pathParts = explode('/', ltrim($urlParts['path'], '/'), 2);
if (isset($pathParts[0]) && $pathParts[0] == $defaultCode) {
return true;
}
}
return false;
}
}

0 comments on commit 81dece4

Please # to comment.