Magento – Magento 2 :Share Customer Login and Cart Between 2 Stores in Website

magento2multidomainmultistore

I have two stores in website. The store switching is working fine. But when I login in Store1 and switching to Store2, I need to login again separately in store2.

My stores are using different domain like womenmysite.com and menmysite.com . Both have two separate store views also. In Store -> Configuration I have changed Share Customer Accounts to Global under the store view Default Config.

I don't know how to give cookie domain for my stores. I have given cookie path as '/' for the store views. I also need to share cart between these stores.

Best Answer

Looks like there no easy way for this. I will suggest a method, but it is upto you make it work.

Create 4 domain names points to your server. womenmysite.com, menmysite.com, women.mastersite.com, men.mastersite.com

Create a proxy from womenmysite.com -> women.mastersite.com and another proxy from menmysite.com -> men.mastersite.com. For creating a proxy please refer mod_proxy module for apache http server or you can find similar modules for other web servers. If your server behind any other proxy like firewall or load balancer, try to configure these proxies there itself instead of creating one more layer.

you can set cookie domain as .mastersite.com and store sites as men.mastersite.com and women.mastersite.com. Also, you will face some issue with proxy and Magento authentication system. But I think it can be fixed easily.

Here comes the tricky part. You will manually need to write a PHP script that will synchronize the sessions between two websites. you can either implement this using a cross-domain script include or an iframe. For example for menmysite.com.

://womenmysite.com/sync-session?id=some encrypted data

This encrypted data should contain all the cookies sent to menmysite.com. This script should set the same cookies send to menmysite.com to womenmysite.com.

Also, the similar script should be there for womenmysite.com that links to menmysite.com.

You have to use an invisible iframe too. I have no idea how much tweak required to make this work as I worked with Magento some time ago.