簡體   English   中英

使用ajax更新購物車項目數量僅在第一次左右有效

[英]Update cart item quantity with ajax only works first time around

單擊“添加到購物車”時,有1個項目被添加到購物車。 購物車中的每個項目都有一個加號和減號,因此用戶可以增加1或減少1。如果您單擊加號,它應該在單擊的特定項目上添加一個。 目前,這僅在首次單擊時有效,此后的點擊無效。 我不確定問題是jquery還是php。

$("body").on("click", ".cartPlus", function () {
        var itemToEdit = $(this).data('id');
        var qty = $(this).data('qty');
        var newQty = qty + 1;

        $.ajax({
                url: 'functions/show-cart.php',
                type: 'POST',
                dataType: 'json',
                data: {
                    itemToEdit: itemToEdit,
                    newQty: newQty
                },
                beforeSend: function () {
                    $(".price-xs").empty();
                },
            })
            .done(function (data) {
                $.each(data.cart, function (index, item) {
                    console.log(item.each_item);

                });
            })

        .fail(function (jqXHR, textStatus, errorThrown) {
            console.log(textStatus + ': ' + errorThrown);
            console.warn(jqXHR.responseText);
        });

    });


if(isset($_POST['itemToEdit']) && $_POST['itemToEdit'] != "") {

    $i = 0;
    $item_to_edit = $_POST['itemToEdit'];
    $quantity = $_POST['newQty'];

        foreach($_SESSION['cart_array'] as $each_item) {
            $i++;
            while(list($key, $value) = each($each_item)) {

                if($key == "item_id" && $value == $item_to_edit) {
                    array_splice($_SESSION['cart_array'], $i-1, 1, array(array("item_id" => $item_to_edit, "quantity" => $quantity)));

            }
        }
    }
}



if(!isset($_SESSION['cart_array'])) {

    $itemsInCart = 0;
    $response['total'] = 0;
    echo json_encode($response);

} else {

        $featured = "Yes";
        $i=0;
        foreach($_SESSION['cart_array'] as $each_item) {
            $item_id = $each_item['item_id'];

            $stmt = $link->prepare("SELECT `product_name`, `price`, `pic_name` FROM `products` as `p` INNER JOIN `product_images` as `pi` ON p.`id` = pi.`product_id` WHERE p.`id` = ? AND `featured` = ?");
            $stmt->bind_param("is", $item_id, $featured);
            $stmt->execute();
            $result = $stmt->get_result();
            $numRows = $result->num_rows;
            if($numRows > 0) {
                while($row = $result->fetch_assoc()) {
                    $product_name = sanitize($row['product_name']);
                    $price = sanitize(money_format('%.2n', $row['price']));
                    $subtotal = money_format('%.2n', $each_item['quantity'] * $price);
                    $pic_name = $row['pic_name'];
                    $cartTotal = $subtotal + $cartTotal;
                    $quantity = $each_item['quantity'];

                    $cart_details[] = array(

                    "product_name" => $product_name,
                    "price" => $price,
                    "subtotal" => $subtotal,
                    "pic_name" => $pic_name,
                    "each_item" => $quantity,
                    "item_id" =>$item_id,
                    "i" => $i

                    );

                    $i++;
                }
            }

            $stmt->close();
        }


    $response['total'] = $cartTotal;
    $response['cart'] = $cart_details;
    echo json_encode($response);
}

請按照以下示例更新數據量。

var num = $('#foo').data("num") + 1; 
console.log(num); 
$('#foo').data('num', num); 
console.log(num);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM