how to make opencart multi-store share same cart o

2020-04-16 04:37发布

问题:

I have the following scenario:

  • domain1.com - store1 - opencart 1.5.6 installed
  • domain2.com - store2
  • domain3.com - store3

I managed to install all 3 stores on different domains and all of them are using the same opencart install (store1). So far all seems to work properly. What I need is when the user is on domain1.com, adds product1 in his cart, then navigates to store2 (domain2.com) and adds product2 in his cart - now he should have both product1 from shop1 and product2 from shop2 in his cart. In other words, all stores should behave like one, if the user is logged in into store1 then he should keep his login session, his cart and all other options when he navigates to store2 or store3.

Is there any way to achieve this?

NOTE: I know how to achieve this if the stores are installed in multiple subdomains, but not using different domains.

回答1:

I just found a solution for this problem so here's what I did:

I modified /system/library/session.php as follows:

if ($_SERVER['HTTP_HOST'] != 'store1.loc') {
   if (!isset($_COOKIE["PHPSESSID"]) && !isset($_GET['session_id'])) { 
      $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";//get current URL
      header('Location: http://store1.loc?getsession=true&url='.base64_encode($actual_link));
   }
   elseif (isset($_GET['session_id'])) { //set session based on the session_id received from main store
      session_destroy();
      session_id($_GET['session_id']);
      session_start();
      header('Location: '. base64_decode($_GET['url'])); //redirect to original requested url
   }
}
else {
   if (isset($_GET['getsession'])) { //send back session_id
      header('Location: http://store2.loc?session_id='.urlencode(session_id()) . '&url=' . $_GET['url']);
   }
}

Explanation: If the user enters any store that is not the main store a redirect is made to the main store, a session is started if not present, then the session id is sent back to the requester via GET parameter in url, then a session is started using the same session id, then it redirects back to the original requested URL. The drawback to this is the fact that when the user visits for the first time store2 the page loading will be at least double because of the redirects.