将相同项目添加到具有不同选项的购物车
Add same item to shopping cart with different options
我正在开发 PHP 购物车,我有 3 种产品可供用户选择数量和黄油类型。商品正在添加到购物车中,其 ID 与数据库中的产品 ID 相同。
如何将相同的商品添加到带有不同黄油选项的购物车,以便我可以为添加的商品设置不同的 id/index,因为在结帐页面中,当我尝试删除一件商品时,多于一件由于 ID 问题,项目已被删除。我希望你能指导我这样做。这是我的购物车代码,
if(isset($_POST["add_to_cart"]))
{
if(isset($_SESSION["shopping_cart"]))
{
$item_array_id = array_column($_SESSION["shopping_cart"], "item_id");
$item_array_butter = array_column($_SESSION["shopping_cart"], "butter_type");
if(isset($_POST["butter_type"]))
{
$butter_type = $_POST["butter_type"];
}
else
{
$butter_type = '';
}
if(in_array($_GET["id"], $item_array_id) && in_array($butter_type, $item_array_butter))
{
//increment qty by 1
$_SESSION["shopping_cart"][0]['item_quantity']++;
}
else if(in_array($_GET["id"], $item_array_id) && !in_array($butter_type, $item_array_butter))
{
$item = array("item_id"=>$_GET["id"],
"item_name"=>$_POST["hidden_name"],
"item_price"=> $_POST["hidden_price"],
"subtotal_price"=>$_POST["hidden_price"]*$_POST["quantity"],
"item_quantity"=>$_POST["quantity"],
"butter_type"=>$butter_type
);
array_push($_SESSION["shopping_cart"],$item); // Items added to cart
}
else
{
$item = array("item_id"=>$_GET["id"],
"item_name"=>$_POST["hidden_name"],
"item_price"=> $_POST["hidden_price"],
"subtotal_price"=>$_POST["hidden_price"]*$_POST["quantity"],
"item_quantity"=>$_POST["quantity"],
"butter_type"=>$butter_type
);
array_push($_SESSION["shopping_cart"],$item); // Items added to cart
}
}
else
{
$_SESSION["shopping_cart"] = array();
if(isset($_POST["butter_type"]))
{
$butter_type = $_POST["butter_type"];
}
else
{
$butter_type = '';
}
$item = array("item_id"=>$_GET["id"],
"item_name"=>$_POST["hidden_name"],
"item_price"=> $_POST["hidden_price"],
"subtotal_price"=>$_POST["hidden_price"]*$_POST["quantity"],
"item_quantity"=>$_POST["quantity"],
"butter_type"=>$butter_type
);
array_push($_SESSION["shopping_cart"],$item); // Items added to cart
}
}
这是要添加到购物车的 html 表格:
<form method="post" action="index.php?action=add&id=<?php echo $mainrow["id"]; ?>">
<input type="submit" name="add_to_cart" id="add_to_cart" value="Add to Cart" />
</form
一个简单的解决方案是在表单中同时传递“id”和“butter_type”值。
<form method="post" action="index.php?action=add&id=<?php echo $mainrow["id"]; ?>&butter_type=<?php echo $butterType ?>">
然后,在后端,当您存储项目时,不是存储项目的“id”,而是创建一个组合键并将其作为“id”。
$item = array("item_id"=> $_GET["id"].'_'.$_GET['butter_type']...
这样,一个项目由项目的“id”以及它的“butter_type”
定义
当您从购物车中删除商品时,现在您应该删除具有上述组合键的会话商品,而不是“id”。
然后结帐时,您可以使用 explode 命令获取商品 ID 以及黄油类型。
我通过将 'product_id' 变量添加到购物车数组解决了我的问题,并且我使用会话变量计数器作为值,因此购物车中的所有商品都有一个唯一的 ID。并设法无误地更新和删除项目。谢谢你的帮助
我正在开发 PHP 购物车,我有 3 种产品可供用户选择数量和黄油类型。商品正在添加到购物车中,其 ID 与数据库中的产品 ID 相同。
如何将相同的商品添加到带有不同黄油选项的购物车,以便我可以为添加的商品设置不同的 id/index,因为在结帐页面中,当我尝试删除一件商品时,多于一件由于 ID 问题,项目已被删除。我希望你能指导我这样做。这是我的购物车代码,
if(isset($_POST["add_to_cart"]))
{
if(isset($_SESSION["shopping_cart"]))
{
$item_array_id = array_column($_SESSION["shopping_cart"], "item_id");
$item_array_butter = array_column($_SESSION["shopping_cart"], "butter_type");
if(isset($_POST["butter_type"]))
{
$butter_type = $_POST["butter_type"];
}
else
{
$butter_type = '';
}
if(in_array($_GET["id"], $item_array_id) && in_array($butter_type, $item_array_butter))
{
//increment qty by 1
$_SESSION["shopping_cart"][0]['item_quantity']++;
}
else if(in_array($_GET["id"], $item_array_id) && !in_array($butter_type, $item_array_butter))
{
$item = array("item_id"=>$_GET["id"],
"item_name"=>$_POST["hidden_name"],
"item_price"=> $_POST["hidden_price"],
"subtotal_price"=>$_POST["hidden_price"]*$_POST["quantity"],
"item_quantity"=>$_POST["quantity"],
"butter_type"=>$butter_type
);
array_push($_SESSION["shopping_cart"],$item); // Items added to cart
}
else
{
$item = array("item_id"=>$_GET["id"],
"item_name"=>$_POST["hidden_name"],
"item_price"=> $_POST["hidden_price"],
"subtotal_price"=>$_POST["hidden_price"]*$_POST["quantity"],
"item_quantity"=>$_POST["quantity"],
"butter_type"=>$butter_type
);
array_push($_SESSION["shopping_cart"],$item); // Items added to cart
}
}
else
{
$_SESSION["shopping_cart"] = array();
if(isset($_POST["butter_type"]))
{
$butter_type = $_POST["butter_type"];
}
else
{
$butter_type = '';
}
$item = array("item_id"=>$_GET["id"],
"item_name"=>$_POST["hidden_name"],
"item_price"=> $_POST["hidden_price"],
"subtotal_price"=>$_POST["hidden_price"]*$_POST["quantity"],
"item_quantity"=>$_POST["quantity"],
"butter_type"=>$butter_type
);
array_push($_SESSION["shopping_cart"],$item); // Items added to cart
}
}
这是要添加到购物车的 html 表格:
<form method="post" action="index.php?action=add&id=<?php echo $mainrow["id"]; ?>">
<input type="submit" name="add_to_cart" id="add_to_cart" value="Add to Cart" />
</form
一个简单的解决方案是在表单中同时传递“id”和“butter_type”值。
<form method="post" action="index.php?action=add&id=<?php echo $mainrow["id"]; ?>&butter_type=<?php echo $butterType ?>">
然后,在后端,当您存储项目时,不是存储项目的“id”,而是创建一个组合键并将其作为“id”。
$item = array("item_id"=> $_GET["id"].'_'.$_GET['butter_type']...
这样,一个项目由项目的“id”以及它的“butter_type”
定义当您从购物车中删除商品时,现在您应该删除具有上述组合键的会话商品,而不是“id”。
然后结帐时,您可以使用 explode 命令获取商品 ID 以及黄油类型。
我通过将 'product_id' 变量添加到购物车数组解决了我的问题,并且我使用会话变量计数器作为值,因此购物车中的所有商品都有一个唯一的 ID。并设法无误地更新和删除项目。谢谢你的帮助