<?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;
}
}
}
?>
あなたから各項目の数量を格納しますループを使用しているカート:
function getCartQty()
{
# If there is nothing in the cart, return 0
if(empty($_SESSION['shopping_cart']))
return 0;
# Store array
$qty = array();
# Loop items in cart
foreach($_SESSION["shopping_cart"] as $item){
# Store the quantity of each item
$qty[] = $item['product_quantity'];
}
# Return the sum
return array_sum($qty);
}
?>
<a href="#tab2">CART <span class="badge"><?php echo getCartQty() ?></span></a>
これはMagentoカートですか?私はそれがあなたのタグに入っていて、人々はMagentoの回答で回答していますが、あなたがMagentoのチュートリアルからこれを得ているのなら、私はMagentoカートを扱う従来の方法ではない新しいチュートリアルを探すかもしれません。 Magentoカートではない場合は、そのタグを削除してMagento固有の回答を得ないようにしてください。問題には関係ありません。 – Rasclatt