title: Cart total price Override in WooCommerce
body: <p>I am running into issues with the Woocommerce cart total only displaying <strong>the price of the last item</strong> added to the cart. Individual items prices are not adding up to the total price correctly, it only show the price of the last item.</p>
  
  <p>Here is the code I'm using for overriding the price of the products added to the cart:</p>
  
  <pre><code>function action_woocommerce_before_cart_table() {
      global $woocommerce;
      $items = $woocommerce-&gt;cart-&gt;get_cart();
      foreach ($items as $item =&gt; $values) {
          $price = 0;
          if (array_key_exists('addons', $values) &amp;&amp; count($values['addons']) &gt; 0) {
              foreach ($values['addons'] as $value) {
                  $price = $price + $value['price'];
              }
          } else {
              $regular_price = get_post_meta($values['product_id'], '_regular_price', true);
              $sale_price = get_post_meta($values['product_id'], '_sale_price', true);
              $price = $regular_price;
          }
          $values['data']-&gt;set_price($price);
      }
  }
  
  add_action('woocommerce_before_cart_table', 'action_woocommerce_before_cart_table', 10, 0);
  
  function action_woocommerce_checkout_before_order_review() {
      foreach (WC()-&gt;cart-&gt;get_cart() as $item =&gt; $values) {
          $price = 0;
          $quantity = $values['quantity'];
          if (array_key_exists('addons', $values) &amp;&amp; count($values['addons']) &gt; 0) {
              foreach ($values['addons'] as $value) {
                  $price = $price + $value['price'];
              }
          } else {
              $regular_price = get_post_meta($values['product_id'], '_regular_price', true);
              $sale_price = get_post_meta($values['product_id'], '_sale_price', true);
              $price = $regular_price;
          }
  
  //        $final_price = $quantity * $price;
          $values['data']-&gt;set_price($price);
      }
  }
  
  add_action('woocommerce_review_order_before_cart_contents', 'action_woocommerce_checkout_before_order_review', 10, 0);
  
  function woocommerce_calculate_totals($cart) {
      $cart_sub_total = 0;
      global $woocommerce;
      $items = $woocommerce-&gt;cart-&gt;get_cart();
      foreach ($items as $item =&gt; $values) {
          $price = 0;
          $quantity = $values['quantity'];
          if (array_key_exists('addons', $values) &amp;&amp; count($values['addons']) &gt; 1) {
              foreach ($values['addons'] as $value) {
                  $price = $price + $value['price'];
              }
          } else {
              $regular_price = get_post_meta($values['product_id'], '_regular_price', true);
              $sale_price = get_post_meta($values['product_id'], '_sale_price', true);
              $price = $regular_price;
          }
          $final_price = $quantity * $price;
          $cart_sub_total = $final_price;
          $values['data']-&gt;set_price($cart_sub_total);
      }
      WC()-&gt;cart-&gt;subtotal = $cart_sub_total;
  
      $cart-&gt;sub_total = $cart_sub_total;
      $coupons = WC()-&gt;cart-&gt;get_coupons();
      $cupon_price = 0;
      if (!empty($coupons)) {
          foreach ($coupons as $code =&gt; $coupon) {
              $cupon_price = $cupon_price + $coupon-&gt;get_amount();
          }
      }
      $new_price = $cart_sub_total - $cupon_price;
      WC()-&gt;cart-&gt;total = $new_price;
      $cart-&gt;total = $new_price;
  }
  
  add_action('woocommerce_before_cart_totals', 'woocommerce_calculate_totals', 30);
  add_action('woocommerce_after_calculate_totals', 'woocommerce_calculate_totals', 30);
  </code></pre>
  
  <p>Appreciate any help.</p>