我一直在關注一個ecom網絡教程,我一直在更改代碼多次,但仍然沒有得到它的正確。問題是改變數量根據產品數量而不是產品ID旁邊的購物車旁的回聲。回聲推車與添加到購物車中的商品數量的關係
Shop.php代碼:
<a href="#tab2">CART <span class="badge">
<?php
if(isset($_SESSION["shopping_cart"])) {
echo count($_SESSION["shopping_cart"]);
} else {
echo '0';}
?>
</span></a>
action.php的代碼:
<?php
if (isset($_POST["product_id"])) {
$order_table = '';
$message = '';
if ($_POST["action"] == "add") {
if (isset($_SESSION["shopping_cart"])) {
$is_available = 0;
foreach ($_SESSION["shopping_cart"] as $keys => $values) {
if ($_SESSION["shopping_cart"][$keys]['product_id'] == $_POST["product_id"]) {
$is_available++;
$_SESSION["shopping_cart"][$keys]['product_quantity'] = $_SESSION["shopping_cart"][$keys]['product_quantity'] + $_POST["product_quantity"];
}
}
if ($is_available < 1) {
$item_array = array(
'product_id' => $_POST["product_id"],
'product_name' => $_POST["product_name"],
'product_price' => $_POST["product_price"],
'product_quantity' => $_POST["product_quantity"]
);
$_SESSION["shopping_cart"][] = $item_array;
}
} else {
$item_array = array(
'product_id' => $_POST["product_id"],
'product_name' => $_POST["product_name"],
'product_price' => $_POST["product_price"],
'product_quantity' => $_POST["product_quantity"]
);
$_SESSION["shopping_cart"][] = $item_array;
}
}
}
?>
這是一個Magento購物車嗎?我發現它出現在你的標籤中,人們正在回答Magento的答案,但是如果你是從Magento教程中獲得的,我可能會尋找一個新的教程,這不是處理Magento購物車的傳統方式。如果不是Magento購物車,請刪除該標籤,以免得到Magento特定的答案,它們將與您的問題無關。 – Rasclatt