Opencart cart across multiple stores with different subdomains

可紊 提交于 2019-12-05 08:14:04

问题


Hi have a single opencart install setup with several stores with different subdomains (all under the same domain). I want customers to be able to put items in the cart on one site, then move onto the next and put in more or even subtract, till eventually a customer checkouts out on any store. Note products might appear in one store but not another.

I notice opencart does this somewhat. ie it will bring products already in the cart to the next store but only if the products appear in both stores. Further if a customer then deletes one of the items and moves back to the same store, they product reappears.

First Problem seems to firstly be products in the cart are being displayed through what i guess is a query that selects products by store_id. I have had a hard look to see if i can find anything but am at a loss.

Second problem seems to be with the contents of the session. I am still learning php and am a bit confused of how to even attempt to modify how the session works.

Can anyone please provide some guidance on how i can go about fixing/changing this.


回答1:


OpenCart stores all these information in you PHP session. Since your stores are located under different subdomains, the PHP session changes when you switch from one store to another.

So the first thing you need to do is to share the session between all subdomains. By default, PHP uses the 'PHPSESSID' cookie to propagate session data across multiple pages, and by default it uses the current top-level domain and subdomain in the cookie declaration.

Example: www.domain.com

The downside to this is that the session data can't travel with you to other subdomains. So if you started a session on www.domain.com, the session data would become unavailable on forums.domain.com. The solution is to change the domain PHP uses when it sets the 'PHPSESSID' cookie.

Assuming you have an init file that you include at the top of every PHP page, you can use the ini_set() function. Just add this to the top of your init page:

ini_set('session.cookie_domain',
substr($_SERVER['SERVER_NAME'],strpos($_SERVER['SERVER_NAME'],"."),100));

This line of code takes the domain and lops off the subdomain.

Example: forums.domain.com -> .domain.com

Now, every time PHP sets the 'PHPSESSID' cookie, the cookie will be available to all subdomains!

You might also need to make some little modifications to the OpenCart's core in order to make it work.

Have fun :)




回答2:


After Tohids help I have the following solution, hopefully it helps others. I added the cookie_domain code line to the session.php file and also added or changed the cookie name wherever the setcookie function was used to cover the currency and language cookies.

open \system\session.php

find;

ini_set('session.use_cookies', 'On');
ini_set('session.use_trans_sid', 'Off');

insert after;

ini_set('session.cookie_domain', substr($_SERVER['SERVER_NAME'],strpos($_SERVER['SERVER_NAME'],"."),100));

open \index.php

find;

if (!isset($request->cookie['language']) || $request->cookie['language'] != $code) {      
  setcookie('language', $code, time() + 60 * 60 * 24 * 30, '/', $request->server['HTTP_HOST']);
}

replace with;

if (!isset($request->cookie['language']) || $request->cookie['language'] != $code) {      
  setcookie('language', $code, time() + 60 * 60 * 24 * 30, '/', substr($_SERVER['SERVER_NAME'],strpos($_SERVER['SERVER_NAME'],"."),100));
}           

find;

if (isset($request->get['tracking']) && !isset($request->cookie['tracking'])) {
    setcookie('tracking', $request->get['tracking'], time() + 3600 * 24 * 1000, '/');
}

replace with;

if (isset($request->get['tracking']) && !isset($request->cookie['tracking'])) {
  setcookie('tracking', $request->get['tracking'], time() + 3600 * 24 * 1000, '/', substr($_SERVER['SERVER_NAME'],strpos($_SERVER['SERVER_NAME'],"."),100));
}

open system\currency.php

find;

if (!isset($this->request->cookie['currency']) || ($this->request->cookie['currency'] != $currency)) {
  setcookie('currency', $currency, time() + 60 * 60 * 24 * 30, '/', $this->request->server['HTTP_HOST']);
}

replace with;

if (!isset($this->request->cookie['currency']) || ($this->request->cookie['currency'] != $currency)) {
  setcookie('currency', $currency, time() + 60 * 60 * 24 * 30, '/', substr($_SERVER['SERVER_NAME'],strpos($_SERVER['SERVER_NAME'],"."),100));
}



回答3:


Very Easy Solution! Share the login session across the subdomains

  1. OPEN FILE: system/library/session.php
  2. FIND LINE: session_set_cookie_params(0, '/');
  3. APPEND : session_set_cookie_params(0, '/','.DOMAIN.COM);

Make sure to include the period "." before DOMAIN.COM

That's it... Now login sessions started on www.domain.com is shared with www.sub.domain.com



来源:https://stackoverflow.com/questions/12066759/opencart-cart-across-multiple-stores-with-different-subdomains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!