From 7342d7e7fa0262ff96829afae46723156f780e22 Mon Sep 17 00:00:00 2001 From: Roy Duineveld Date: Thu, 29 Jun 2023 16:57:38 +0200 Subject: [PATCH] Determine the site by MAGE_RUN_CODE --- README.md | 2 +- src/Extend/SitesLinkedToMagentoStores.php | 21 +++++++++++++++++++++ src/RapidezStatamicServiceProvider.php | 9 +++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/Extend/SitesLinkedToMagentoStores.php diff --git a/README.md b/README.md index 11d2d04..b3aa9cb 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ php artisan vendor:publish --provider="Rapidez\Statamic\RapidezStatamicServicePr ### Magento Store ID -It is important to add the Magento store ID in the attributes section within `config/statamic/sites.php` for every site +It is important to add the Magento store ID in the attributes section within `config/statamic/sites.php` for every site and use the store code as key. The current site will be determined based on the `MAGE_RUN_CODE`. By default Statamic uses the url for this; that's still the fallback. ```php 'sites' => [ diff --git a/src/Extend/SitesLinkedToMagentoStores.php b/src/Extend/SitesLinkedToMagentoStores.php new file mode 100644 index 0000000..f977115 --- /dev/null +++ b/src/Extend/SitesLinkedToMagentoStores.php @@ -0,0 +1,21 @@ +sites)->get($code); + } + + public function current() + { + return $this->current + ?? $this->findByMageRunCode(request()->server('MAGE_RUN_CODE')) + ?? $this->findByUrl(request()->getUri()) + ?? $this->default(); + } +} diff --git a/src/RapidezStatamicServiceProvider.php b/src/RapidezStatamicServiceProvider.php index 9433313..3f0771e 100644 --- a/src/RapidezStatamicServiceProvider.php +++ b/src/RapidezStatamicServiceProvider.php @@ -9,6 +9,7 @@ use Illuminate\Support\ServiceProvider; use Illuminate\View\View as RenderedView; use Rapidez\Core\Facades\Rapidez; +use Rapidez\Statamic\Extend\SitesLinkedToMagentoStores; use Rapidez\Statamic\Forms\JsDrivers\Vue; use Rapidez\Statamic\Http\Controllers\StatamicRewriteController; use Rapidez\Statamic\Http\ViewComposers\StatamicGlobalDataComposer; @@ -16,10 +17,18 @@ use Statamic\Events\GlobalSetSaved; use Statamic\Facades\Entry; use Statamic\Facades\Site; +use Statamic\Sites\Sites; use Statamic\Statamic; class RapidezStatamicServiceProvider extends ServiceProvider { + public function register() + { + $this->app->extend(Sites::class, function () { + return new SitesLinkedToMagentoStores(config('statamic.sites')); + }); + } + public function boot() { $this