首页 > 解决方案 > 如何检查产品是否已经在购物车中?

问题描述

这是我的简单购物车代码,如果选中,我正在尝试检查会话购物车中是否已经存在产品。如果会话购物车为空,则添加产品时不会给出错误消息,但会给出错误消息:致命错误:如果在会话中选择相同产品或不同产品,则调用未定义函数 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;
                }   
            }

标签: php

解决方案


推荐阅读