如何检查商品是否已经在购物车中?

时间:2020-05-20 09:16:42

标签: 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;
                }   
            }

0 个答案:

没有答案