如何仅将购物车仅限于同一卖家? (PHP)
我目前正在做一项功能,由于我的网站的有意限制和讲师的指示,我的“添加到购物车”功能仅允许同一卖家使用,我希望“添加到购物车”功能只允许用户/买家仅添加来自同一卖家的自行车。
如果他们来自不同的卖家,则会出现一个简单的警告框,通知他们无法从不同的卖家购买自行车,并且他们尝试添加的自行车将不会添加到购物车中。
<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>
我只需要一个解决方案,每次将自行车添加到购物车时都会进行检查,检查作为第一个自行车添加到购物车中的卖家是否不允许下一个自行车来自不同的卖家,除非用户/买家清除如果购物车带有“全部删除”按钮,则可以选择不同的卖家,但规则仍然适用,即只允许添加与添加到购物车的第一辆自行车相同的卖家。下面的代码位于购物车页面,添加的自行车将显示在此处。希望这足够清楚,请告诉我。
<?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>';
}
}
}
}
?>
I am currently doing a feature where my Add To Cart Feature is only allowed for the same Seller due to the intentional limitation of my website and instruction from my lecturer, where I want to have my add to cart feature only allows the User/Buyer to add bicycles only from the same Seller.
If they are from a different Seller, a simple alert box will appear to inform them that they cannot purchase bicycles from a different Seller and the bicycles that they try to add will not be added into the cart.
<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>
I just need a solution that will check every time a bicycle is added into the cart that checks whether the Seller that was added as the first one into the cart does not allow the next to be from a different Seller unless the User/Buyer clears the cart with a remove all button then a different Seller can be chosen but the rule still applies that only the Same Seller from the first bicycle that is added to the cart is allowed to be added. The code below is on the cart page where the bicycles that are added will be displayed here. Hope this is clear enough, please let me know.
<?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>';
}
}
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎您只需要一个条件 - &gt;
$ _ post [“ hidden_seller”] == $ _session [“ cart”] [0] ['shidend_seller']
seems like all you need is just a condition ->
$_POST["hidden_seller"] == $_SESSION["cart"][0]['hidden_seller']