title: How to update product quantity in session when same items are added to cart?
body: <p>I am creating a cart using a session, but I don't know how to update product quantity when the same item is added to the cart session?</p>
  <ol>
  <li>WhatsApp_add_cart function where I create session..</li>
  <li>Get data from the hidden field...</li>
  <li>WhatsApp_Cart_page where I display all session values using for each loop.</li>
  </ol>
  <pre><code>public function WhatsApp_Cart_button() {
          $whatsapp_names    = '';
          $whatsapp_price    = '';
          $whatsapp_quantity = '';
          $total_price       = '';
          $Whatsapp_id='';
          if ( isset( $_POST['WhatsApp_Cart_btn'] ) ) {
  
              if ( isset( $_POST['whatsapp_cart_nonce_field'] ) &amp;&amp; wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['whatsapp_cart_nonce_field'] ) ), 'whatsapp_cart_field' ) ) {
  
                  echo '';
              }
  
              if ( isset( $_POST['product_name'] ) ) {
  
                      $whatsapp_names = sanitize_text_field( wp_unslash( $_POST ['product_name'] ) );
              }
              if ( isset( $_POST ['product_price'] ) ) {
  
                      $whatsapp_price = sanitize_text_field( wp_unslash( $_POST ['product_price'] ) );
              }
  
              if ( isset( $_POST ['quantity'] ) ) {
                  $whatsapp_quantity = sanitize_text_field( wp_unslash( $_POST ['quantity'] ) );
              }
                  if ( isset( $_POST ['product_id'] ) ) {
                  $Whatsapp_id = sanitize_text_field( wp_unslash( $_POST ['product_id'] ) );
              }
              $product_info = array( $Whatsapp_id,$whatsapp_names, $whatsapp_price, $whatsapp_quantity);
  
              $_SESSION['WhatsApp_cart'][] = $product_info;
  
          }
      }
  
      public function Whatsapp_cart_function() {
  
          global $product;
          if( $product-&gt;is_type( 'simple' ) ){
          $product_id   = $product-&gt;get_id();
          $product_name = $product-&gt;get_title();
  
                  // $p_Desp =$product-&gt;get_description();.
          $final_price = $product-&gt;get_price();
          $button_cart = get_option( sanitize_text_field( 'WhattsApp_BtnTxt_cart' ) );
          ?&gt;
  &lt;form method=&quot;post&quot; action=&quot;&quot; enctype=&quot;multipart/form-data&quot;&gt;
  &lt;input type=&quot;hidden&quot; name=&quot;product_id&quot; value=&quot;&lt;?php echo esc_html( $product_id ); ?&gt;&quot;&gt;
  &lt;input type=&quot;hidden&quot; name=&quot;product_name&quot; value=&quot;&lt;?php echo esc_html( $product_name ); ?&gt;&quot;&gt;
  &lt;input type=&quot;hidden&quot; name=&quot;product_price&quot; value=&quot;&lt;?php echo esc_html( $final_price ); ?&gt;&quot;&gt;
  &lt;?PHP
  
    if ( ! empty( $_SESSION['WhatsApp_cart'] ) ) {
              foreach ( $_SESSION['WhatsApp_cart'] as $key =&gt; $values ) {
     p_id=$values[0];
      if ( isset( $_GET['product_id'] ) ) {
    if($p_id==$_GET['product_id']){
  
    $_SESSION[&quot;WhatsApp_cart&quot;][$key][$values[3]] + $_POST[&quot;quantity&quot;];
      ?&gt;                                                              
  &lt;input type=&quot;hidden&quot; name=&quot;quantity&quot; value=&quot;1&quot; min=&quot;1&quot; max=&quot;10&quot; step=&quot;1&quot; /&gt;
  
  &lt;?PHP
        } 
       ?&gt;
  
          &lt;?PHP
          } 
    ?&gt;
    &lt;?php
    } 
    ?&gt;
  
       &lt;input type=&quot;submit&quot; name=&quot;WhatsApp_Cart_btn&quot; style=&quot;background:#25d366; color: white;&quot; value=&quot; 
             &lt;? 
         PHP 
            echo esc_html( $button_cart ); ?&gt;&quot; id=&quot;demo&quot; onclick=&quot;Add_items();&quot; /&gt;
  
          &lt;?php wp_nonce_field( 'whatsapp_cart_field', 'whatsapp_cart_nonce_field' ); ?&gt;
            &lt;/form&gt;
  
              public function WhatsApp_cart_page() {
  
              if ( isset( $_GET['index_to_remove'] ) ) {
                  unset( $_SESSION['WhatsApp_cart'][$_GET['index_to_remove']]);
                  wc_add_notice('Item removed from cart successfully' , 'success');
              }
          ?&gt;
  &lt;table class=&quot;&quot; cellspacing=&quot;0&quot;&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th class=&quot;product_id&quot;&gt;&lt;?php esc_html_e( 'Remove product', 'Woo-WhatsApp' ); ?&gt;&lt;/th&gt;
              &lt;th class=&quot;product-name&quot;&gt;&lt;?php esc_html_e( 'Product', 'Woo-WhatsApp' ); ?&gt;&lt;/th&gt;
              &lt;th class=&quot;product-price&quot;&gt;&lt;?php esc_html_e( 'Price', 'Woo-WhatsApp' ); ?&gt;&lt;/th&gt;
              &lt;th class=&quot;product-quantity&quot;&gt;&lt;?php esc_html_e( 'Quantity', 'Woo-WhatsApp' ); ?&gt;&lt;/th&gt;
              &lt;th class=&quot;Total-price&quot;&gt;&lt;?php esc_html_e( 'Total', 'Woo-WhatsApp' ); ?&gt;&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;?php
          global $wp;
          if ( ! empty( $_SESSION['WhatsApp_cart'] ) ) {
              foreach ( $_SESSION['WhatsApp_cart'] as $key =&gt; $values ) {
                  print_r( $_SESSION['WhatsApp_cart']);
                      $current_url    = home_url( $wp-&gt;request ); 
                      $remove_url     = $current_url . '?index_to_remove=' . $key;
                  ?&gt;
                  &lt;tr&gt;
                      &lt;th &gt;&lt;form action=&quot;&quot; method=&quot;post&quot;&gt;&lt;a href=&quot;&lt;?php echo esc_url($remove_url); ?&gt; &quot; name=&quot;index_to_remove&quot; &gt;X&lt;/a&gt;&lt;/form&gt;
                      &lt;/th&gt;
                      &lt;?php echo '&lt;th&gt;' . esc_html( $values[1] ) . '&lt;/th&gt;'; ?&gt;
                      &lt;?php echo '&lt;th&gt;' . esc_html( $values[2] ) . '&lt;/th&gt;'; ?&gt;
                      &lt;th&gt;&lt;input type=&quot;number&quot; name=&quot;quantity&quot; value=&quot;&lt;?php echo $values[3]?&gt;&quot; min=&quot;0&quot; max=&quot;10&quot; step=&quot;1&quot;&gt; &lt;/th&gt;
                  &lt;/tr&gt;
                  
      &lt;?php }
      ?&gt;
                  &lt;/table&gt;
  
   &lt;?php } ?&gt;
  </code></pre>