将相同产品添加到购物篮后更新数量

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

我不知道如何通过再次添加来更新已经在购物篮中的产品的数量。如果数量为1,则在添加数量为3的相同产品后,应在购物篮中显示4。目前,我所能做的就是要么用新的数量替换增加的数量,要么在购物篮表中插入一个空元素,而没有任何产品详细信息。

<?php
// Products are added to the basket
if (!isset($_SESSION['basket'])) {
  $_SESSION['basket'] = array();
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  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'])
    );
  }
}

// This is the code with which I can only insert an empty element in the basket table with no product details.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (isset($_POST['id']) && isset($_POST['quantity'])) {
    foreach ($_SESSION['basket'][$_POST['id']] as $item) {
        if ($_item['product_id'] === $_SESSION['basket']['product_id']) {
          $_SESSION['basket']['product_quantity'] += $item['product_quantity']; 
        } 
    }    
  }   
}

// If I apply below code it just replaces the quantity:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (isset($_POST['id']) && isset($_POST['quantity'])) {
    foreach ($_SESSION['basket'] as $item) {
        if ($_item['product_id'] === [$_POST['id']]) {
          $item['product_quantity'] += $_POST['quantity']; 
        } 
    }    
  }   
}
?>

Quantity replacedEmpty element inserted

[如果您需要更多详细信息,请告诉我。

UPDATE

我写了另一个代码,但是这次只是将发送到购物篮的数量增加了一倍。我还在列出产品的页面上添加了一个隐藏的输入字段。

列出产品以及将它们添加到购物篮的页面上的代码:

<form> // Submitted via Ajax
  <div>
    <input type="hidden" name="id" value="<?php echo $row['id']; ?>"/>
    <input type="hidden" name="do_it" value="quantity_again"/> // New input field added
    <select name="quantity">
                <?php
                // Drop-down select quantity menu 
                for ($i=1; $i<=$row['stock']; $i++) {
                ?>
      <option value="<?php echo $i; ?>"><?php echo $i; ?></option>

                <?php
                }   
                ?>
    </select>
  </div>
<input type="hidden" name="hidden_photo" value="<?php echo $row['img_front_url']; ?>"/>
<input type="hidden" name="hidden_photo_alt" value="<?php echo $row['img_front_alt'];?>"/>
<input type="hidden" name="hidden_name" value="<?php echo $row['product']; ?>"/>
<input type="hidden" name="hidden_price" value="<?php echo $row['price']; ?>"/>
        <button class="add-to-basket">Add to basket</button>
</form>

新代码只将购物篮中的数量加倍。

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (isset($_POST['do_it']) && $_POST['do_it'] == 'quantity_again') {
    foreach ($_SESSION['basket'] as &$item) {
        if ($item['product_id'] === $_POST['id']) {
          $item['product_quantity'] += $_POST['quantity'];
        } 
    }    
  }   
}

[$item['product_quantity']$_POST['quantity']似乎相同,所以这就是数量翻倍的原因。我以为我需要先检查$_POST['quantity']是否存在于数组$_SESSION['basket']中,如果存在,将其存储在变量中,然后再执行$item['product_quantity'] += $stored_qty,但这又会覆盖数量。这是代码:

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (isset($_POST['do_it']) && $_POST['do_it'] == 'quantity_again') {
    foreach ($_SESSION['basket'] as $item) {
      if (in_array($_POST['quantity'], $_SESSION['basket'])) {
        $stored_qty = $_POST['quantity']; 
          if ($item['product_id'] === $_POST['id']) {
            $item['product_quantity'] += $stored_qty;
          } 
      }  
    }
  }   
}

php shopping-cart product-quantity
1个回答
0
投票
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (isset($_POST['do_it']) && $_POST['do_it'] == 'change') {
    if (isset($_SESSION['basket'])) {
      $is_available = 0;
        foreach ($_SESSION['basket'] as $keys => $values) {
          if ($_SESSION['basket'][$keys]['product_id'] == $_POST['id']) {
            $is_available++;
            $_SESSION['basket'][$keys]['product_quantity'] +=  $_POST['quantity'];
          } 
        } 
    }

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

      if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        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'])
          );
        }
      }   
    }  
  }   
}

贷方转到https://www.webslesson.info/2018/04/shopping-cart-by-using-bootstrap-popover-with-ajax-php.html

尽管,但是我不确定我是否了解代码中的$is_available实际作用以及添加相同产品时它如何指增加数量。

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