简体   繁体   中英

How to update cart quantity using ajax,jquery,php and sessions

I am facing a problem updating the quantity of cart items,click the + or - button to increase or decrease the quantity of an item, the quantity of every item is affected. Here is a screenshot of the page http://prntscr.com/7wc9ar

I have taken help from https://www.codeofaninja.com/2013/04/shopping-cart-in-php.html in order to make this cart

Here is my code

Cart.php

 <?php require_once("includes/initialize.php"); $action = isset($_GET['action']) ? $_GET['action'] : ""; $name = isset($_GET['name']) ? $_GET['name'] : ""; $qty = isset($_SESSION['qty']) ? $_SESSION['qty'] : 1; require_once("includes/header.php"); ?> <section id="cart_items"> <div class="container"> <div class="breadcrumbs"> <ol class="breadcrumb"> <li><a href="#">Home</a></li> <li class="active">Shopping Cart</li> </ol> </div> <?php if ($action == 'removed') { echo "<div class='alert alert-info'>"; echo "<strong>{$name}</strong> was removed from your cart!"; echo "</div>"; } else if ($action == 'quantity_updated') { echo "<div class='alert alert-info'>"; echo "<strong>{$name}</strong> quantity was updated! {$qty}"; echo "</div>"; } if (isset($_SESSION["cart_items"]) && count($_SESSION['cart_items']) > 0) { // get the product ids $ids = ""; foreach ($_SESSION['cart_items'] as $id => $value) { $ids = $ids . $id . ","; } // remove the last comma $ids = rtrim($ids, ','); ?> <div class="table-responsive cart_info"> <table class="table table-condensed"> <thead> <tr class="cart_menu"> <td class="image">Item</td> <td class="description"></td> <td class="price">Price</td> <td class="quantity">Quantity</td> <td class="total">Total</td> <td></td> </tr> </thead> <tbody> <?php $query = "SELECT * FROM products WHERE id IN ({$ids}) ORDER BY productName"; $result = $database->query($query); $total_price = 0; while ($row = $database->fetchArray($result)) { ?> <tr> <td class="cart_product"> <a href="#"><img src="images/products/<?php echo $row['productImageName'] ?>" height="100" width="100" alt=""></a> </td> <td class="cart_description"> <h4><a href="#"><?php echo $row["productName"] ?></a></h4> <p>Web ID: 1089772</p> </td> <td class="cart_price"> <?php $price = $row["productPrice"] ?> <p>PKR <?php echo $price ?></p> </td> <td class="cart_quantity"> <div class="cart_quantity_button"> <?php $id = $row["id"]; $name = $row["productName"]; ?> <a class="cart_quantity_down" href="<?php echo "decreaseQty.php?id={$id}&name={$name}&qty={$qty}"; ?>"> - </a> <!--<a class="cart_quantity_down" href=""> - </a>--> <input class="cart_quantity_input" type="text" name="quantity" value="<?php echo $_SESSION['qty']; ?>" autocomplete="off" size="2"> <a class="cart_quantity_up" href="<?php echo "increaseQty.php?id={$id}&name={$name}&qty={$qty}"; ?>"> + </a> <!--<a class="cart_quantity_up" href=""> + </a>--> </div> </td> <td class="cart_total"> <p class="cart_total_price">PKR <?php echo $price * $qty ?></p> </td> <td class="cart_delete"> <?php $id = $row["id"]; $name = $row["productName"]; echo "<a href='removeFromCart.php?id={$id}&name={$name}' class='cart_quantity_delete'><i class=\\"fa fa-times\\"></i></a>"; ?> <!--<a class="cart_quantity_delete" href="#"><i class="fa fa-times"></i></a>--> </td> </tr> <?php $total_price += $price * $qty; } ?> </tbody> </table> </div> <?php } else { echo "<div class='alert alert-danger'>"; echo "<strong>No products found</strong> in your cart!"; echo "</div>"; } ?> </div> </section> <!--/#cart_items--> <section id="do_action"> <div class="container"> <div class="heading"> <h3>What would you like to do next?</h3> <p>Choose if you have a discount code or reward points you want to use or would like to estimate your delivery cost.</p> </div> <div class="row"> <div class="col-sm-6"> <div class="chose_area"> <ul class="user_option"> <li> <input type="checkbox"> <label>Use Coupon Code</label> </li> <li> <input type="checkbox"> <label>Use Gift Voucher</label> </li> <li> <input type="checkbox"> <label>Estimate Shipping & Taxes</label> </li> </ul> <ul class="user_info"> <li class="single_field"> <label>Country:</label> <select> <option>United States</option> <option>Bangladesh</option> <option>UK</option> <option>India</option> <option>Pakistan</option> <option>Ucrane</option> <option>Canada</option> <option>Dubai</option> </select> </li> <li class="single_field"> <label>Region / State:</label> <select> <option>Select</option> <option>Dhaka</option> <option>London</option> <option>Dillih</option> <option>Lahore</option> <option>Alaska</option> <option>Canada</option> <option>Dubai</option> </select> </li> <li class="single_field zip-field"> <label>Zip Code:</label> <input type="text"> </li> </ul> <a class="btn btn-default update" href="">Get Quotes</a> <a class="btn btn-default check_out" href="">Continue</a> </div> </div> <div class="col-sm-6"> <div class="total_area"> <ul> <?php if(!isset($total_price)) { $total_price = 0; } ?> <li>Cart Sub Total <span>PKR <?php echo $total_price ?></span></li> <li>Eco Tax <span>NO Tax</span></li> <li>Shipping Cost <span>PKR 200</span></li> <?php if($total_price == 0) { $total_price = 0; } else { $total_price += 200; } ?> <li>Total <span>PKR <?php echo $total_price ?></span></li> </ul> <a class="btn btn-default update" href="">Update</a> <a class="btn btn-default check_out" href="">Check Out</a> </div> </div> </div> </div> </section><!--/#do_action--> <?php require_once("includes/footer.php"); ?> 

increaseQty.php

 <?php require_once("includes/initialize.php"); // get the product id $id = isset($_GET['id']) ? $_GET['id'] : ""; $name = isset($_GET['name']) ? $_GET['name'] : ""; $qty = isset($_GET['qty']) ? $_GET['qty'] : ""; $qty++; $_SESSION['qty'] = $qty; reDirectTo("cart.php?action=quantity_updated&id=" . $id . '&name=' . $name.'&qty=' . $qty); ?> 

decreaseQty.php

 <?php require_once("includes/initialize.php"); // get the product id $id = isset($_GET['id']) ? $_GET['id'] : ""; $name = isset($_GET['name']) ? $_GET['name'] : ""; $qty = isset($_GET['qty']) ? $_GET['qty'] : ""; $qty--; $_SESSION['qty'] = $qty; reDirectTo("cart.php?action=quantity_updated&id=" . $id . '&name=' . $name.'&qty=' . $qty); ?> 

Please help me to solve this quantity updating issue. If it cannot be handled through sessions then please guide me to a different solution.

In your code there is no quantity based on a particular product. The quantity is assigned to the session variable $_SESSION['qty'] . If you want to store the cart items based on individual product based. You should save that using an array of information.

For eg.

$_SESSION['cart'] = array(
  $productid => array(
           'product_name' => $productname,
           'productId' => $productid,
           'quantity' => $productquantity
  ),
);

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