9

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.

enter image description here


Root Categories and Test Products

I created Store2 Root Category and Store3 Root Category. I also created sub categories under each Root Category.

enter image description here

I added one test product to each sub category.

enter image description here


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.

enter image description here


For Store2, I set Store2 as Name and selected Store2 Root Category as Root Category.

enter image description here


For Store View 2, I selected Store2 as Store, used Store View 2 as Name, and entered store2 as Store Code.

enter image description here


For 3rd Store, I set Store3 as Name and selected Store3 Root Category as Root Category.

enter image description here


For Store View 3, I selected Store3 as Store, used Store View 3 as Name, and entered store3 as Store Code.

enter image description here


URL and Cookie setting

The Configuration screen now shows 5 choices under Store View drop-down:

  1. Default Config
  2. Main Website
  3. Default Store View
  4. Store View 2
  5. Store View 3

enter image description here

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.

enter image description here


For Store View 2, I changed Base URL and Cookie Path.

enter image description here

enter image description here


For Store View 3, I changed Base URL and Cookie Path.

enter image description here

enter image description here


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.

enter image description here


.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.

enter image description here

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?

  • Hi Did you able to fix the issue?? I'm also facing same issue on my site. Login details are not shared among stores under single website. Can you please share your solution – Zammuuz Jan 25 '17 at 10:13
0

We managed to make the store switcher and login a bit more stable with new configuration.

Add Store Code to Urls

The biggest change was setting Add Store Code to Urls to "Yes".

Stores =>
Configuration in Settings section =>
select Default Config =>
Web in GENERAL section =>
Url Options =>
  change the Add Store Code to Urls to "Yes"

enter image description here

 

Remove Cookie Domain

Then, we removed Cookie Domain value from Store2 and Store3 from the Configuration page.

Stores =>
Configuration in Settings section =>
select Store2 or Store3 =>
Web in GENERAL section =>
Default Cookie Settings =>
  remove the value from Cookie Domain and leave it blank

enter image description here

 

Remove custom codes from .htaccess and index.php

Then, we removed custom code lines form .htaccess and index.php.

With the new configuration, the customization on these files were no longer needed. This is great, cleaner code.

 

After these changes, the store switcher, shared shopping cart, and user login became much more stable. Make sure you clear the cache after you make these changes.

 

Magento2 core upgrade and its effect on .htaccess and index.php

Before making config and file changes, we upgraded the Magento2 core from 2.1.0 to 2.1.3 using composer. (Reference: Update Magento 2.1.2 for 2.1.3 )

We are not sure this will be a required part for this multi store configuration, but we performed it hoping the site will be more stable with latest updates from 2.1.3

We had 3 files modified which which were part of core and module files (these files are ignored by our Git version control):

  1. .htaccess
  2. index.php
  3. dev/tools/grunt/configs/themes.js (Grunt config file to compile our custom theme css)

When we upgraded Magento2, these files are reset to initial state of 2.1.3. We did NOT have to manually clean up our .htaccess and index.php files. The file contents were automatically reset by the upgrade. This was a positive side effect.

But there was also a bad side effect. Our custom code in the Grunt config file was reset and erased by the core upgrade. We had to manually restore our custom code lines on this file.

I recommend to take file and DB backups before doing this core upgrade and config/file changes.


Minifying Javascript might help

Minifying JS made the entire site more stable on our site.

Stores =>
Configuration in Settings section =>
select Default Config =>
Developer in ADVANCED section =>
JavaScript Settings =>
  set Merge JavaScript Files to "Yes" =>
  set Enable JavaScript Bundling to "Yes" =>
  set Minify JavaScript Files to "Yes" =>
then, clear cache

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.