Sign out and/or clear cart
In some cases, perhaps running Ecwid in a kiosk, or public space, you may want to sign out any user who may have signed in, and you may also want to clear the cart of any products so that the next user of the system starts form a clean slate.
Script to sign out and clear cart on order confirmation
<script type="text/javascript">
// A drop-in HTML/JS script for Ecwid to force sign-out and clear the cart 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>
The code here is based on a script by Makfruit from Ecwid that is on Github here
Ecwid stores its state for a particular store in browser local storage using keys that contain the store ID. The code above reads the store ID form Ecwid, when the product browser widget loads the "ORDER_CONFIRMATION" page. It then checks if a user is signed in, and if, so, clears the local storage keys associated with that users account.
If you also want to clear the cart, you need to clear the local storage key 'PSecwid__9999999PScart' where 9999999 is the Ecwid store ID. We only clear the cart if it is not null, and then if any local storage has been cleared, we refresh the window.
Sign out and clear cart when a "reset" or "home" page is loaded
Script to sign out and clear cart on any page load
<script type="text/javascript">
// A drop-in HTML/JS script to force Ecwid sign-out and clear the cart when the page is loaded
var storeID = '9999999'; // place your store ID here
var localStoragePrefix = 'PSecwid__' + storeID + '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>
In some situations, you may want to clear the cart and sign out on a specific page, maybe the home page, so users starting a session from there have a clean account to start from.
In this case, you may not have the product browser widget on the screen to get the shop ID from. You could add the widget and hide it, then check for the "CATEGORY" page type instead of "ORDER_CONFIRMATION". But as you probably know what store ID is being used, you can simply hard coded it, and run the script without needing the product browser widget on the page and waiting for the Ecwid.OnPageLoaded event.