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

我不知道如何通过再次添加来更新已经在购物篮中的产品数量。如果数量为 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']; 

        } 

    }    

  }   

}

?>

http://img.mukewang.com/644b8e820001f2c706560660.jpg

汪汪一只猫
浏览 118回答 1
1回答

茅侃侃

if ($_SERVER["REQUEST_METHOD"] == "POST") {&nbsp; if (isset($_POST['do_it']) && $_POST['do_it'] == 'change') {&nbsp; &nbsp; if (isset($_SESSION['basket'])) {&nbsp; &nbsp; &nbsp; $is_available = 0;&nbsp; &nbsp; &nbsp; &nbsp; foreach ($_SESSION['basket'] as $keys => $values) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($_SESSION['basket'][$keys]['product_id'] == $_POST['id']) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $is_available++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['basket'][$keys]['product_quantity'] +=&nbsp; $_POST['quantity'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; if ($is_available == 0) {&nbsp; &nbsp; &nbsp; if (!isset($_SESSION['basket'])) {&nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['basket'] = array();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if ($_SERVER['REQUEST_METHOD'] == 'POST') {&nbsp; &nbsp; &nbsp; &nbsp; if (isset($_POST['id'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['basket'][$_POST['id']] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'product_id'=>($_POST['id']),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'product_photo'=>($_POST['hidden_photo']),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'product_photo_alt'=>($_POST['hidden_photo_alt']),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'product_name'=>($_POST['hidden_name']),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'product_price'=>($_POST['hidden_price']),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'product_quantity'=>($_POST['quantity'])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; }&nbsp; &nbsp;}归功于https://www.webslesson.info/2018/04/shopping-cart-by-using-bootstrap-popover-with-ajax-php.html虽然,我不确定我是否理解$is_available代码中的实际作用以及添加相同产品时它是如何指代增加数量的。
打开App,查看更多内容
随时随地看视频慕课网APP