使用Javascript会话购物车项目数量增减
我在这里看到过关于这个问题的类似问题,但在我的案例中,没有一个对我有用 我正在使用session处理购物车项目。我有数量的递增和递减按钮。问题是,当单击“增加和减少”按钮时,购物车项目的数量和价格不会像预期的那样发生变化。我的JavaScript不好,所以请原谅我,我真的尽力了。谢谢使用Javascript会话购物车项目数量增减,javascript,php,Javascript,Php,我在这里看到过关于这个问题的类似问题,但在我的案例中,没有一个对我有用 我正在使用session处理购物车项目。我有数量的递增和递减按钮。问题是,当单击“增加和减少”按钮时,购物车项目的数量和价格不会像预期的那样发生变化。我的JavaScript不好,所以请原谅我,我真的尽力了。谢谢 if (! empty($_GET["action"])) { switch ($_GET["action"]) { case "add": $productByCod
if (! empty($_GET["action"])) {
switch ($_GET["action"]) {
case "add":
$productByCode = $shoppingCart->getProductByCode($_GET["code"]);
$itemArray = array(
$productByCode[0]["menuCode"]=>array(
'menuName'=>$productByCode[0]["menuName"],
'menuCode'=>$productByCode[0]["menuCode"],
'quantity'=>$_POST["quantity"],
'menuPrice'=>$productByCode[0]["menuPrice"],
'restaurantId'=>$restaurant_id,
'restaurantLogo'=>$restaurant["restaurantLogo"]
)
);
if (! empty($_POST["quantity"])) {
if(!empty($_SESSION["cart_item"])) {
if(in_array($productByCode[0]["menuCode"],array_keys($_SESSION["cart_item"]))) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode[0]["menuCode"] == $k) {
if(empty($_SESSION["cart_item"][$k]["quantity"])) {
$_SESSION["cart_item"][$k]["quantity"] = 0;
}
$_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
}
}
} else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
}
break;
case "remove":
// Delete single entry from the cart
if(!empty($_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($_GET["code"] == $k)
unset($_SESSION["cart_item"][$k]);
if(empty($_SESSION["cart_item"]))
unset($_SESSION["cart_item"]);
}
}
break;
case "empty":
// Empty cart
unset($_SESSION["cart_item"]);
break;
}
}
function increment_quantity(menuCode, menuPrice) {
var inputQuantityElement = $("#input-quantity-"+menuCode);
var newQuantity = parseInt($(inputQuantityElement).val())+1;
var newPrice = newQuantity * menuPrice;
}
function decrement_quantity(menuCode, menuPrice) {
var inputQuantityElement = $("#input-quantity-"+menuCode);
if($(inputQuantityElement).val() > 1)
{
var newQuantity = parseInt($(inputQuantityElement).val()) - 1;
var newPrice = newQuantity * menuPrice;
}
}
<?php
if (! empty($_SESSION["cart_item"])) {
$item_quantity = 0;
$item_price = 0;
if (! empty($_SESSION["cart_item"])) {
foreach ($_SESSION["cart_item"] as $item) {
$item_quantity = $item_quantity + $item["quantity"];
$item_price = $item_price + ($item["menuPrice"] * $item["quantity"]);
}
}
}
?>
<div class="gold-members p-2 border-bottom">
<p class="text-gray mb-0 float-right ml-2" id="cart-price-<?php echo $item["menuCode"]; ?>">
<?php echo "₵". ($item["menuPrice"] * $item["quantity"]); ?>
</p>
<span class="count-number float-right">
<div class="btn btn-outline-secondary" onClick="decrement_quantity('<?php echo $item['menuCode']; ?>', '<?php echo $item['menuPrice']; ?>')">
-
</div>
<input class="count-number-input" id="input-quantity-<?php echo $item['menuCode']; ?>" value="<?php echo $item['quantity']; ?>" readonly="">
<div class="btn btn-outline-secondary" onClick="increment_quantity('<?php echo $item['menuCode']; ?>', '<?php echo $item['menuPrice']; ?>')">
+
</div>
</span>
<div class="media">
<div class="mr-2">
<a href="detail.php?action=remove&code=<?php echo $item["menuCode"]; ?>">
<img src="img/icon-delete.png" alt="icon-delete" data-toggle="tooltip" title="Remove from cart" />
</a>
</div>
<div class="media-body">
<p class="mt-1 mb-0 text-black"><small><?php echo $item["menuName"]; ?></small></p>
</div>
</div>
</div>
<?php
}
?>
</div>
<?php
}
?>
<?php
if (!empty($item_quantity)) {
echo '
<div class="mb-2 bg-white rounded p-2 clearfix">
<div class="input-group mb-0">
<div class="input-group-prepend">
<span class="input-group-text"><i class="icofont-comment"></i></span>
</div>
<textarea class="form-control" name="orderNote" placeholder="Any note? We will pass it on...eg no salad, little black pepper" aria-label="With textarea"></textarea>
</div>
</div>
';
}
?>
<div class="mb-2 bg-white rounded p-2 clearfix">
<?php
if ( !empty($item_quantity)) {
echo '
<img class="img-fluid float-left" src="img/wallet-icon.png">
<h6 class="font-weight-bold text-right mb-2">
<br>
Subtotal :
<span class="text-danger" id="total-price">
<span id="total-price">
₵ '.number_format($item_price, 2).'
</span>
</span>
</h6>
';
}else{
echo '
<p class="mb-4 text-white" id="total-quantity">
<div class="mb-2 bg-white rounded p-2 clearfix text-center">
<img class="img-fluid " src="empty-cart.png" >
<p class="font-weight-bold mb-2">
Add item to your cart
</p>
</div>
';
}
?>
<!-- <img class="img-fluid float-left" src="img/wallet-icon.png">
<h6 class="font-weight-bold text-right mb-2">
Subtotal :
<span class="text-danger" id="total-price">
<span id="total-price">
₵
<?php
if ( empty($item_price)) {
echo " ";
}else{
echo $item_price;
}
?>
</span>
</span>
</h6>
<p class="seven-color mb-1 text-right">Extra charges may apply</p>
<p class="text-black mb-0 text-right">You have saved $955 on the bill</p> -->
</div>
<?php
if ( !empty($item_quantity)) {
echo '
<a href="deliveryAddress.php" class="btn btn-success btn-block btn-lg">
Checkout <i class="icofont-long-arrow-right"></i>
</a>
';
}
?>
</div>