i want to get cart.total_price and cart.count_item at header.liquid section.
But in my code, it’s only render 1 time and not change if i’m add new product to cart.
Below is my code.
<div class="announcement-bar">
<div class="announcement-bar__value">{{ cart.total_price | money }}</div>
</div>
Thanks
2
Answers
This is because the liquid is not dynamic. You need to use Shopify Cart API to manually update the cart or implement your own logic to listen to changes:
https://shopify.dev/api/ajax/reference/cart
It looks like the question you are asking was already answered here:
https://stackoverflow.com/a/23503789/6110804
Someone created a code solution to listen for Shopify cart events. Here you can find the JavaScript code, as well as the accompanying explainer article.
Basically, you have to spy on fetch and XMLHTTPRequest to see when it receives cart-related requests, and then you can run whatever code you like in response to these requests. It’s a little hacky, but it’s the best solution I could find.