我有以下php代码,可将商品添加到基于会话的购物车中。例如,当使用大小不一样的商品时,它的效果很好。产品包括各种衣服。有些尺寸不同,有些则不同。一种产品可以是具有不同尺寸的T恤,另一种产品可以是仅提供一种尺寸的帽子。我希望能够将不同尺寸的T恤添加到购物车中。现在,它仅添加到基于会话的购物车(如果不存在)。如果存在,它将更新数量。
<?php
ini_set('display_errors', 'On');
/*include("session.php"); */
session_start();
if (isset($_POST["add_to_cart"]))
{
if (isset($_SESSION["shopping_cart"]))
{
$item_array_id = array_column($_SESSION["shopping_cart"],"item_id");
if (!in_array($_GET["id"], $item_array_id))
{
$count = count($_SESSION["shopping_cart"]);
$item_array = array(
'item_id' => $_GET["id"],
'item_name' => $_POST["hidden_name"],
'item_description' => $_POST["hidden_description"],
'item_price' => $_POST["hidden_price"],
'item_qty' => $_POST["hidden_qty"],
'itm_size' => $_POST["select-size-1"],
'itm_color' => $_POST["product-1-color"]
);
$_SESSION["shopping_cart"][$count] = $item_array;
}
else
{
// article exsits, update or remove
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
if ($values["item_id"] == $_GET["id"] and ($values["itm_size"] != $_GET["select-size-1"]))
{
// check it item id is the same but color differs, if so add to session array
$count = count($_SESSION["shopping_cart"]);
$item_array = array(
'item_id' => $_GET["id"],
'item_name' => $_POST["hidden_name"],
'item_description' => $_POST["hidden_description"],
'item_price' => $_POST["hidden_price"],
'item_qty' => $_POST["hidden_qty"],
'itm_size' => $_POST["select-size-1"],
'itm_color' => $_POST["product-1-color"]
);
$_SESSION["shopping_cart"][$count] = $item_array;
}
else
{
// Orginal kod
if ($values["item_id"] == $_GET["id"])
{
// Om man anger 0 som antal ska artikel tas bort
if ($_POST["hidden_qty"] == "")
{
unset($_SESSION["shopping_cart"][$keys]);
}
else
{
// update qty
$_SESSION['shopping_cart'][$keys]['item_qty'] += $_POST["hidden_qty"];
}
}
}
}
}
}
else
{
//echo '<script>alert("session is set")</script>';
$item_array = array(
'item_id' => $_GET["id"],
'item_name' => $_POST["hidden_name"],
'item_description' => $_POST["hidden_description"],
'item_price' => $_POST["hidden_price"],
'item_qty' => $_POST["hidden_qty"],
'itm_size' => $_POST["select-size-1"],
'itm_color' => $_POST["product-1-color"]
);
$_SESSION["shopping_cart"][0] = $item_array;
}
}
?>
因此,数量应根据ID的大小进行更新...
更新:我添加了我认为也有助于考虑处理尺寸的内容。它不起作用,如果我添加了XL号的T恤,它会按原样添加它,但是当我添加另一件XL号的T恤时,它不会更改数量,而是再增加2件相同大小的T恤?
正如04FS解释的那样,您不检查尺寸,仅检查产品ID。只需添加一个“否则”检查尺寸。您可以增强代码,但是有一个基于您的实际代码的示例:
// articel is present, update or remove
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
if ($values["item_id"] == $_REQUEST["id"])
{
// if qty = 0 remove item
if ($_POST["hidden_qty"] == "")
{
unset($_SESSION["shopping_cart"][$keys]);
}
else if ($values['itm_size'] != $_POST['select-size-1'])
{
// create new entry in the session shopping_cart
$_SESSION['shopping_cart'][] = array(
'item_id' => $_REQUEST["id"],
'item_name' => $_POST["hidden_name"],
'item_description' => $_POST["hidden_description"],
'item_price' => $_POST["hidden_price"],
'item_qty' => $_POST["hidden_qty"],
'itm_size' => $_POST["select-size-1"],
'itm_color' => $_POST["product-1-color"]
);
}
else
{
// update qty
$_SESSION['shopping_cart'][$keys]['item_qty'] += $_POST["hidden_qty"];
}
}
}
随时询问您是否需要帮助。