简体   繁体   中英

How to update product quantity in session when same items are added to cart?

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?

  1. WhatsApp_add_cart function where I create session..
  2. Get data from the hidden field...
  3. WhatsApp_Cart_page where I display all session values using for each loop.
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'] ) && 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->is_type( 'simple' ) ){
        $product_id   = $product->get_id();
        $product_name = $product->get_title();

                // $p_Desp =$product->get_description();.
        $final_price = $product->get_price();
        $button_cart = get_option( sanitize_text_field( 'WhattsApp_BtnTxt_cart' ) );
        ?>
<form method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="product_id" value="<?php echo esc_html( $product_id ); ?>">
<input type="hidden" name="product_name" value="<?php echo esc_html( $product_name ); ?>">
<input type="hidden" name="product_price" value="<?php echo esc_html( $final_price ); ?>">
<?PHP

  if ( ! empty( $_SESSION['WhatsApp_cart'] ) ) {
            foreach ( $_SESSION['WhatsApp_cart'] as $key => $values ) {
   p_id=$values[0];
    if ( isset( $_GET['product_id'] ) ) {
  if($p_id==$_GET['product_id']){

  $_SESSION["WhatsApp_cart"][$key][$values[3]] + $_POST["quantity"];
    ?>                                                              
<input type="hidden" name="quantity" value="1" min="1" max="10" step="1" />

<?PHP
      } 
     ?>

        <?PHP
        } 
  ?>
  <?php
  } 
  ?>

     <input type="submit" name="WhatsApp_Cart_btn" style="background:#25d366; color: white;" value=" 
           <? 
       PHP 
          echo esc_html( $button_cart ); ?>" id="demo" onclick="Add_items();" />

        <?php wp_nonce_field( 'whatsapp_cart_field', 'whatsapp_cart_nonce_field' ); ?>
          </form>

            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');
            }
        ?>
<table class="" cellspacing="0">
    <thead>
        <tr>
            <th class="product_id"><?php esc_html_e( 'Remove product', 'Woo-WhatsApp' ); ?></th>
            <th class="product-name"><?php esc_html_e( 'Product', 'Woo-WhatsApp' ); ?></th>
            <th class="product-price"><?php esc_html_e( 'Price', 'Woo-WhatsApp' ); ?></th>
            <th class="product-quantity"><?php esc_html_e( 'Quantity', 'Woo-WhatsApp' ); ?></th>
            <th class="Total-price"><?php esc_html_e( 'Total', 'Woo-WhatsApp' ); ?></th>
        </tr>
    </thead>
    <?php
        global $wp;
        if ( ! empty( $_SESSION['WhatsApp_cart'] ) ) {
            foreach ( $_SESSION['WhatsApp_cart'] as $key => $values ) {
                print_r( $_SESSION['WhatsApp_cart']);
                    $current_url    = home_url( $wp->request ); 
                    $remove_url     = $current_url . '?index_to_remove=' . $key;
                ?>
                <tr>
                    <th ><form action="" method="post"><a href="<?php echo esc_url($remove_url); ?> " name="index_to_remove" >X</a></form>
                    </th>
                    <?php echo '<th>' . esc_html( $values[1] ) . '</th>'; ?>
                    <?php echo '<th>' . esc_html( $values[2] ) . '</th>'; ?>
                    <th><input type="number" name="quantity" value="<?php echo $values[3]?>" min="0" max="10" step="1"> </th>
                </tr>
                
    <?php }
    ?>
                </table>

 <?php } ?>
$flag = false;
            if ( ! empty( $_SESSION['WhatsApp_cart'] ) ) {
                foreach ( $_SESSION['WhatsApp_cart'] as $key => $values ) {
                    $p_id     = $values[0];
                    $quantity = $values[3];
                    if ( $p_id == $whatsapp_id ) {
                        $flag                               = true;
                        $whatsapp_upquantity                = $whatsapp_quantity + $quantity;
                        $total_uprice                       = $whatsapp_upquantity * $whatsapp_price;
                        $product_info                       = array( $whatsapp_id, $whatsapp_names, $whatsapp_price, $whatsapp_upquantity, $total_uprice, $whatsapp_desp );
                         $_SESSION['WhatsApp_cart'][ $key ] = $product_info;
                    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM