商品不会从购物车中删除

时间:2017-09-02 20:05:18

标签: php jquery ajax

我没有使用产品ID,而是从0开始购物车中的每件商品都有一个索引。购物车中的每件商品都有自己从购物车按钮删除/删除。单击该按钮时,该值将通过ajax发布到php脚本:

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

        $key_to_remove = $_POST['indexToRemove'];
        if(count($_SESSION['cart_array']) <=1) {
            unset($_SESSION['cart_array']);

        } else {

            unset($_SESSION['cart_array'][$key_to_remove]);

        }
    }

如果我点击console.log()indexToRemove,它会在控制台中显示正确的值。但是,该项目永远不会从会话中删除,并且控制台中没有错误可帮助您进行故障排除。

这是jQuery:

$("body").on("click", ".removeItem", function () {
        var indexToRemove = $(this).data('itr');
        var div = $(this).parents("div.hr");
        $.ajax({
            url: 'functions/show-cart.php',
            type: 'POST',
            dataType: 'json',
            data: {
                indexToRemove: indexToRemove
            },
            beforeSend: function () {
                $(div).html("<img src='images/spinner.gif'>");
                $("#total").empty();
            },
        })

        .done(function (data) {
            $(div).fadeOut();
            show_cart();
        })

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

添加到购物车代码:

$quantity = 1;
$product_id = $_POST['id'];
$colour = $_POST['colour'];
$size = $_POST['size'];


   $key = "{$product_id}.{$colour}.{$size}";
   if (empty($_SESSION['cart_array'][$key])) {
     $_SESSION['cart_array'][$key] = array(
        "item_id" => $product_id,
        "quantity" => $quantity,
        "colour" => $colour,
        "size" => $size,
     );
   }
   else {
      $_SESSION['cart_array'][$key]['quantity'] += $quantity;
   }

显示购物车项目的PHP代码:

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'];
            $colour = $each_item['colour'];
            $size = $each_item['size'];


        $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,
                    "colour" => $colour,
                    "size" => $size,


                    );

                    $i++;
                }
            }

            $stmt->close();
        }


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

1 个答案:

答案 0 :(得分:0)

在您的客户端,您需要更改此行:

data: {
     indexToRemove: indexToRemove
 },

为:

data: JSON.stringify({indexToRemove: indexToRemove}),

相反,在服务器端,第一个问题是:

unset($_SESSION['cart_array']["$key_to_remove"]);
                              ^              ^

将其更改为:

unset($_SESSION['cart_array'][$key_to_remove]);

第二个是(因为该项是您需要的json对象json_decode):

$key_to_remove = $_POST['indexToRemove'];

将其更改为:

$key_to_remove = json_decode($_POST['indexToRemove']);