更新一种产品的购物车中的数量会重置其他产品的先前更新的数量

问题描述 投票:0回答:1

我设法通过下拉数量菜单和this.form.submit()来更改购物车中一种产品的数量,但是如果购物车中有两种产品的数量已更新,我决定再次更新其中一种产品的数量被覆盖,并恢复为以前的原始值(更新前),该值是从列出所有产品的页面上的“添加到购物篮”中发送的。

当我更新产品数量时,如何保留已经更新的其他产品数量?

<?php
// Add products to basket:

if (!isset($_SESSION['basket'])) {
  $_SESSION['basket'] = array();
}

if (isset($_POST['id'])) {
  $_SESSION['basket'][$_POST['id']] = array(
    'product_id'=>$_POST['id'],
    'product_photo'=>$_POST['hidden_photo'],
    'product_photo_alt'=>$_POST['hidden_photo_alt'],
    'product_name'=>$_POST['hidden_name'],
    'product_price'=>$_POST['hidden_price'],
    'product_quantity'=>$_POST['quantity'],
  );
}
?>

<?php
// 
 if(!empty($_SESSION['basket'])) {
   $total = 0;
   foreach ($_SESSION['basket'] as $key => $value) {
// Below is the code which updates the quantity of a product with a particular id
// But after submitting the form resetes the quantity of other products 
// With already updated quantity 
     if (!empty($_POST['new_qty'][$value['product_id']])) {
       $quantity = $_POST['new_qty'][$value['product_id']];
     } else {
       $quantity = $value['product_quantity'];
     }
?>

<form method="post" action="">
<select class="quantity-basket" name="new_qty[<?php echo $value['product_id']; ?>]" onchange="this.form.submit()">

<?php
// Quantity added to the basket:                        
if ($quantity) {
?>

<option value="<?php echo $quantity; ?>" hidden><?php echo $quantity; ?></option>

<?php
}
?>

<?php
// Drop-down quantity menu
for ($i = 0; $i <=50; $i++) {
?>

<option value="<?php echo ($i == 0) ? $i . ' (Remove)' : $i; ?>">

<?php echo ($i == 0) ? $i . ' (Remove)' : $i; ?>

</option>

<?php
}
?>

</select>
<!-- End of quantity drop-down menu -->
</form>

Shopping cart screenshot

php shopping-cart
1个回答
0
投票

由于本教程,我已经对其进行了整理:https://www.allphptricks.com/simple-shopping-cart-using-php-and-mysql/#comment-79810

<?php

if (!isset($_SESSION['basket'])) {
  $_SESSION['basket'] = array();
}

if (isset($_POST['id'])) {
  $_SESSION['basket'][$_POST['id']] = array(
    'product_id'=>$_POST['id'],
    'product_photo'=>$_POST['hidden_photo'],
    'product_photo_alt'=>$_POST['hidden_photo_alt'],
    'product_name'=>$_POST['hidden_name'],
    'product_price'=>$_POST['hidden_price'],
    'product_quantity'=>$_POST['quantity'],
  );
}

// Update quantity
if (isset($_POST['action']) && $_POST['action'] == 'update') {
  foreach($_SESSION['basket'] as &$value) {
    if($value['product_id'] === $_POST['type']) {
        $value['product_quantity'] = $_POST['new_qty'];
        break; // Stop the loop after we've found the product
    }
  }
}
?>

<?php
// 
 if(!empty($_SESSION['basket'])) {
   $total = 0;
   foreach ($_SESSION['basket'] as $product) {
?>

<form method="post" action="">
<input type="hidden" name="type" value='<?php echo $product["product_id"]; ?>'>
<input type="hidden" name="action" value="update">
<select class="quantity-basket" name="new_qty" onchange="this.form.submit()">
<option value="<?php echo $product['product_quantity']; ?>" hidden><?php echo $product['product_quantity']; ?></option>

<?php
// Drop-down quantity menu
for ($i = 0; $i <=50; $i++) {
?>

<option value="<?php echo ($i == 0) ? $i . ' (Remove)' : $i; ?>">

<?php echo ($i == 0) ? $i . ' (Remove)' : $i; ?>

</option>

<?php
}
?>

</select>
</form>

© www.soinside.com 2019 - 2024. All rights reserved.