title: Local Storage Update Cart Total
body: <p>I am looking to store just the cart subtotal in my local storage. I am using GTM to call the add to cart event and get the price that has been added to the cart. From there I want to just update that price or create a cart value that updates each time another item is added. Once I get the add function figured out I can easily fill in the gaps when something is removed or a qty is updated. </p>
  
  <pre><code>// this is the GTM call to track what the price is when the add to cart button is pushed
  var pr = {{DL - Add To Cart}};
  
  // Store product price in localStorage
  localStorage.setItem('price', pr.price);
  </code></pre>
  
  <p>The above I have no problem with. I get the price to show each time an item is added. Below is the code I am working with to update the price as each additional price is added. The main issue I am trying to work around is I have over complicated the cart to show in {[]} and I want it instead to display just as the above code ('price', pr.price) is showing so a simple number.</p>
  
  <pre><code>var addItem = function (price) {
      var oldItems = JSON.parse(localStorage.getItem('cartvalue')) || [] ;
      var match = oldItems.find(function (item) {
          return item['price'] = price;
      });
      if (match) {
          match['price'] += price;
      } else {
          var newItem = {
              'price': price,
          };
          oldItems.push(newItem);
      }
      localStorage.setItem('cartvalue', JSON.stringify(oldItems));
  };
  
   addItem(pr.price);
  </code></pre>