Skip to content

Commit 9177d40

Browse files
author
Derrick Heesbeen
authored
Merge pull request #12 from EgorDm/bugfix/fix-direct-links
Bugfix/fix direct links
2 parents 8513db4 + 1a79d9a commit 9177d40

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed

Helper/Data.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace Experius\MultipleWebsiteStoreCodeUrl\Helper;
2323

24+
use Magento\Config\Model\Config\Backend\Admin\Custom;
2425
use Magento\Framework\App\Helper\AbstractHelper;
2526
use Magento\Store\Model\StoreManagerInterface;
2627

@@ -35,14 +36,22 @@ class Data extends AbstractHelper
3536
*/
3637
protected $storeManager;
3738

39+
/** @var \Magento\Framework\App\ResourceConnection */
40+
protected $resource;
41+
42+
protected $currentWebsite = null;
43+
3844
/**
3945
* Data constructor.
4046
* @param StoreManagerInterface $storeManager
47+
* @param \Magento\Framework\App\ResourceConnection $resource
4148
*/
4249
public function __construct(
43-
StoreManagerInterface $storeManager
50+
StoreManagerInterface $storeManager,
51+
\Magento\Framework\App\ResourceConnection $resource
4452
) {
4553
$this->storeManager = $storeManager;
54+
$this->resource = $resource;
4655
}
4756

4857
/**
@@ -82,5 +91,37 @@ public function validateStore($storeCode)
8291
return false;
8392
}
8493

85-
}
94+
/**
95+
* @param \Magento\Framework\App\Request\Http $request
96+
* @return int|null
97+
*/
98+
public function getRequestToWebsiteId($request)
99+
{
100+
$baseUrl = str_replace('www.', '', $request->getDistroBaseUrl());
86101

102+
$connection = $this->resource->getConnection();
103+
$table = $connection->getTableName('core_config_data');
104+
$websiteFilter = $connection->select()->from($table, ['scope_id'])
105+
->where('scope = ?', 'websites')
106+
->where('path in (?)', [Custom::XML_PATH_SECURE_BASE_URL, Custom::XML_PATH_UNSECURE_BASE_URL])
107+
->where('value = ?', $baseUrl);
108+
$match = $connection->fetchCol($websiteFilter);
109+
110+
return count($match) > 0 ? (int)$match[0] : null;
111+
}
112+
113+
/**
114+
* Warning: Caches result
115+
* @param \Magento\Framework\App\Request\Http $request
116+
* @return \Magento\Store\Api\Data\WebsiteInterface|null
117+
* @throws \Magento\Framework\Exception\LocalizedException
118+
*/
119+
public function getRequestToWebsite($request)
120+
{
121+
if (!$this->currentWebsite) {
122+
$websiteId = $this->getRequestToWebsiteId($request);
123+
$this->currentWebsite = $websiteId ? $this->storeManager->getWebsite($websiteId) : null;
124+
}
125+
return $this->currentWebsite;
126+
}
127+
}

Plugin/Magento/Store/App/Request/StorePathInfoValidator.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<?php
22

3-
43
namespace Experius\MultipleWebsiteStoreCodeUrl\Plugin\Magento\Store\App\Request;
54

5+
use Experius\MultipleWebsiteStoreCodeUrl\Helper\Settings;
66
use Magento\Store\Api\StoreCookieManagerInterface;
77
use Magento\Store\Model\StoreManagerInterface;
8-
use Experius\MultipleWebsiteStoreCodeUrl\Helper\Settings;
98

109
/**
1110
* Class StorePathInfoValidator
@@ -24,6 +23,11 @@ class StorePathInfoValidator
2423
*/
2524
private $settings;
2625

26+
/**
27+
* @var \Experius\MultipleWebsiteStoreCodeUrl\Helper\Data
28+
*/
29+
private $helper;
30+
2731
/**
2832
* @var StoreCookieManagerInterface
2933
*/
@@ -34,7 +38,6 @@ class StorePathInfoValidator
3438
*/
3539
private $pathInfo;
3640

37-
3841
/**
3942
* StorePathInfoValidator constructor.
4043
* @param StoreManagerInterface $storeManager
@@ -45,12 +48,13 @@ class StorePathInfoValidator
4548
public function __construct(
4649
StoreManagerInterface $storeManager,
4750
Settings $settings,
51+
\Experius\MultipleWebsiteStoreCodeUrl\Helper\Data $helper,
4852
StoreCookieManagerInterface $storeCookieManager,
4953
\Magento\Framework\App\Request\PathInfo $pathInfo
50-
)
51-
{
54+
) {
5255
$this->storeManager = $storeManager;
5356
$this->settings = $settings;
57+
$this->helper = $helper;
5458
$this->storeCookieManager = $storeCookieManager;
5559
$this->pathInfo = $pathInfo;
5660
}
@@ -67,8 +71,7 @@ public function afterGetValidStoreCode(
6771
$result,
6872
$request,
6973
$pathInfo = ''
70-
)
71-
{
74+
) {
7275
if ($result != null || !$this->settings->shouldRemoveWebsiteCodeFromStoreUrl()) {
7376
return $result;
7477
}
@@ -79,6 +82,10 @@ public function afterGetValidStoreCode(
7982
);
8083
}
8184
$websiteCode = $this->storeCookieManager->getStoreCodeFromCookie();
85+
86+
if(!$websiteCode && $website = $this->helper->getRequestToWebsite($request)) {
87+
$websiteCode = $website->getCode();
88+
}
8289
if (!$websiteCode) {
8390
return $result;
8491
}
@@ -92,5 +99,4 @@ public function afterGetValidStoreCode(
9299
}
93100
return $storeCode;
94101
}
95-
96102
}

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ The module has configuration to enable/disable it in the web/url section.
55

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

8+
# Configuration
9+
In order for direct links to work, you must set base urls on website level.
10+
11+
In "Stores -> Configuration -> General -> Web -> Base Urls", "web/unsecure/base_url" or "web/secure/base_url" or both must be filled.
12+
The values must be present at website level.
13+
814
# Changelog:
915
- 2.0.0 added compatibility for magento 2.3.*.
1016
- 2.0.1 fixed issues with not being able to navigate on a not default store + fixed storeswitcher url redirect

0 commit comments

Comments
 (0)