如何将购物车限制为同一卖家? (PHP)

How Do I Limit A Cart Only To The Same Seller? (PHP)

我目前正在做一个功能,由于我的网站和讲师的指示有意限制,我的添加到购物车功能只允许同一卖家使用,我希望我的添加到购物车功能只允许User/Buyer 添加仅来自同一卖家的自行车。

如果他们来自不同的卖家,则会出现一个简单的警告框,通知他们他们不能从其他卖家那里购买自行车,并且他们尝试添加的自行车将不会添加到购物车中。

  <form method='POST' action='cart.php?action=addCart&BicycleID=<?php echo $row["BicycleID"]; ?>'>
                                         <p style="color:black;float:center;margin-top:5px;">Quantity: <input type='number' name='gadget_quantity' value='0' style="width:20%;color:black;" min="1" max="5"></p>
                                        
                                 <?php
                                
                                $colors=explode("," , $row['BicycleColor']);
                                $colors = array_diff($colors, array(""));
                                    
                                ?>
                                 
                                    <select name="visible_color" style="width:200px;margin-bottom:10px;">
                                     
                                    <?php
                                            foreach($colors as $color):
                                            $color = trim($color);
                                            echo '<option value="'.$color.'">'.$color.'</option>'; //close your tags!!
                                            endforeach;
                                    ?>
                                     
                                   </select>
                                        
                                        <button  name='addCart' class="btn btn-default add-to-cart" style="margin-top:2px;"><i class="fa fa-shopping-cart"></i>Add To Cart</button>
                                    
                                    <input type='hidden' name='hidden_id' value="<?php echo $row["BicycleID"];?>">    
                                    <input type='hidden' name='hidden_name' value="<?php echo $row["BicycleName"];?>">  
                                    <input type='hidden' name='hidden_price' value="<?php echo $row["BicyclePrice"];?>">
                                    <input type='hidden' name='hidden_image' value="<?php echo $row["BicycleImage"];?>">
                                    <input type='hidden' name='hidden_seller' value="<?php echo $row["AccountUsername"];?>">
                                    <input type='hidden' name='hidden_bank' value="<?php echo $row["SellerBank"];?>">    
 </form>

我只需要一个解决方案,每次将自行车添加到购物车时都会进行检查,以检查作为第一个添加到购物车中的卖家是否不允许下一个来自不同的卖家,除非User/Buyer 使用删除所有按钮清除购物车,然后可以选择不同的卖家,但规则仍然适用,即只允许添加来自添加到购物车的第一辆自行车的同一卖家。下面的代码位于购物车页面,添加的自行车将显示在此处。希望这足够清楚,请告诉我。

<?php 
include('dbconnect.php');

if (isset($_POST["addCart"])){
    if (isset($_SESSION["cart"][0])){
        $item_array_id = array_column($_SESSION["cart"],"BicycleID");
        if (!in_array($_GET["BicycleID"],$item_array_id)){
            $count = count($_SESSION["cart"]);
            $item_array = array(
                'BicycleID' => $_POST["hidden_id"],
                'BicycleName' => $_POST["hidden_name"],
                'BicyclePrice' => $_POST["hidden_price"],
                'BicycleImage' => $_POST["hidden_image"],
                'BicycleColor' => $_POST["visible_color"],
                'AccountUsername' => $_POST["hidden_seller"],
                'SellerBank' => $_POST["hidden_bank"],
                'gadget_quantity' => $_POST["gadget_quantity"],
            );
            $_SESSION["cart"][$count] = $item_array;
            echo '<script>window.location="cart.php"</script>';           
        
         }
        else{
            echo '<script>alert("Product is already Added to Cart")</script>';
            echo '<script>window.location="cart.php"</script>';
        }
    }
    else{
        $item_array = array(
            'BicycleID' => $_POST["hidden_id"],
            'BicycleName' => $_POST["hidden_name"],
            'BicyclePrice' => $_POST["hidden_price"],
            'BicycleImage' => $_POST["hidden_image"],
            'BicycleColor' => $_POST["visible_color"],
            'AccountUsername' => $_POST["hidden_seller"],
            'SellerBank' => $_POST["hidden_bank"],
            'gadget_quantity' => $_POST["gadget_quantity"],
        );
        $_SESSION["cart"][0] = $item_array;
    }
}

if (isset($_GET["action"])){
    if ($_GET["action"] == "empty"){
       unset($_SESSION["cart"]);
    }
    
}  
if (isset($_GET["action"])){
    if ($_GET["action"] == "delete"){
        foreach ($_SESSION["cart"] as $keys => $value){
            if ($value["BicycleID"] == $_GET["BicycleID"]){
                unset($_SESSION["cart"][$keys]);
                echo '<script>alert("Bicycle has been Removed")</script>';
                echo '<script>window.location="cart.php"</script>';
            }
        }
    }
    
} 

?>

似乎你只需要一个条件 -> $_POST["hidden_seller"] == $_SESSION["cart"][0]['hidden_seller']

<?php 
include('dbconnect.php');

if (isset($_POST["addCart"])){
    if (isset($_SESSION["cart"][0])){
        $item_array_id = array_column($_SESSION["cart"],"BicycleID");
        if (!in_array($_GET["BicycleID"],$item_array_id)){
            if($_POST["hidden_seller"] == $_SESSION["cart"][0]['hidden_seller']){

                $count = count($_SESSION["cart"]);
                $item_array = array(
                    'BicycleID' => $_POST["hidden_id"],
                    'BicycleName' => $_POST["hidden_name"],
                    'BicyclePrice' => $_POST["hidden_price"],
                    'BicycleImage' => $_POST["hidden_image"],
                    'BicycleColor' => $_POST["visible_color"],
                    'AccountUsername' => $_POST["hidden_seller"],
                    'SellerBank' => $_POST["hidden_bank"],
                    'gadget_quantity' => $_POST["gadget_quantity"],
                );
                $_SESSION["cart"][$count] = $item_array;
                echo '<script>window.location="cart.php"</script>';           
            }else{
                echo '<script>alert(" {message error } ")</script>';
                echo '<script>window.location="cart.php"</script>';

            }
         }
        else{
            echo '<script>alert("Product is already Added to Cart")</script>';
            echo '<script>window.location="cart.php"</script>';
        }
    }
    else{
        $item_array = array(
            'BicycleID' => $_POST["hidden_id"],
            'BicycleName' => $_POST["hidden_name"],
            'BicyclePrice' => $_POST["hidden_price"],
            'BicycleImage' => $_POST["hidden_image"],
            'BicycleColor' => $_POST["visible_color"],
            'AccountUsername' => $_POST["hidden_seller"],
            'SellerBank' => $_POST["hidden_bank"],
            'gadget_quantity' => $_POST["gadget_quantity"],
        );
        $_SESSION["cart"][0] = $item_array;
    }
}