Jump to content

[SOLVED] Multishop share cookie/session


Recommended Posts

Hello guys,

 

i have a question: i have 2 stores on the same domain:
1. domain.de/store1

2. domain.de/store2

 

In the frontend i have a dropdown where user can switch between those 2 shops.

 

Question: how can i make user loggedin in BOTH shops after sucess login? Right now i can login in one shop and see the store. When i change to "store2" it asks me to login.

 

Best regards,

Link to comment
Share on other sites

This is exactly the problem, that all my shops use the same domain! And the cookie has this "_shopUrl" in it. Is it possible to maintain cookies only on domain base without "_shopUrl"?

shop.de/berlin
shop.de/munchen
shop.de/stuttgart
Users are available to switch the shop to be able to order the goods from their local store.

Edited by kuskov (see edit history)
Link to comment
Share on other sites

Well, I have tried a little changes and apparently works ;), obviously need more tests, try this below...

Create a new override file in: /override/classes/Cookie.php
Paste this code:

<?php

class Cookie extends CookieCore
{
    public function __construct($name, $path = '', $expire = null, $shared_urls = null, $standalone = false)
    {
        parent::__construct('ps');
    }
}

Delete the file: /cache/class_index.php


Please let me know what happen, cheers.

  • Like 1
Link to comment
Share on other sites

  • 2 months later...
  • 3 months later...

Well, I have tried a little changes and apparently works ;), obviously need more tests, try this below...

 

Create a new override file in: /override/classes/Cookie.php

Paste this code:

<?php

class Cookie extends CookieCore
{
    public function __construct($name, $path = '', $expire = null, $shared_urls = null, $standalone = false)
    {
        parent::__construct('ps');
    }
}

Delete the file: /cache/class_index.php

 

 

Please let me know what happen, cheers.

I hope this finds you in time I have a little dilemma

 

How would I insert this if I have the agile module on my shops cookie.php? 

 

<?php
class Cookie extends CookieCore
{
/*
* module: agilemultipleseller
* date: 2015-04-29 19:31:26
* version: 3.0.6.2
*/
protected function _setcookie($cookie = null)
{
if(Module::isInstalled('agilemultipleshop'))
{
$_SESSION[$this->_name] = $this->_cipherTool->encrypt($cookie);
}
return parent::_setcookie($cookie);
}
 
/*
* module: agilemultipleseller
* date: 2015-04-29 19:31:26
* version: 3.0.6.2
*/
public function update($nullValues = false)
{
if(Module::isInstalled('agilemultipleshop'))
{
if(isset($_SESSION[$this->_name]))
{
$_COOKIE[$this->_name] = $_SESSION[$this->_name];
}
}
parent::update($nullValues);
}
 
}
Link to comment
Share on other sites

 

I hope this finds you in time I have a little dilemma

 

How would I insert this if I have the agile module on my shops cookie.php? 

 

<?php
class Cookie extends CookieCore
{
/*
* module: agilemultipleseller
* date: 2015-04-29 19:31:26
* version: 3.0.6.2
*/
protected function _setcookie($cookie = null)
{
if(Module::isInstalled('agilemultipleshop'))
{
$_SESSION[$this->_name] = $this->_cipherTool->encrypt($cookie);
}
return parent::_setcookie($cookie);
}
 
/*
* module: agilemultipleseller
* date: 2015-04-29 19:31:26
* version: 3.0.6.2
*/
public function update($nullValues = false)
{
if(Module::isInstalled('agilemultipleshop'))
{
if(isset($_SESSION[$this->_name]))
{
$_COOKIE[$this->_name] = $_SESSION[$this->_name];
}
}
parent::update($nullValues);
}
 
}

 

I think just need add the construct method there and should work...

public function __construct($name, $path = '', $expire = null, $shared_urls = null, $standalone = false)
    {
        parent::__construct('ps');
    }
  • Like 1
Link to comment
Share on other sites

 

I think just need add the construct method there and should work...

public function __construct($name, $path = '', $expire = null, $shared_urls = null, $standalone = false)
    {
        parent::__construct('ps');
    }

 

Sorry for my lack of knowledge. Where exactly would I insert that in the code?

Link to comment
Share on other sites

This should be the code merged with the other...

<?php
class Cookie extends CookieCore
{
public function __construct($name, $path = '', $expire = null, $shared_urls = null, $standalone = false)
{
 parent::__construct('ps');
}

/*
* module: agilemultipleseller
* date: 2015-04-29 19:31:26
* version: 3.0.6.2
*/
protected function _setcookie($cookie = null)
{
if(Module::isInstalled('agilemultipleshop'))
{
$_SESSION[$this->_name] = $this->_cipherTool->encrypt($cookie);
}
return parent::_setcookie($cookie);
}
 
/*
* module: agilemultipleseller
* date: 2015-04-29 19:31:26
* version: 3.0.6.2
*/
public function update($nullValues = false)
{
if(Module::isInstalled('agilemultipleshop'))
{
if(isset($_SESSION[$this->_name]))
{
$_COOKIE[$this->_name] = $_SESSION[$this->_name];
}
}
parent::update($nullValues);
}
 
}
  • Like 1
