Created
February 16, 2023 12:48
-
-
Save ihorduchenko/ab8bde8db5648cb8cade122982ef57ee to your computer and use it in GitHub Desktop.
Programmatically get cart object in Shopify
This file contains hidden or 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
<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