Skip to content

Instantly share code, notes, and snippets.

@bmodena
Last active October 24, 2024 21:05
Show Gist options
  • Save bmodena/a405144cf1f292ac8039796e1686bd13 to your computer and use it in GitHub Desktop.
Save bmodena/a405144cf1f292ac8039796e1686bd13 to your computer and use it in GitHub Desktop.
Shopify Clear Cart Cookie on logout
/*
@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);
});
@cjauvin
Copy link

cjauvin commented Oct 1, 2017

I'm new to Shopify programming.. would it be possible to explain in what context this code is actually necessary? I made some research about a possibly faulty logout behavior in my app (i.e. the cart does not get cleared at logout), and I stumbled upon this gist. Thanks!

@lsloss
Copy link

lsloss commented May 25, 2018

Thanks for this Brian. This stops personal information being stored in the cart after log out - something that is so important for customer privacy on shared computers!

@mikegazdag
Copy link

mikegazdag commented May 17, 2019

I came across this answer and found that this may be a more up to date interface using the Shopify AJAX API instead.

https://shopify.dev/api/ajax/reference/cart#post-cart-clear-js

@jeremypagley
Copy link

jeremypagley commented Nov 25, 2019

Just to clarify for anyone looking to mess with the Shopify cart. The answer @mikegazdag purposed does clear the cart line_items but does not clear the carts session from the browser cookies. To do that you can use the code below anywhere in your shopify theme .js code to clear the cart fully including the session.

For those wanting to clear the cart outside of shopify the way we have found with our site is that when we send the user to their order_status_url after creating an order with the Shopify api we call the code below inside the Shopify > Settings > Checkout > Additional Scripts.

<script>
function delete_cookie(name) {
  document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
delete_cookie('cart');
</script>

@carlosmirandadiaz
Copy link

Hi,

I try this code, but i don't get results. It's still functional in the last updates of Shopify?

Can you tell me more about how can i implement it?

Thank you

@bmodena
Copy link
Author

bmodena commented Aug 19, 2021

Hi,

I try this code, but i don't get results. It's still functional in the last updates of Shopify?

Can you tell me more about how can i implement it?

Thank you

I would look into - https://shopify.dev/api/ajax/reference/cart#post-cart-clear-js this method above was always a hack prior to this clear cart js ability being added

@QuantumB0x
Copy link

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!

@bmodena
Copy link
Author

bmodena commented Sep 15, 2021

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