php - 如何检查产品是否已经在购物车中?
问题描述
这是我的简单购物车代码,如果选中,我正在尝试检查会话购物车中是否已经存在产品。如果会话购物车为空,则添加产品时不会给出错误消息,但会给出错误消息:致命错误:如果在会话中选择相同产品或不同产品,则调用未定义函数 array_column()大车。请我需要帮助。
if(isset($_POST["addToCart"])){
if(isset($_SESSION["shopping_cart"])){
$item_array_id = array_column($_SESSION['shopping_cart'], "item_id");
if(!in_array($_POST["hidden_pid"], $item_array_id)){ //check if product is in cart
$count = count($_SESSION["shopping_cart"]);
$item_array = array(
'item_id' => $_POST["hidden_pid"],
'item_name' => $_POST["hidden_name"],
'item_price' => $_POST["hidden_price"],
'item_img' => $_POST["hidden_img"],
'item_quantity' => $_POST["hidden_quantity"]
);
$_SESSION["shopping_cart"][$count] = $item_array;
echo '<script>location.href="index.php"</script>';
}
else{
echo '<script>
alert("Item Already in Cart");
location.href="index.php";
</script>';
}
}else{
$item_array = array(
'item_id' => $_POST["hidden_pid"],
'item_name' => $_POST["hidden_name"],
'item_price' => $_POST["hidden_price"],
'item_img' => $_POST["hidden_img"],
'item_quantity' => $_POST["hidden_quantity"]
);
$_SESSION["shopping_cart"][0] = $item_array;
}
}
解决方案
推荐阅读
- python-3.x - 是否可以将训练迭代编码为可以使用 tf.vectorized_map 调用的张量流函数?
- postgresql - 运行多个 Airflow Scheduler 会导致 Postgres 锁定问题
- javascript - 填充 ObjecId 字段时,它的行为是否与普通嵌套文档一样?
- html - html中嵌入pdf的字体大小
- python - 安装 pyenv 后,我的 aws-adfs 得到了“ModuleNotFoundError”
- java - Java在列表中查找重复项并将关联值映射到另一个列表
- python-requests - 在嵌套 JSON 中为 Post 设置值
- kubernetes - Kubernetes Pod 通过负载均衡器而不是内部与其他 Pod 通信
- python - 使用 PyGithub 根据 URL 获取存储库的提交历史?
- bash - 用****屏蔽输出卡号