gurl_o/themes/gurlushyk/assets/js/script.js

127 lines
4.1 KiB
JavaScript
Raw Normal View History

2023-10-12 20:59:00 +00:00
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) => {
2023-10-15 20:26:14 +00:00
console.log("add to card function");
console.log(localStorage.getItem("cartForForm"));
console.log(productsForForm);
2023-10-12 20:59:00 +00:00
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;
}
});
2023-10-15 20:26:14 +00:00
2023-10-12 20:59:00 +00:00
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));
2023-10-15 20:26:14 +00:00
console.log(localStorage.getItem("cartForForm"));
2023-10-12 20:59:00 +00:00
}
window.onload = () => {
2023-10-15 20:26:14 +00:00
console.log("window onload");
2023-10-12 20:59:00 +00:00
cart = JSON.parse(localStorage.getItem("cart")) ?? [];
2023-10-15 20:26:14 +00:00
cartForForm = JSON.parse(localStorage.getItem("cartForForm")) ?? [];
2023-10-12 20:59:00 +00:00
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));
};
2023-10-15 20:26:14 +00:00
$(".addToCard").on('click', function (){
console.log("add to card");
2023-10-12 20:59:00 +00:00
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(){
2023-10-15 20:26:14 +00:00
console.log("Product update");
2023-10-12 20:59:00 +00:00
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));
}