127 lines
4.1 KiB
JavaScript
127 lines
4.1 KiB
JavaScript
var cart = [];
|
|
var cartForForm = [];
|
|
var products = [];
|
|
var productsForForm = [];
|
|
var new_product_total_price = 0.0;
|
|
var total_quantity = 0;
|
|
var cardAmount = 0;
|
|
|
|
const addToCard = (id, price, quantity, name, image, vendor) => {
|
|
console.log("add to card function");
|
|
console.log(localStorage.getItem("cartForForm"));
|
|
console.log(productsForForm);
|
|
var exists = false;
|
|
cart.map((item) => {
|
|
if (item.id === id){
|
|
item.price = price;
|
|
item.quantity += quantity;
|
|
item.name = name;
|
|
item.image = image;
|
|
item.total = parseFloat(price * quantity);
|
|
item.vendor = vendor;
|
|
exists = true;
|
|
}
|
|
});
|
|
|
|
cartForForm.map((item) => {
|
|
if (item.item === id){
|
|
item.qty += quantity;
|
|
item.price = price;
|
|
item.vendor = vendor;
|
|
exists = true;
|
|
}
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!exists){
|
|
cart.push({
|
|
id: id,
|
|
price: price,
|
|
quantity: quantity,
|
|
name: name,
|
|
image: image,
|
|
total: parseFloat(price * quantity),
|
|
vendor: vendor
|
|
});
|
|
|
|
cartForForm.push({
|
|
item: id,
|
|
qty: quantity,
|
|
price: price,
|
|
vendor: vendor
|
|
});
|
|
}
|
|
localStorage.setItem("cart", JSON.stringify(cart));
|
|
localStorage.setItem("cartForForm", JSON.stringify(cartForForm));
|
|
console.log(localStorage.getItem("cartForForm"));
|
|
}
|
|
|
|
|
|
window.onload = () => {
|
|
console.log("window onload");
|
|
cart = JSON.parse(localStorage.getItem("cart")) ?? [];
|
|
cartForForm = JSON.parse(localStorage.getItem("cartForForm")) ?? [];
|
|
var totalQuantity = 0;
|
|
var totalPrice = 0;
|
|
cart.map((item) => {
|
|
totalQuantity += parseInt(item.quantity);
|
|
totalPrice += parseInt(item.price * item.quantity);
|
|
});
|
|
$("#cardQuantity").html(totalQuantity);
|
|
$("#cardAmount").html(totalPrice.toFixed(2));
|
|
|
|
};
|
|
|
|
|
|
$(".addToCard").on('click', function (){
|
|
console.log("add to card");
|
|
let id = parseInt($(this).data('id'));
|
|
let price = parseFloat($(this).data('price'));
|
|
let quantity = parseInt($("#quantity"+id).val());
|
|
let name = $(this).data('name')
|
|
let image = $(this).data('image')
|
|
let vendor = $(this).data('vendor')
|
|
$("#cardAmount").html((parseFloat($("#cardAmount").html()) + (price * quantity)).toFixed(2));
|
|
$("#cardQuantity").html(parseInt($("#cardQuantity").html()) + quantity);
|
|
addToCard(id, price, quantity, name, image, vendor);
|
|
toastrCustomSuccess("Sebede goşuldy");
|
|
});
|
|
|
|
|
|
function updateProducts(){
|
|
console.log("Product update");
|
|
const cartBody = $("#cartBody");
|
|
cartBody.html('');
|
|
new_product_total_price = 0;
|
|
total_quantity = 0;
|
|
cardAmount = 0;
|
|
products.map((item) => {
|
|
new_product_total_price = parseFloat(new_product_total_price) + parseFloat(item.total);
|
|
total_quantity = total_quantity + parseInt(item.quantity);
|
|
cartBody.append(`
|
|
<tr>
|
|
<td>
|
|
<img src="`+item.image+`" alt="`+item.name+`">
|
|
</td>
|
|
<td>`+item.name+`</td>
|
|
<td>`+item.price+`</td>
|
|
<td>
|
|
<div class="quantity input-counter">
|
|
<span class="minus-btn mr-2" data-id="`+item.id+`"><i class='icofont-minus'></i></span>
|
|
<input type="text" value="`+item.quantity+`">
|
|
<span class="plus-btn ml-2" data-id="`+item.id+`"><i class='icofont-plus'></i></span>
|
|
</div>
|
|
</td>
|
|
<td>`+item.price * item.quantity+`</td>
|
|
</tr>
|
|
`);
|
|
});
|
|
$("#cardQuantity").html(total_quantity);
|
|
$("#cardAmount").html(new_product_total_price.toFixed(2));
|
|
$("#card_amount").val(new_product_total_price.toFixed(2));
|
|
$("#items").val(JSON.stringify(productsForForm));
|
|
localStorage.setItem("cart", JSON.stringify(products));
|
|
localStorage.setItem("cartForForm", JSON.stringify(productsForForm));
|
|
} |