我的代码工作正常,我可以添加或删除产品,但是我想为产品添加选项(大小,颜色)。
如果产品选项不同,我该如何将它们添加为新产品
ajax请求;
/* Action on form submit */
$(".item_form").submit(function(e){
var form_data = $(this).serialize();
/* AJAX request */
$.ajax({
url: "cart.php",
type: "POST",
dataType:"json",
data: form_data
}).done(function(data){ //if AJAX request if succesfull .....
var totalItemInCart = $("#items_in_shopping_cart, #items_in_shopping_cart_mobile").html(data.items_in_cart); // get Json data
var new_item_qty = $("#items_in_shopping_cart, #items_in_shopping_cart_mobile").html(data.all_items); // get Json data
/* Empty the update info bar before calling it. Otherwise appended content will duplicate. */
$('#cart_update_info').empty();
/* append data/info to cart_update_info bar */
swal({
title: "Artikel wurde dem Warenkorb hinzugefügt",
timer: 1000,
showConfirmButton: false
});
/* If shopping cart is still open, items will appear on it at the same time of adding them */
if($(".shopping_cart_holder").css("display") == "block"){ // Check if shopping cart is open
$(".shopping_cart_info").trigger( "click" ); // update cart on event
}
})
e.preventDefault();
});
cart.php;
if(session_status()==PHP_SESSION_NONE){session_start();}
if(isset($ _ POST [“ item_id”])&& isset($ _ POST ['item_qty'])){
$added_item["item_id"]=$_POST['item_id'];
$added_item["item_qty"]=$_POST['item_qty'];
/* Fetch all items from database that matches $_POST['item_id']
Limit 1 at the end so only one item is fetched. */
$items = $database->find_by_query("SELECT * FROM shopping_items WHERE item_id='{$added_item["item_id"]}' LIMIT 1");
/* Foreach item -> Remaining data about items fetched from database */
foreach($items as $item){
$added_item["item_name"] = $item['item_name'];
$added_item["item_price"] = $item['item_price'];
/* Update item session array with newly added items - items that already exist in the basket will be overwritten */
$_SESSION["items"][$added_item['item_id']] = $added_item;
}
/* Calculate number of items in cart and output it in json format */
exit(json_encode(array('items_in_cart'=>count($_SESSION['items']))));
}
/ *添加项目数量-每个项目最多允许添加10个项目* /
if(isset($_GET["add_itm_qty"]) && isset($_SESSION["items"])){
if(isset($_SESSION['items'][$_GET["add_itm_qty"]])){
if($_SESSION['items'][$_GET["add_itm_qty"]]["item_qty"] <= 9){
$_SESSION['items'][$_GET["add_itm_qty"]]["item_qty"]+=1;
}else{
$_SESSION['items'][$_GET["add_itm_qty"]]["item_qty"]=10;
}
}
unset($_GET["add_itm_qty"]);
exit(json_encode(array('items_in_cart'=>count($_SESSION['items']),(array('all_items'=>$_SESSION["items"])))));}
/ *减去项目数量-仅在item_qty不小于0时减去数量* /
if(isset($_GET["subtruct_itm_qty"]) && isset($_SESSION["items"])){
if(isset($_SESSION['items'][$_GET["subtruct_itm_qty"]])){
if($_SESSION['items'][$_GET["subtruct_itm_qty"]]["item_qty"] >= 2){
$_SESSION['items'][$_GET["subtruct_itm_qty"]]["item_qty"]-=1;
}else{
$_SESSION['items'][$_GET["subtruct_itm_qty"]]["item_qty"]=1;
}
}
unset($_GET["subtruct_itm_qty"]);
/* Get item quantity and output it in json format */
exit(json_encode((array('item_qty'=>$_SESSION["items"]))));}
/ *从购物车中删除商品* /
if(isset($_GET["remove_item_from_cart"]) && isset($_SESSION["items"])){
$item_id = $_GET["remove_item_from_cart"];
/* Check if item is ina item session array */
if(isset($_SESSION["items"][$item_id])){
unset($_SESSION["items"][$item_id]); // Remove/unset item
}
/* Calculate total number of items in cart and output it in json format */
exit(json_encode(array('items_in_cart'=>count($_SESSION['items']))));}