skip to Main Content

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


  1. 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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search