亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將相同產品添加到購物車后更新數量

將相同產品添加到購物車后更新數量

PHP
汪汪一只貓 2023-04-28 17:14:08
我不知道如何通過再次添加來更新已經在購物籃中的產品數量。如果數量為 1,添加數量為 3 的相同產品后,應在購物籃中顯示 4。我現在所能做的就是用新的數量替換添加的數量,或者在購物籃表中插入一個空元素,不包含任何產品詳細信息。<?php// Products are added to the basketif (!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'];         }     }      }   }?>
查看完整描述

1 回答

?
茅侃侃

TA貢獻1842條經驗 獲得超21個贊

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代碼中的實際作用以及添加相同產品時它是如何指代增加數量的。


查看完整回答
反對 回復 2023-04-28
  • 1 回答
  • 0 關注
  • 132 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號