Skip to content

Commit

Permalink
Merge pull request #12 from EgorDm/bugfix/fix-direct-links
Browse files Browse the repository at this point in the history
Bugfix/fix direct links
  • Loading branch information
Derrick Heesbeen authored Mar 2, 2020
2 parents 8513db4 + 1a79d9a commit 9177d40
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
45 changes: 43 additions & 2 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace Experius\MultipleWebsiteStoreCodeUrl\Helper;

use Magento\Config\Model\Config\Backend\Admin\Custom;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Store\Model\StoreManagerInterface;

Expand All @@ -35,14 +36,22 @@ class Data extends AbstractHelper
*/
protected $storeManager;

/** @var \Magento\Framework\App\ResourceConnection */
protected $resource;

protected $currentWebsite = null;

/**
* Data constructor.
* @param StoreManagerInterface $storeManager
* @param \Magento\Framework\App\ResourceConnection $resource
*/
public function __construct(
StoreManagerInterface $storeManager
StoreManagerInterface $storeManager,
\Magento\Framework\App\ResourceConnection $resource
) {
$this->storeManager = $storeManager;
$this->resource = $resource;
}

/**
Expand Down Expand Up @@ -82,5 +91,37 @@ public function validateStore($storeCode)
return false;
}

}
/**
* @param \Magento\Framework\App\Request\Http $request
* @return int|null
*/
public function getRequestToWebsiteId($request)
{
$baseUrl = str_replace('www.', '', $request->getDistroBaseUrl());

$connection = $this->resource->getConnection();
$table = $connection->getTableName('core_config_data');
$websiteFilter = $connection->select()->from($table, ['scope_id'])
->where('scope = ?', 'websites')
->where('path in (?)', [Custom::XML_PATH_SECURE_BASE_URL, Custom::XML_PATH_UNSECURE_BASE_URL])
->where('value = ?', $baseUrl);
$match = $connection->fetchCol($websiteFilter);

return count($match) > 0 ? (int)$match[0] : null;
}

/**
* Warning: Caches result
* @param \Magento\Framework\App\Request\Http $request
* @return \Magento\Store\Api\Data\WebsiteInterface|null
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getRequestToWebsite($request)
{
if (!$this->currentWebsite) {
$websiteId = $this->getRequestToWebsiteId($request);
$this->currentWebsite = $websiteId ? $this->storeManager->getWebsite($websiteId) : null;
}
return $this->currentWebsite;
}
}
22 changes: 14 additions & 8 deletions Plugin/Magento/Store/App/Request/StorePathInfoValidator.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php


namespace Experius\MultipleWebsiteStoreCodeUrl\Plugin\Magento\Store\App\Request;

use Experius\MultipleWebsiteStoreCodeUrl\Helper\Settings;
use Magento\Store\Api\StoreCookieManagerInterface;
use Magento\Store\Model\StoreManagerInterface;
use Experius\MultipleWebsiteStoreCodeUrl\Helper\Settings;

/**
* Class StorePathInfoValidator
Expand All @@ -24,6 +23,11 @@ class StorePathInfoValidator
*/
private $settings;

/**
* @var \Experius\MultipleWebsiteStoreCodeUrl\Helper\Data
*/
private $helper;

/**
* @var StoreCookieManagerInterface
*/
Expand All @@ -34,7 +38,6 @@ class StorePathInfoValidator
*/
private $pathInfo;


/**
* StorePathInfoValidator constructor.
* @param StoreManagerInterface $storeManager
Expand All @@ -45,12 +48,13 @@ class StorePathInfoValidator
public function __construct(
StoreManagerInterface $storeManager,
Settings $settings,
\Experius\MultipleWebsiteStoreCodeUrl\Helper\Data $helper,
StoreCookieManagerInterface $storeCookieManager,
\Magento\Framework\App\Request\PathInfo $pathInfo
)
{
) {
$this->storeManager = $storeManager;
$this->settings = $settings;
$this->helper = $helper;
$this->storeCookieManager = $storeCookieManager;
$this->pathInfo = $pathInfo;
}
Expand All @@ -67,8 +71,7 @@ public function afterGetValidStoreCode(
$result,
$request,
$pathInfo = ''
)
{
) {
if ($result != null || !$this->settings->shouldRemoveWebsiteCodeFromStoreUrl()) {
return $result;
}
Expand All @@ -79,6 +82,10 @@ public function afterGetValidStoreCode(
);
}
$websiteCode = $this->storeCookieManager->getStoreCodeFromCookie();

if(!$websiteCode && $website = $this->helper->getRequestToWebsite($request)) {
$websiteCode = $website->getCode();
}
if (!$websiteCode) {
return $result;
}
Expand All @@ -92,5 +99,4 @@ public function afterGetValidStoreCode(
}
return $storeCode;
}

}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ 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.**

# Configuration
In order for direct links to work, you must set base urls on website level.

In "Stores -> Configuration -> General -> Web -> Base Urls", "web/unsecure/base_url" or "web/secure/base_url" or both must be filled.
The values must be present at website level.

# 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

0 comments on commit 9177d40

Please sign in to comment.