无法将相同的产品添加到购物车

时间:2018-03-06 07:25:21

标签: php jquery html5 session

我使用session和jquery创建了一个添加到购物车的代码。这几乎已经完成,但我遇到了两个问题。

1)当我刷新页面时,我收到错误

Notice: Undefined index: quantity in C:\xampp\htdocs\add-to-cart\ajax_cart.php on line 5

2)我可以将产品添加到购物车但如果我添加相同的产品两次,那么我收到错误。

 Notice: Undefined index: 250.00 in C:\xampp\htdocs\add-to-cart\ajax_cart.php on line 30
 Notice: Undefined offset: 1 in C:\xampp\htdocs\add-to-cart\ajax_cart.php on line 31

其他部分存在问题(可能是价格)。

else{
    $_SESSION['product_cart'][$product_id][$product_price] = $_SESSION['product_cart'][$product_id][$product_price] + ($product_price*$quantity);
    $_SESSION['product_cart'][$product_id][$quantity] = $_SESSION['product_cart'][$product_id][$quantity]+$quantity;
    }

AJAX函数调用ajax_cart.php

function add_cart(p_id=""){
    var quantity = $(".quantity"+p_id).val();
    $.ajax({
        type:"post",
        url:"ajax_cart.php",
        data:{action:'add',p_id:p_id,quantity:quantity},
        success:function(result){
            $('.cart_data').html(result);
        }
    });
}

添加到购物车

$action = $_POST['action'];
 $p_id   = trim($_POST['p_id']);
 $quantity = $_POST['quantity'];
if($action == 'add'){
     if(!empty($p_id)){
           $query = "SELECT product_id,product_title,product_price,product_image FROM product WHERE product_id=?";
            if ($stmt = $conn->prepare($query)) {
                $stmt->bind_param("i", $p_id);
                $stmt->execute();
                $stmt->bind_result($product_id, $product_title,$product_price, $product_image);
                $stmt->fetch();
              }

             $product = array(
                "p_id"=>$product_id,
                "title"=>$product_title,
                "price"=>$product_price*$quantity,
                "image"=>$product_image,
                "quantity"=>$quantity
             );
        if(isset($_SESSION['product_cart']) && !empty($_SESSION['product_cart']))
        {
            if(!array_key_exists($product_id,$_SESSION['product_cart']))
            {
                $_SESSION['product_cart'][$product_id] = $product;
            }
            else{
                $_SESSION['product_cart'][$product_id][$product_price] = $_SESSION['product_cart'][$product_id][$product_price] + ($product_price*$quantity);
                $_SESSION['product_cart'][$product_id][$quantity] = $_SESSION['product_cart'][$product_id][$quantity]+$quantity;
            }       
        }
        else{
          $_SESSION['product_cart'][$product_id] = $product;
        }
    }   
}

2 个答案:

答案 0 :(得分:0)

使用下面的其他部分

else{
            $_SESSION['product_cart'][$product_id]['price'] += ($product_price*$quantity);
            $_SESSION['product_cart'][$product_id]['quantity'] += $quantity;
        }   

并添加以下代码

if(!isset($_POST['action']) || !isset($_POST['quantity'])) return;

以上

$action = $_POST['action'];

答案 1 :(得分:0)

试试这个

if(!array_key_exists($product_id,$_SESSION['product_cart']))
{
    $_SESSION['product_cart'][$product_id] = $product;
}
else{
    $_SESSION['product_cart'][$product_id]['price'] += ($product_price*$quantity);
    $_SESSION['product_cart'][$product_id]['quantity'] += $quantity;
}