Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A drop-in HTML/JS script for Ecwid to force logout after an order is placed
<script type="text/javascript">
// A drop-in HTML/JS script for Ecwid to force logout after an order is placed
if (typeof(Ecwid) == 'object') {
Ecwid.OnPageLoaded.add(function(page) {
if ('ORDER_CONFIRMATION' == page.type) {
var localStoragePrefix = 'PSecwid__' + Ecwid.getOwnerId() + 'PS';
var isLoggedIn = (localStorage.getItem(localStoragePrefix + 'token') != null);
if (isLoggedIn) {
var itemsToRemove = ['token','customerid','profile'];
for (var i = 0; i < itemsToRemove.length; i++) {
localStorage.removeItem(localStoragePrefix + itemsToRemove[i]);
}
window.location.reload(true);
}
}
});
}
</script>
@ian-holden

This comment has been minimized.

Copy link

commented Aug 3, 2016

Hi,
Is there any way this code could also clear the cart?
I have a client running Ecwid in a shop kiosk.
I use this code to sign out and start a new session every time the home page is loaded, and need to clear the cart/basket too if possible.

EDIT - I figured out how to do this. Here is a modified version that also clears the cart regardless of the user being signed in:

<script type="text/javascript">
// A drop-in HTML/JS script for Ecwid to force logout after an order is placed
if (typeof(Ecwid) == 'object') {
  Ecwid.OnPageLoaded.add(function(page) {
    if ('ORDER_CONFIRMATION' == page.type) {
      var localStoragePrefix = 'PSecwid__' + Ecwid.getOwnerId() + 'PS';
      var reload=false;
      var cart = localStorage.getItem(localStoragePrefix + 'cart');
      if(cart != null){
        localStorage.removeItem(localStoragePrefix + 'cart');
        reload=true;
      }
      var isLoggedIn = (localStorage.getItem(localStoragePrefix + 'token') != null);
      if (isLoggedIn) {
        var itemsToRemove = ['token','customerid','profile'];
        for (var i = 0; i < itemsToRemove.length; i++) {
          localStorage.removeItem(localStoragePrefix + itemsToRemove[i]);
        }
        reload=true;
      }
      if(reload) window.location.reload(true);
    }
  });
}
</script>

Thanks,
Ian

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.