📌 const cart = {
list: {}
}
list 裡面會所選的商品資料
📌 init: function (ID, Name, Price, Amount,Max_Amount, overwrite = true)
{
if (overwrite) this.list[ID] = {"Name": Name, "Price": Price, "Amount": Amount,"Max_Amount":Max_Amount};
}
如果這個商品
在清單不成出現過
新增在清單
如果有出現過
清單裡的商品數量改變
📌 del: function (ID)
{
delete this.list[ID];
}
刪除清單中的指定編號
📌 total: () =>
{
let price = 0;
Object.entries(shop.list).forEach(([key, value]) =>
{
price += (parseInt(value["Price"]) * parseInt(value["Amount"]));
});
return price;
}
數量 * 價格 = 所選商品須付的總金額
📌 const shop = {
list: {},
add: function (ID, Name, Price, Amount,Max_Amount)
{
if (this.list.hasOwnProperty(ID))
{
this.list[ID]["Amount"] = parseInt(this.list[ID]["Amount"]) + parseInt(Amount);
}
else
{
shop.init(ID, Name, Price, Amount,Max_Amount);
}
},
init: function (ID, Name, Price, Amount,Max_Amount, overwrite = true)
{
if (overwrite) this.list[ID] = {"Name": Name, "Price": Price, "Amount": Amount,"Max_Amount":Max_Amount};
},
del: function (ID)
{
delete this.list[ID];
},
total: () =>
{
let price = 0;
Object.entries(shop.list).forEach(([key, value]) =>
{
price += (parseInt(value["Price"]) * parseInt(value["Amount"]));
});
return price;
}
};
這樣一來就有間的的購物車功能了~