cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 cookies for multiple subdomains

Magento 2 cookies for multiple subdomains

Hello,

I'm using cookies in Magento 2 to handle a modal to display a store selector. I'm using multiples subdomains for each website handling store views.

In administration I did set cookie domain to ".my-domain.com", cookie path is "/"

My script looks like :

<script>
require([
        'jquery',
        'mage/cookies',
        'Magento_Ui/js/modal/modal',
        'domReady!'
],function($){
    var cookie = $.mage.cookies['defaults']['lifetime'];
    if(!$.mage.cookies.get("storeselectorpopup")){
                $.mage.cookies.set('storeselectorpopup', 'true');
                $('.popup-storeselector').modal({
                        autoOpen: true,
                        wrapperClass: 'modals-wrapper popup-storeselector',
                        responsive: true,
                });
    }
});
</script>


The problem is that user is asked twice when the subdomain changes, for example, landing page is www.my-domain.com, if the user selects Europe, he gets redirected to europe.my-domain.com, so the modal appears again.

 

I also tried to set cookie domain empty.

Any idea on what I missed here please ?

Cookie should be once for all subdomains..

1 REPLY

Re: Magento 2 cookies for multiple subdomains

Well, I don't know if it's the best solution, but I eended up storing a cookie without Magento, so it works fine.

 

<script>
require([
        'jquery',
        'Magento_Ui/js/modal/modal',
        'domReady!'
],function($){
	<?php if(!isset($_COOKIE['StoreSelector']) || $_COOKIE['StoreSelector'] != "storeselected") {
		$value = 'storeselected';
		setcookie("StoreSelector", $value, time()+7200, "/", ".my-domain.com", 1);
	?>
        $('.popup-storeselector').modal({
        	autoOpen: true,
        	wrapperClass: 'modals-wrapper popup-storeselector',
        	responsive: true,
         });
	<?php } ?>
});
</script>