diff --git a/Model/Config.php b/Model/Config.php new file mode 100644 index 0000000..ba74a87 --- /dev/null +++ b/Model/Config.php @@ -0,0 +1,33 @@ +scopeConfig->isSetFlag(self::XML_PATH_USE_PER_STORE_CONFIG); + } + + public function getStoreIdsWithoutStoreCode(): array + { + $value = $this->scopeConfig->getValue(self::XML_PATH_STORES_WITHOUT_STORE_CODE); + + if (empty($value)) { + return []; + } + + return array_map('intval', explode(',', $value)); + } +} diff --git a/Plugin/Store/HideDefaultStoreCodePlugin.php b/Plugin/Store/HideDefaultStoreCodePlugin.php index ad26c57..1606097 100644 --- a/Plugin/Store/HideDefaultStoreCodePlugin.php +++ b/Plugin/Store/HideDefaultStoreCodePlugin.php @@ -4,16 +4,34 @@ namespace CustomGento\DefaultStoreCodeRemover\Plugin\Store; +use CustomGento\DefaultStoreCodeRemover\Model\Config; use Magento\Store\Model\Store; class HideDefaultStoreCodePlugin { - public function afterIsUseStoreInUrl(Store $subject, bool $resultIsUseInUrl): bool + public function __construct(private readonly Config $config) { - if ($subject->getCode() !== Store::ADMIN_CODE && $subject->isDefault()) { + } + + public function afterIsUseStoreInUrl(Store $subject, bool $storeCodeShallBeIncludedInUrl): bool + { + if (!$storeCodeShallBeIncludedInUrl) { + return $storeCodeShallBeIncludedInUrl; + } + + if ($subject->getCode() === Store::ADMIN_CODE) { + return $storeCodeShallBeIncludedInUrl; + } + + if ($this->config->isPerStoreConfigEnabled()) { + return !in_array((int)$subject->getId(), $this->config->getStoreIdsWithoutStoreCode(), true); + } + + // Default / legacy behaviour: only strip the code for the default store + if ($subject->isDefault()) { return false; } - return $resultIsUseInUrl; + return $storeCodeShallBeIncludedInUrl; } } diff --git a/README.md b/README.md index f9f2b74..721d548 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,17 @@ Default Store Code Remover for Magento 2 hides the store code in the default store from the URL. The module is in effect only when web/url/use_store is enabled and is meant to be used for multistore setups, where the default shop should not contain any store code, whereas all other stores should. +The module also provides an optional, extended configuration to control store code removal **per store view**: + +- Configuration path: **Store > Configuration > Genral > Web** +- Setting: **Configure Store Code Removal Per Store** (Yes/No) + - Default value: **No** (backwards compatible) + - Uses the original behavior: the store code is removed from the **default store view** URL, while all other store views keep their store code in the URL. + - If set to **Yes** + - An additional setting becomes available with a list of **store views**. + - Only the **selected store views** will have the store code removed from the URL. + - All **non-selected store views** will keep the store code in the URL. + ## Installation * composer require customgento/module-default-store-code-remover-m2 @@ -16,13 +27,13 @@ When the store codes in the url are disabled, the URL of the stores looks like t When the store codes in the url are enabled, the URL of the stores looks like this: `https://website.com/store_code/`. When this extension is installed and enabled, and the store codes are enabled to be shown in the url under -Store > Configuration > General > Web >Add Store Code, the module will not show the store code for the default store +Store > Configuration > General > Web >Add Store Code, the module will not show the store code for the default store but will show it for any other store. So, for the default store the url will be `https://website.com/` while for all other store, the URL will be : `https://website.com/store_code` ## License -[OSL - Open Software Licence 3.0](https://opensource.org/licenses/osl-3.0.php) +[OSL - Open Software Licence 3.0](https://opensource.org/licenses/osl--3.0.php) ## Copyright © 2021 - present CustomGento GmbH diff --git a/composer.json b/composer.json index 4e52d6c..354e10c 100644 --- a/composer.json +++ b/composer.json @@ -2,6 +2,7 @@ "name": "customgento/module-default-store-code-remover-m2", "description": "Default Store Code Remover for Magento 2 hides the store code in the default store from the URL.", "type": "magento2-module", + "version":"3.0.0", "license": "OSL-3.0", "authors": [ { @@ -10,7 +11,7 @@ } ], "require": { - "php": "~7.3.0||~7.4.0||~8.1.0||~8.2.0||~8.3.0||~8.4.0", + "php": "~8.1.0||~8.2.0||~8.3.0||~8.4.0", "magento/module-store": "~101.0" }, "suggest": { diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml new file mode 100644 index 0000000..1fa44bb --- /dev/null +++ b/etc/adminhtml/system.xml @@ -0,0 +1,27 @@ + + + +
+ + + + No (default): The default store has its store code removed from the URL; all other stores keep it.
+ Yes: You choose individually which stores have their store code removed.]]>
+ Magento\Config\Model\Config\Source\Yesno + + 1 + +
+ + + Select every store whose store code should be removed from the URL. + Magento\Config\Model\Config\Source\Store + + 1 + 1 + + +
+
+
+
diff --git a/etc/config.xml b/etc/config.xml new file mode 100644 index 0000000..9f082fd --- /dev/null +++ b/etc/config.xml @@ -0,0 +1,11 @@ + + + + + + 0 + + + + +