I would like to delete cookies of the store, as we don’t want to store users’ checkout addresses for a long time.
How can we clear cookies on logout or any button click? and where will i get name,domain and path if i use javascript to delete it?
Thank for your help
Hello, @AnthonyWeiner
The code below from: https://stackoverflow.com/questions/19470517/delete-all-cookies-with-jquery-and-set-new/19470558
will do the trick. Just call
byecookies();
and your cookies will be erased.
function byecookies(){
var cookies = $.cookie();
for(var cookie in cookies) {
$.removeCookie(cookie);
}
}
@David_Weru can I set this on click of a button. if so what will be the script.
Thanks
Hello, @AnthonyWeiner
//somewhere in the head, place this function. (of your theme)
function byecookies(){
var cookies = $.cookie();
for(var cookie in cookies) {
$.removeCookie(cookie);
}
}
//somewhere in your page/section/button
//anything you put here including your button will trigger the "onclick" event which will call the function.
If you’d like further assistance, please let me know.
it works on console but not on live? what would be the reason
https://korresshop-greece.myshopify.com/
@AnthonyWeiner
You’ll need to wire up that function to the onclick listener of whichever button you already have in your theme.
The example above is the “bare bones” of how to achieve the cookie deleting functionality, you’d need to adapt it to your theme.
If you already did so, I’d assume that the jQuery isn’t loaded until you call the function, hence why it doesn’t work. In this case, for the sake of simplicity, I’d personally suggest a jQuery free alternative like this:
function deleteAllCookies() {
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}
Which you can then just execute it as deleteAllCookies(); and wire it up to whichever button you wish.
Kind regards,
Diego
Hello, @AnthonyWeiner
This is more direct. Put this line in whatever element you want to use as a button.
: onclick="var cookies = $.cookie();for(var cookie in cookies) { $.removeCookie(cookie);};location.reload();"
one click and all cookies are deleted. (also the page reloads)