title: WooCommerce - auto update total price when quantity changed
body: <p>I've been searching for several days but I have no answer yet. Basically, I'm trying to replace woocommerce standart "update cart" button with ajax call, which automatically updates order total price when quantity changed. 
  This is my html</p>
  
  <pre><code>&lt;div class="cart_item"&gt;
      &lt;div class="product-thumbnail"&gt;
          &lt;a href="http://example.com"&gt;&lt;img width="90" height="90" src="path to thumbnail"/&gt;&lt;/a&gt;
      &lt;/div&gt;
  
      &lt;div class="product-name"&gt;
          &lt;a class="cart-page-product__title" href="http://example.com"&gt;Product1 name&lt;/a&gt;
      &lt;/div&gt;
  
      &lt;div class="product-quantity"&gt;
          &lt;div class="quantity"&gt;
              &lt;input type="number" step="1"   name="cart[some_security_key][qty]" value="1" class="input-text qty text" size="4"/&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="product-subtotal"&gt;&lt;span class="amount"&gt;2 000&lt;/span&gt;&lt;/div&gt;
          &lt;div class="product-remove"&gt;
              &lt;a class="product-remove_link" href="http://example.com/?remove_item"&gt;&amp;times;&lt;/a&gt;
          &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="cart_item"&gt;
      &lt;div class="product-thumbnail"&gt;
          &lt;a href="http://example.com"&gt;&lt;img width="90" height="90" src="path to thumbnail"/&gt;&lt;/a&gt;
      &lt;/div&gt;
  
      &lt;div class="product-name"&gt;
          &lt;a class="cart-page-product__title" href="http://example.com"&gt;Product2 name&lt;/a&gt;
      &lt;/div&gt;
  
      &lt;div class="product-quantity"&gt;
          &lt;div class="quantity"&gt;
              &lt;input type="number" step="1"   name="cart[some_security_key][qty]" value="1" class="input-text qty text" size="4"/&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="product-subtotal"&gt;&lt;span class="amount"&gt;2 000&lt;/span&gt;&lt;/div&gt;
          &lt;div class="product-remove"&gt;
              &lt;a class="product-remove_link" href="http://example.com/?remove_item"&gt;&amp;times;&lt;/a&gt;
          &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>In functions.php I have a function for updating totals:</p>
  
  <pre><code>public function update_total_price() {
          check_ajax_referer( 'update_total_price', 'security' );
  
          if ( ! defined('WOOCOMMERCE_CART') ) {
              define( 'WOOCOMMERCE_CART', true );
          }
  
          $cart_updated = false;
              $cart_totals  = isset( $_POST['cart'] ) ? $_POST['cart'] : '';
  
              if ( sizeof( WC()-&gt;cart-&gt;get_cart() ) &gt; 0 ) {
                  foreach ( WC()-&gt;cart-&gt;get_cart() as $cart_item_key =&gt; $values ) {
                      $_product = $values['data'];
  
                      // Skip product if no updated quantity was posted
                      if ( ! isset( $_POST['quantity'] ) ) {
                      // if ( ! isset( $cart_totals[ $cart_item_key ]['qty'] ) ) {
                          continue;
                      }
  
                      // Sanitize
                      $quantity = apply_filters( 'woocommerce_stock_amount_cart_item', apply_filters( 'woocommerce_stock_amount', preg_replace( "/[^0-9\.]/", '', filter_var($_POST['quantity'], FILTER_SANITIZE_NUMBER_INT)) ), $cart_item_key );
                      // $quantity = apply_filters( 'woocommerce_stock_amount_cart_item', apply_filters( 'woocommerce_stock_amount', preg_replace( "/[^0-9\.]/", '', $cart_totals[ $cart_item_key ]['qty'] ) ), $cart_item_key );
  
                      if ( '' === $quantity || $quantity == $values['quantity'] )
                          continue;
  
                      // Update cart validation
                      $passed_validation  = apply_filters( 'woocommerce_update_cart_validation', true, $cart_item_key, $values, $quantity );
  
                      // is_sold_individually
                      if ( $_product-&gt;is_sold_individually() &amp;&amp; $quantity &gt; 1 ) {
                          wc_add_notice( sprintf( __( 'You can only have 1 %s in your cart.', 'woocommerce' ), $_product-&gt;get_title() ), 'error' );
                          $passed_validation = false;
                      }
  
                      if ( $passed_validation ) {
                          WC()-&gt;cart-&gt;set_quantity( $cart_item_key, $quantity, false );
                      }
  
                      $cart_updated = true;
                  }
              }
  
              // Trigger action - let 3rd parties update the cart if they need to and update the $cart_updated variable
              $cart_updated = apply_filters( 'woocommerce_update_cart_action_cart_updated', $cart_updated );
  
              if ( $cart_updated ) {
                  // Recalc our totals
                  WC()-&gt;cart-&gt;calculate_totals();
                  woocommerce_cart_totals();
                  exit;
              }
  
      }
  </code></pre>
  
  <p>And Jquery code is:</p>
  
  <pre><code>jQuery( function( $ ) {
  
      // wc_cart_params is required to continue, ensure the object exists
      if ( typeof wc_cart_params === 'undefined' ) {
          return false;
      }
  
      // Cart price update depends on quantity
      //$( document ).on( 'click', '.quantity', function() {
  
  
      $( document ).on( 'change', '.quantity, input[type=number]', function() {
          var qty = $( this ).val();
          var currentVal  = parseFloat( qty);
  
          $( 'div.cart_totals' ).block({ message: null, overlayCSS: { background: '#fff url(' + wc_cart_params.ajax_loader_url + ') no-repeat center', backgroundSize: '16px 16px', opacity: 0.6 } });
  
          var data = {
              action: 'rf_update_total_price',
              security: rf_cart_params.rf_update_total_price_nonce,
              quantity: currentVal
          };
  
          $.post( rf_cart_params.ajax_url, data, function( response ) {
  
              $( 'div.cart_totals' ).replaceWith( response );
              $( 'body' ).trigger( 'rf_update_total_price' );
  
          });
          return false;
      });
  });
  </code></pre>
  
  <p>The above code works great if only one product is in the cart. 
  <img src="https://i.stack.imgur.com/sq6HS.jpg" alt="enter image description here"></p>
  
  <p>But when I add some other product and change quantity of one of them my function use last value of quantity for all of my products.</p>
  
  <p><img src="https://i.stack.imgur.com/jrxsB.jpg" alt="enter image description here"> </p>
  
  <p>For example, total price for the second image must be 7000(2000*1+2500*2) but it is 9000(2000*2+2500*2). 
  I am new to ajax and jquery so appreciate any help.</p>