Link to comment
Share on other sites

  • 5 months later...
  • 7 months later...
  • 2 months later...

If you want to share just some cookie data between your multishops you should override CookieCore like this:
 

class Cookie extends CookieCore
{
    public function setName($name)
    {
        $this->_name = $name;
        $this->update();
    }
}

 
This funtion will set a global name for your cookie without any relation to context shop or group. You can use it like this:
 

$cookie = new Cookie('cookie_name');
$cookie->setName('cookie_name');
$cookie->__set('key', $value);

 
Good luck!!!

Edited by PrestaAlba (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...

But this solution don't works in differents domains, only works for the same domain with diferents paths (stores.com/st1, store.com/st2...). If you want the same cookies for diferents domains( store1.co, store2.com, store3.com,...) the solution is shared the cookies in the link to chenga the store.

 

My solution (in spanish, sorry):

 

Hola a todos, os explico mi solución que es sencilla y efectiva, a ver si os vale. Vamos a suponer que tenemos un prestashop con multitienda y cada una de ellas con un dominio (exampl1.com, example2.com,example3.com...),da igual el número de dominios/tiendas que tengais.

 

Paso 1) Codificamos las cookies del dominio actual (creamos una cadena) y se las pasamos al .tpl dónde tengamos los links de las diferentes tiendas. Para hacerlo, vamos al PHP que "genera" los datos para el .tpl (en mi caso es un simple módulo que genera un menu superior dónde estan los enlaces a las diferentes tiendas).

 

Así, en el PHP:

//config. Multicarrito

//codificamos la variable global $_COOKIE
$cookies_compartidas=htmlentities(base64_encode(serialize($_COOKIE)));

// Y la metemos en smarty para que el tpl la pueda leer
$this->smarty->assign('cookies_compartidas', $cookies_compartidas);

 

Seguidamente, vamos a editar el tpl que és el que muestra el menu. en Mi caso es una lista <UL><LI> con CSS para hacerlo bonito: La cuestión es que cuándo hagamos click sobre el enlace para ir a otra tienda/dominio, envíe esa cookie que hemos codificado al nuevo dominio al que queremos ir.

 

Modificamos el .tpl:

{* creamos un formulario "invisible" que enviará las cookies *}
<form action="foo" method="post" id="frmC000" STYLE='display:none;'>
<input type='hidden' id='shC000' name='shC000' value='{$cookies_compartidas}'>
</form>
<div class="menu_tiendas">
<ul>
{* links a las diferentes tiendas, la etiqueta Alt contiene la URL de la tienda, que lo ponemos como 'action' al formulario*}
<li>
<A alt="http://www.dominio1.com" onClick="$('#frmC000').attr('action',$(this).attr('alt'));$('#frmC000').submit();">Tienda 1</A>
</li>
<li>
<A alt="http://www.dominio2.com" onClick="$('#frmC000').attr('action',$(this).attr('alt'));$('#frmC000').submit();">Tienda 2</A>
</li>
<li>
<A alt="http://www.dominio3.com" onClick="$('#frmC000').attr('action',$(this).attr('alt'));$('#frmC000').submit();">Tienda 3</A>
</li>
</ul>

Ahora, con eso, cuándo hagámos click en cualquier enlace a nuestras tiendas, en vez de ir directamente al dominio, irá pero enviando la cookie.

 

Paso 2) leemos las cookies. Ahora, debemos leer esas cookies justo antes de actualizar nuestro carrito. Para ello, sobreescribiremos la función init del FrontController (recordad crear un nuevo FrontController.php en override/classes/controller para que con las actualizaciones continuen funcionando).

class FrontController extends FrontControllerCore
{
    public function init()
    {
// la variable $_POST ya debería ser global, pero por algún motivo, si no la declaro, no funciona
global $_POST;
// BY SAM, hemos recibido cookies??
if(isset($_POST['shC000'])){
//descodificamos las cookies
$cookies_import=ToolsCore::unSerialize(base64_decode($_POST['shC000']),true);
$cookie_lifetime = time() + 24 * 3600);
foreach($cookies_import as $nombre=>$valor)
{
//borramos la cooki anterior
setcookie($nombre,'',time()-3600*100,'/', $_SERVER['HTTP_HOST'],isset($_SERVER['HTTPS'])?true:false);
// establecemos la nueva cookie
setcookie($nombre,$valor,$cookie_lifetime,'/', $_SERVER['HTTP_HOST'],isset($_SERVER['HTTPS'])?true:false);

// si descomentamos, generará una linea en en log de errores para ver si se establecen
//error_log("Cookie establecida en ".$_SERVER['HTTP_HOST'].": ".$nombre."=".$valor,0);
}
} 
// lamamos al init de la clase FrontController original 
parent::init();
    }

Et voilà, magia hecha, rápido y funcional.

  • Like 4
Link to comment
Share on other sites

  • 2 weeks later...
  • 1 month later...
×
×
  • Create New...