I am trying to setup a multi store with shared cart. However, the user login sometimes does not work.
The Store Switcher
works fine, but the frontend User Login
on 2nd and 3rd Store
requires me to click the Sign In
button twice to login.
I have setup multi stores with these setups:
Multi Domain (Virtual Hosts)
I am using one Website
. The site will have three Stores
. The domain names will share one common base domain. The additional stores will use subdomains.
I created magento2.com as the default base domain.
I created store2.magento2.com as the 2nd store.
I created store3.magento2.com as the 3rd store.
Root Categories and Test Products
I created Store2 Root Category and Store3 Root Category. I also created sub categories under each Root Category.
I added one test product to each sub category.
Stores and Views
I did NOT change anything on Main Website Store and Default Store View.
I created Store2 and Store View 2 for 2nd store.
I created Store3 and Store View 3 for 3rd store.
For Store2, I set Store2 as Name
and selected Store2 Root Category as Root Category
.
For Store View 2, I selected Store2 as Store
, used Store View 2 as Name
, and entered store2 as Store Code
.
For 3rd Store
, I set Store3 as Name
and selected Store3 Root Category as Root Category
.
For Store View 3, I selected Store3 as Store
, used Store View 3 as Name
, and entered store3 as Store Code
.
URL and Cookie setting
The Configuration
screen now shows 5 choices under Store View
drop-down:
Default Config
Main Website
Default Store View
- Store View 2
- Store View 3
I changed URL and Cookie settings for Default Store View
, Store View 2, and Store View 3.
I did NOT change URL or Cookie settings for Default Config
and Main Website
.
For Default Store View
, I changed Cookie Path
.
For Store View 2, I changed Base URL
and Cookie Path
.
For Store View 3, I changed Base URL
and Cookie Path
.
Account Sharing
To share user accounts and shopping cart among multiple stores, I changed Share Customer Accounts
from "Per Website" to Global under Default Config
.
.htaccess change
I added these lines at the top of .htaccess
file at the project root directory.
#Multi store setting
SetEnvIf Host magento2.com MAGE_RUN_CODE=default
SetEnvIf Host magento2.com MAGE_RUN_TYPE=store
SetEnvIf Host ^magento2.com MAGE_RUN_CODE=default
SetEnvIf Host ^magento2.com MAGE_RUN_TYPE=store
SetEnvIf Host store2.magento2.com MAGE_RUN_CODE=store2
SetEnvIf Host store2.magento2.com MAGE_RUN_TYPE=store
SetEnvIf Host ^store2.magento2.com MAGE_RUN_CODE=store2
SetEnvIf Host ^store2.magento2.com MAGE_RUN_TYPE=store
SetEnvIf Host store3.magento2.com MAGE_RUN_CODE=store3
SetEnvIf Host store3.magento2.com MAGE_RUN_TYPE=store
SetEnvIf Host ^store3.magento2.com MAGE_RUN_CODE=store3
SetEnvIf Host ^store3.magento2.com MAGE_RUN_TYPE=store
index.php change
Using the Store Switcher
, sometimes required to click twice to switch from store2/3 to the default. To fix this, I had to reset the value for $_COOKIE['store']
at the index.php:
<?php
/**
* Application entry point
*
* Example - run a particular store or website:
* --------------------------------------------
* require __DIR__ . '/app/bootstrap.php';
* $params = $_SERVER;
* $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'website2';
* $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
* $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
* \/** @var \Magento\Framework\App\Http $app *\/
* $app = $bootstrap->createApplication('Magento\Framework\App\Http');
* $bootstrap->run($app);
* --------------------------------------------
*
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
try {
require __DIR__ . '/app/bootstrap.php';
} catch (\Exception $e) {
echo <<<HTML
<div style="font:12px/1.35em arial, helvetica, sans-serif;">
<div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;">
<h3 style="margin:0;font-size:1.7em;font-weight:normal;text-transform:none;text-align:left;color:#2f2f2f;">
Autoload error</h3>
</div>
<p>{$e->getMessage()}</p>
</div>
HTML;
exit(1);
}
// Fix store switcher bug on switching to the default store
if ( isset($_SERVER['MAGE_RUN_TYPE']) == 'store' && isset($_SERVER['MAGE_RUN_CODE']) ) {
if ( !empty($_SERVER['MAGE_RUN_CODE']) ) {
$_COOKIE['store'] = $_SERVER['MAGE_RUN_CODE'];
}
}
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$bootstrap->run($app);
User login sometimes requires to click twice
With the above setup, the Store Switcher
is working fine. The shopping Cart
is shared among all 3 Stores for both logged-in and not-logged-in users.
However, on Store2 and Store3, the frontend user login
sometimes redirects back to the same login page. If I press the Sign In
button on the redirected login page (2nd time to click this button), it let's me login.
Is there a way to fix this problem?
Update: disabling Cache
I disabled the caching from command line:
php bin/magento cache:disable
With disabled cache, the user login
on Store2 and Store3 became much more stable. It still redirected back to the login page 1 out of 10 attempts, but this change made the login to work better.
However, the entire site became slow!!!
Is there a way to properly configure the cache to make the user login
to perform correctly on a multi Store
setup?