skip to Main Content

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


  1. Your code includes:

    jQuery.getJSON('/cart.js', function(cart) { 
      $('#line-total-{{ item.id  }}').html(Shopify.formatMoney(item.line_price).replace('$','£'))})
    

    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 named item 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:

    jQuery.getJSON('/cart.js', function(cart) { 
      for(var i=0; i<cart.items.length; i++){
        var item = cart.items[i];
        var price_element = $('#line-total-' + item.id)
        price_element.html(Shopify.formatMoney(item.line_price).replace('$','£'))})
      }
    })
    

    Hope this helps!

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

    jQuery.getJSON('/cart.js', function(cart) { cart.items.each($('#line-total-'+this.id).html(Shopify.formatMoney(this.line_price).replace('$','£')););});
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search