I fixed this issue by disabling the persistent shoppingcart. Stores -> Configuration -> Customers -> Persistent Shopping Cart -> Enable Persistence
I found it by debugging this Observer: magento/module-persistent/Observer/CheckExpirePersistentQuoteObserver.php
. This observer is triggered by controller_action_predispatch
. In this file is are a few checks and if they pass they will expire the quote which will also remove mage-cache-sessid
cookie. This results in triggering this function:
/**
* Invalidate Cache By Close Cookie Session
*/
invalidateCacheByCloseCookieSession = function () {
if (!$.cookieStorage.isSet('mage-cache-sessid')) {
$.cookieStorage.set('mage-cache-sessid', true);
storage.removeAll();
}
};
in the magento/module-customer/view/frontend/web/js/customer-data.js
file. Here the storage.removeAll();
will empty the localstorage.
If you don't want to disable persistent shopping cart, you need take a look at the checks in the Observer: magento/module-persistent/Observer/CheckExpirePersistentQuoteObserver.php
.
if ($this->_persistentData->isEnabled() &&
!$this->_persistentSession->isPersistent() &&
!$this->_customerSession->isLoggedIn() &&
$this->_checkoutSession->getQuoteId() &&
!$observer->getControllerAction() instanceof \Magento\Checkout\Controller\Onepage
// persistent session does not expire on onepage checkout page to not spoil customer group id
) {
$instance = $instance;
$this->_eventManager->dispatch('persistent_session_expired');
$this->quoteManager->expire();
$this->_customerSession->setCustomerId(null)->setCustomerGroupId(null);
}
With !$observer->getControllerAction() instanceof \Magento\Checkout\Controller\Onepage
they try to prevent to lose the checkout data. But in my case I use custom modules like: PostNL which had their own controller actions in the checkout, so this check would pass and the localstorage was cleared.
PS. There is allready an upcoming fix: https://github.com/magento/magento2/commit/bcffc4161e4960713922506870e1a4b711c5ca9e#diff-ee3a4dc8a625074b5313aa7d915944fd where the check is based on the requestUri. A side note is that if you leave the checkout page the localstorage is also cleared.