3

I am trying to setup a multi store website with shared cart. I followed the instruction on this article: Share cart in magento 2 multistore

However, I noticed that sharing cart functionality does not work. I am only able to add items to the cart for the default store.

I will outline the key steps below.

Created multiple Stores

I have setup 3 Stores with 1 Website. Each Store has its unique Store View.

One thing ambiguous about this article was that what to do with the Default Store View. I changed Store Code for the "Default Store View" from "default" to "store1". And renamed it from "Default Store View" to "Store View 1".

I set Store Code for the 2nd Store as "store2", and same for 3rd Store as "store3".

enter image description here

Root Categories for each store

I used the Default Category as the root category for store1.

I created Store2 Root Category for store2.

I created Store3 Root Category for store3.

enter image description here

Share cart admin setting

Since I wanted to share the cart among 3 Stores, I had to set Share Customer Accounts as "Global" for the Default Config.

enter image description here

Domain names

I created virtual hosts for the default domain as "dev.m2.com".

The 2nd store domain is "dev.m2b.com".

The 3rd store domain is "dev.m2c.com".

.htaccess change

I added this code to .htaccess file on the project root:

SetEnvIf Host dev.m2.com MAGE_RUN_CODE=store1
SetEnvIf Host dev.m2.com MAGE_RUN_TYPE=store
SetEnvIf Host ^dev.m2.com MAGE_RUN_CODE=store1
SetEnvIf Host ^dev.m2.com MAGE_RUN_TYPE=store

SetEnvIf Host dev.m2b.com MAGE_RUN_CODE=store2
SetEnvIf Host dev.m2b.com MAGE_RUN_TYPE=store
SetEnvIf Host ^dev.m2b.com MAGE_RUN_CODE=store2
SetEnvIf Host ^dev.m2b.com MAGE_RUN_TYPE=store

SetEnvIf Host dev.m2c.com MAGE_RUN_CODE=store3
SetEnvIf Host dev.m2c.com MAGE_RUN_TYPE=store
SetEnvIf Host ^dev.m2c.com MAGE_RUN_CODE=store3
SetEnvIf Host ^dev.m2c.com MAGE_RUN_TYPE=store

index.php change

Store Switcher worked fine from switching from store1 to store2, and from store1 to store3, and from store2 to store3. However, switching back from store2/3 to store1 required to use the Store Switcher twice to go back to store1.

Due to 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);

.htaccess change (2nd time)

On the browser's inspector, a lot of CORS error was reported. To remove this, I added this code to .htaccess:

# Always set these headers.
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"

# Added a rewrite to respond with a 200 SUCCESS on every OPTIONS request.
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]

Add to Cart on store2 and store3 do not work

Now at this point, the Store Switcher is working correctly. But, the Add to Cart button is not working on store2 and store3.

I can add items on the store1 (the default store), and it would update the mini cart. But, if I switched to store2 or store3, the cart is empty. And when I try to add an item to the cart, I get one of the following scenarios:

1: The Add to Cart button on store2 and store3 stays on "Adding....", and nothing added to the cart. No JavaScript errors.

enter image description here

2: The Add to Cart button on store2 and store3 changes from "Adding..." to "Added", but the cart is not updated. No JavaScript errors.

enter image description here

Note, this happens for both logged-in and not-logged-in users.

Summary

How can I make the cart be shared among the 3 Stores for logged-in users?

Is it possible to make it work for not-logged-in users?

Having 3 different root categories for each store be part of the issue? Do I have to have a category shared among multiple Stores?

More info

I tried Add Store Code to Urls to Yes, but it did not solve the problem.

The Add to Cart AJAX call on the store2 resulted in "You have no items in your shopping cart." page, rendered from vendor/magento/module-checkout/view/frontend/templates/cart/noItems.phtml It seems store2 cannot find items in the cart.

1 Answer 1

2

The Add to Cart buttons started to work when I performed these 2 tasks from command line:

php bin/magento indexer:reindex
php bin/magento cache:clean

For a reference, I set the Cookie Path to be / for all Stores as follows:

enter image description here enter image description here

I did not change Cookie settings and am using default for Default Config and Main Website. The Cookie Path is set for Store View 1, Store View 2, and Store View 3.

2
  • 1
    Also if still you getting issue then please verify cookie domain if mentioned there in every store
    – Jaimin
    Commented Dec 30, 2019 at 6:14
  • Hi @Yas Nakano, when I am switching from one to another store cart is going to empty. I have disabled all the custom & 3rd party modules and verified but no luck. Version 2.3.5 EE I am using. could you please advise Commented Jul 2, 2020 at 4:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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