Share Cart in Magento 2 Multistore Website

Magento provides and incredible feature to create multiple stores under same code base and admin panel. We can share cart in magento 2 multistore website. You can run variety of stores from a single Magento admin panel. You may have setup different stores on subdomains like furniture.mystore.com and fashion.mystore.com. Or it could be on separate domains like myfurniturestore.com and myfashionstore.com. Now you would like your customers to buy from furniture store and then carry the same cart to fashion store or vice versa. Its a good feature to share carts and customer accounts across your multiple stores. In this article we will see how we can achieve this feature in Magento 2 without any coding required.
Here we will assume that we want to setup multiple stores on different domains. So we will start by creating multiple store views and stores.

1. Create Stores

Go to Admin -> Stores -> Settings -> All Stores and create multiple store views and stores. Assign the store views to respective stores. Your store configuration might look like:

stores

Also check if you have assigned respective root categories to their stores like this:

stores-1_1

2. Assign Domains to Stores

We will assign our primary domain to the main store.
a. Go to Admin -> Stores -> Settings -> Configuration. Now click on General -> Web in the left tab. In the “Base URLs” section, update the “Base URL” configuration. Here you will add your primary domain like http://mystore.com/
b. If you have SSL enabled domain, add the https://mystore.com in “Base URLs (Secure)” section.
c. Open the “Default Cookie Settings” section on the same pagestores-1
d. Set the “Cookie Path” setting to “/”.

stores-2
e. Save the configuration.

Now select the second store view from store selector dropdown on the top. Perform the above specified steps for second store also.

stores-3

3. Share Customer Accounts

If we want to share the customer accounts on different stores, we will have to change one more setting.
a. Go to Stores -> Customers -> Customer Configuration tab.
b. Find the “Account Sharing Options” section.
c. Set the “Share Customer Accounts” setting to “Global”
d. Save the configuration

customer-1

Now we are done with the Magento configuration.

Server Configuration

Next step is to configure our Web server (Apache/Nginx) to enable multistore installation. In this article we will see how to configure Apache.
a. Open the .htaccess at the root of the Magento 2 installation
b. On the top of .htaccess file, add following lines:

SetEnvIf Host myfurniturestore.com MAGE_RUN_CODE=store1
SetEnvIf Host myfurniturestore.com MAGE_RUN_TYPE=store
SetEnvIf Host ^myfurniturestore.com MAGE_RUN_CODE=store1
SetEnvIf Host ^myfurniturestore.com MAGE_RUN_TYPE=store

SetEnvIf Host myfashionstore.com MAGE_RUN_CODE=store2
SetEnvIf Host myfashionstore.com MAGE_RUN_TYPE=store
SetEnvIf Host ^myfashionstore.com MAGE_RUN_CODE=store2
SetEnvIf Host ^myfashionstore.com MAGE_RUN_TYPE=store

c. Save the .htaccess file.

Thats all for configuring different stores on multiple domains.

IMPORTANT:
But remember that Magento 2 cant share the cart and customer session if the different stores are accessed directly. Because Magento needs to see the Session ID to enable cart and customer account sharing. So we need to add the SID parameter to URLs pointing to other stores from our site. It is better to use Magento’s default store switcher for moving between different stores so that cart and customer session can be shared.

Written by Arvind Bhardwaj

Arvind is a Magento and WordPress expert with more than 6 years of industry wide experience.

Website: http://www.webspeaks.in/

2 thoughts on “Share Cart in Magento 2 Multistore Website

  1. Thanks for this nice article. Have a question. We would like to share the customer session, but want to have different cart for each store. Possible?

Leave a Reply

Your email address will not be published. Required fields are marked *

four × 4 =