I have found into on how to change your quantity of items via AJAX on the cart page, and I have also found how to update the cart total via AJAX.
What I have not found is how to update the total line item price for each line item as you increase or decrease the quantity.
Here is the code I have thus far (not working) …
jQuery.getJSON('/cart.js', function(cart) { $('#line-total-{{ item.id }}').html(Shopify.formatMoney(item.line_price).replace('$','£'))})
Any tips on what is wrong with my code?
Also this is within an onclick on a button if that helps.
2
Answers
Your code includes:
The above will not work, as the Liquid-based
{{ item.id }}
will get dropped once into the page, never to change afterwards. (Most likely, the above code was included in a place where there was no Liquid variable nameditem
in scope, so the rendered Javascript would have simply read$('#line-total-')
You’ll need to use the Javascript
cart
object that is being given to your function. For example:Hope this helps!
You’re getting “cart” back from /cart.js but you’re not reading any of its data.
You will need to read through the cart object (which will be represented as a shopify “Cart” object) in your javascript. I see you’re trying to get the “item.id” and “item.line_price”, but there is not relationship to the item and the cart object. You also cannot use liquid at this point as this will already be rendered to the client…
You likely need to do something similar to: