Last active
October 24, 2024 21:05
-
-
Save bmodena/a405144cf1f292ac8039796e1686bd13 to your computer and use it in GitHub Desktop.
Shopify Clear Cart Cookie on logout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
@require | |
Jquery: https://jquery.com/ | |
*/ | |
function delete_cookie(name) { | |
document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'; | |
} | |
// logout link | |
$('a[href^="/account/logout"]').on('click', function(event){ | |
event.preventDefault(); | |
var href = $(this).attr("href"), | |
target = ''; | |
delete_cookie('cart'); | |
// wait for data to push and then open link | |
setTimeout(function() { // now wait 150 milliseconds... | |
window.open(href,(!target?"_self":target)); // ...and open the link as usual | |
},150); | |
}); |
Hi Bmodena,
This is fantastic. Question, can you modify this to have a different trigger? For example, rather than having the user trigger the cart being cleared when logging out, can we use a time frame? Example logic statement: 15 days after the cart was created, the cart clears.
Thanks!
I think by default the Shopify cart expires in 14 days. As far as I know It is not possible to get the expiration date of a cookie through Javascript. So you would have to look into https://shopify.dev/api/ajax/reference/cart GET /cart.js endpoint and it looks like this has a few "created_at": "2019-04-10T20:49:00.148Z",
values in the response. If those are tied to cart creation time you could use that.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Bmodena,
This is fantastic. Question, can you modify this to have a different trigger? For example, rather than having the user trigger the cart being cleared when logging out, can we use a time frame? Example logic statement: 15 days after the cart was created, the cart clears.
Thanks!