Skip to content

Instantly share code, notes, and snippets.

@ihorduchenko
Created February 16, 2023 12:48
Show Gist options
  • Save ihorduchenko/ab8bde8db5648cb8cade122982ef57ee to your computer and use it in GitHub Desktop.
Save ihorduchenko/ab8bde8db5648cb8cade122982ef57ee to your computer and use it in GitHub Desktop.
Programmatically get cart object in Shopify
<script>
const cartRoute = '{{ routes.cart_url | append: '.js' }}';
function forceCartUpdate() {
var getCartForce = function() {
var url = ''.concat(cartRoute, '?t=').concat(Date.now());
return fetch(url, {
credentials: 'same-origin',
method: 'GET'
}).then(response => response.json());
};
getCartForce().then(function(cart) {
console.log(cart);
document.dispatchEvent(new CustomEvent('cart:updated', {
detail: {
cart: cart
}
}));
});
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment