I can hide the checkout button when the cart’s total price is under $10 and show the reminder text. But…
When the cart total price is over $10, how does the cart drawer automatically Update without refresh page?
Here is my code:
{% if cart.total_price < 1000 %}
Over $10 to checkout!
{% else %}
<div class="button">
<a class="btn-checkout" href="/cart">checkout</a>
</div>
{% endif %}
2
Answers
Shopify is treating money in cents, so you should change it in order for everything to work:
The first thing to remember when working with a Shopify site is that Liquid is a templating language, not a scripting language. When a visitor requests a page on your store, Shopify’s servers interpret the Liquid commands to assemble a final document that is sent over the internet to the customer’s browser without any templating marks in it – the final result is just a normal page of HTML, Javascript or CSS.
Wrapping your checkout button with an if…else in Liquid will therefore work just fine for a static site, where any cart operation causes the page to reload – but cannot work for a dynamic site, as the page can only be accurate as of the moment when the server put it together.
For dynamic changes to a page that has already loaded, you’re going to need a scripting language – specifically, Javascript. To make the edit you want, you’re going to need to find the Javascript code that is responsible for rendering your theme’s drawer cart and make the code updates that can toggle the checkout button there. Unfortunately, as themes can (and do) go about this any way that the theme developers feel like at the time, there is no single One True Way that I can share with you to make this update without also seeing the relevant code.
If you’re lucky, you have a theme where the JS file you need is well laid out and easily found in the
assets
folder, often with a name liketheme.js
orshop.js
. If you’re having trouble finding the right file but have some familiarity with your browser’s developer tools, one method to track down the Javascript function that updates the cart that I have used many times before is:/cart.js
– this will have been invoked by whatever code your theme used to get the most current state of the cart so that the items, subtotal, etc. could be updated. (If you can’t find/cart.js
in the list, look for/cart/add.js
as the second-best option)gibberish, you probably landed in some helper library that sits
between the code you want to find and the actual request – go back to
the call stack and try the next function down until you find
something that looks like it’s supposed to be read by actual humans.
that relates to the cart update, such as
drawCart
,doCartUpdate
,onProductAdded
, etc.Once you have found the function in question, how you make the update to actually render the change that you want will depend heavily on how the theme is updating the side cart. For some themes, where all the Javascript files (and not just the helper libraries) are minified down to illegible names and scrunched onto a single line, you’re probably as far as you can go without really getting into the nuts and bolts of the code. Other themes might use a templating library for Javascript, such as Handlebars, to render the updates and so your change will be a much easier update to some template file hidden somewhere in your theme files. And of course your theme could be doing anything in between – but if you can at least find where and how your theme is generating the cart drawer you are most of the way to being able to customize it! If you manage to make it this far but the changes you need to make aren’t obvious to you, this would certainly warrant a follow-up question here.