Skip to content

Commit b03427d

Browse files
committed
cart part 2
1 parent 36b53a5 commit b03427d

2 files changed

Lines changed: 86 additions & 15 deletions

File tree

โ€Žassets/products.jsonโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"description": "A high-performance supersport motorcycle with a 599cc engine, 117 hp, and advanced handling",
5252
"originalPrice": "",
5353
"discount": "",
54-
"finalPrice": "23769.91 $",
54+
"finalPrice": "23,769.91 $",
5555
"stock": 1,
5656
"buyLink": "../products/product page.html"
5757
},

โ€Žmoto ride.jsโ€Ž

Lines changed: 85 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function changeCatalogMenueFunction(event) {
8080
}
8181
}
8282

83-
// ื”ื›ื ืกืช ืžื•ืฆืจื™ื ืžืชื•ืš ื’'ื™ื™ืกื•ืŸ ืœืงื˜ืœื•ื’ ืื•ืคื ื•ืขื™ื
83+
//?: ื”ื›ื ืกืช ืžื•ืฆืจื™ื ืžืชื•ืš ื’'ื™ื™ืกื•ืŸ ืœืงื˜ืœื•ื’ ืื•ืคื ื•ืขื™ื
8484
fetch('../assets/products.json')
8585
.then(response => response.json())
8686
.then(products => {
@@ -337,7 +337,7 @@ function updateWishlistHearts() {
337337
});
338338
}
339339

340-
//?: ื˜ืขื™ื ื” ื—ื“ืฉื” ืฉืœ ื”ืžื•ืฆืจื™ื ืžื”ืœื•ืงืืœืกื˜ื•ืจืื’' ื‘ืขืช ืจืขื ื•ืŸ ืื• ื˜ืขื™ื ืช ื”ื“ืฃ ืžื•ืขื“ืคื™ื
340+
//?: ื˜ืขื™ื ื” ื—ื“ืฉื” ืฉืœ ื”ืžื•ืฆืจื™ื ืžื”ืœื•ืงืืœืกื˜ื•ืจืื’' ื‘ืขืช ืจืขื ื•ืŸ ืื• ื˜ืขื™ื ืช ื”ื“ืฃ
341341
document.addEventListener('DOMContentLoaded', () => {
342342
loadWishlistFromLocalStorage();
343343
loadCartFromLocalStorage();
@@ -522,8 +522,12 @@ function saveToCart(event){
522522
const logo = productBox.querySelector('.product_logo_img')?.src;
523523
const originalPrice = productBox.querySelector('.original_price')?.textContent.trim();
524524
const discount = productBox.querySelector('.discount')?.textContent.trim();
525-
const finalPrice = productBox.querySelector('h2')?.textContent.trim();
525+
const finalPriceText = productBox.querySelector('h2')?.textContent.trim() || '0';
526+
527+
const finalPrice = parsePriceToNumber(finalPriceText);
528+
526529
const stock = parseInt(productBox.dataset.stock) || 1;
530+
const quantity = 1;
527531
const buyLink = productBox.querySelector('.a_button')?.href;
528532

529533
if (!productId || !name) return;
@@ -539,6 +543,7 @@ function saveToCart(event){
539543
discount,
540544
finalPrice,
541545
stock,
546+
quantity,
542547
buyLink
543548
};
544549

@@ -594,8 +599,9 @@ function loadCartFromLocalStorage() {
594599
createCartItemElement(product);
595600
});
596601

597-
//TODO: ืงืจื™ืื” ืœืคื•ื ืงืฆื™ื” ืฉื ื•ืชื ืช ืœื›ื•ืœื ืœืื—ืจ ื”ืฉืœื™ืคื” ืžื”ื–ื™ื›ืจื•ืŸ ืื™ื‘ื ื˜ ืœืžื—ื™ืงืช ืžื•ืฆืจ
602+
//TODO: ื”ื•ืกืคืช ืžืื–ื™ื ื™ื ืื—ืจื™ ื™ืฆื™ืจืช ื”ืืœืžื ื˜ื™ื
598603
addCartDeleteEventListeners();
604+
addCartQuantityEventListeners();
599605

600606
// ืžืขื“ื›ืŸ ืืช ื”ื˜ืงืกื˜ ื‘ื›ื•ืชืจืช ืื—ืจื™ ืฉื™ื ื•ื™
601607
const headerParagraph = document.querySelector('.cart_header p');
@@ -607,13 +613,16 @@ function loadCartFromLocalStorage() {
607613
//TODO: ืคื•ื ืงืฆื™ื” ืœื‘ื ื™ื™ืช ืคืจื™ื˜ ืขื’ืœื” ื—ื“ืฉ
608614
function createCartItemElement(cartProductData) {
609615
// ื™ืฆื™ืจืช ืชื™ื‘ืช <li> ืฉืชื›ื™ืœ ืืช ื”ืžื•ืฆืจ
610-
const listItem = document.createElement('li');
611-
listItem.setAttribute('data-id', cartProductData.id); // ืœื”ื•ืกืคื” ืื ืชืฆื˜ืจืš ื‘ืขืชื™ื“
616+
const cartListItem = document.createElement('li');
617+
cartListItem.setAttribute('data-id', cartProductData.id); // ืœื”ื•ืกืคื” ืื ืชืฆื˜ืจืš ื‘ืขืชื™ื“
612618

613619
// ื™ืฆื™ืจืช ืชื™ื‘ืช ื”ืžื•ืฆืจ ื”ื›ืœืœื™ืช ื‘ืขื’ืœื”
614620
const cartProductBox = document.createElement('div');
615621
cartProductBox.classList.add('chosen_product_list');
616622

623+
// ื”ื•ืกืคืช data-price ืขื ื”ืžื—ื™ืจ ืœื™ื—ื™ื“ื” (ืžืกืคืจ)
624+
cartProductBox.dataset.price = cartProductData.finalPrice;
625+
617626
// ื™ืฆื™ืจืช ื›ืคืชื•ืจ ืžื—ื™ืงืช ืžื•ืฆืจ ืžื”ืขื’ืœื”
618627
const deleteIcon = document.createElement('i');
619628
deleteIcon.classList.add('cart_delete', 'fa-solid', 'fa-xmark');
@@ -644,22 +653,22 @@ function createCartItemElement(cartProductData) {
644653

645654
// ืžื—ื™ืจ ืœื™ื—ื™ื“ื”
646655
const pricePerItem = document.createElement('li');
647-
pricePerItem.textContent = `${cartProductData.finalPrice}`;
648-
656+
pricePerItem.classList.add('cart_price_per_item')
657+
pricePerItem.textContent = `${cartProductData.finalPrice.toLocaleString('he-IL', { minimumFractionDigits: 2, maximumFractionDigits: 2 })} $`;
649658
// ืชื™ื‘ืช ื›ืžื•ืช ืœื‘ื—ื™ืจืช ืžืกืคืจ ื™ื—ื™ื“ื•ืช
650659
const quantityItem = document.createElement('li');
651660
const quantityInput = document.createElement('input');
652661
quantityInput.type = 'number';
653-
quantityInput.value = 1; // ืื ืื™ืŸ ื›ืžื•ืช, ืฉื™ื 1 ื›ื‘ืจื™ืจืช ืžื—ื“ืœ
662+
quantityInput.value = cartProductData.quantity || 1;
654663
quantityInput.min = 1;
655664
quantityInput.classList.add('chosen_product_input');
656665
quantityItem.appendChild(quantityInput);
657666

658667
// ืžื—ื™ืจ ื›ื•ืœืœ (ืžื—ื™ืจ ืœื™ื—ื™ื“ื” * ื›ืžื•ืช)
659668
const totalItem = document.createElement('li');
669+
totalItem.classList.add('cart_item_total_price');
660670
const totalPrice = cartProductData.finalPrice;
661-
totalItem.textContent = `${totalPrice}`;
662-
671+
totalItem.textContent = `${(cartProductData.finalPrice * cartProductData.quantity).toLocaleString('he-IL', { minimumFractionDigits: 2, maximumFractionDigits: 2 })} $`;
663672
// ื—ื™ื‘ื•ืจ ื›ืœ ืฉื•ืจื•ืช ื”ืžื™ื“ืข ืœืจืฉื™ืžื”
664673
detailsList.appendChild(nameItem);
665674
detailsList.appendChild(pricePerItem);
@@ -672,9 +681,9 @@ function createCartItemElement(cartProductData) {
672681
cartProductBox.appendChild(detailsList);
673682

674683
// ื”ื•ืกืคืช ื”ืงื•ืคืกื” ืœึพ<li>
675-
listItem.appendChild(cartProductBox);
684+
cartListItem.appendChild(cartProductBox);
676685

677-
document.querySelector('#chosen_products_list_ul').appendChild(listItem);
686+
document.querySelector('#chosen_products_list_ul').appendChild(cartListItem);
678687
}
679688

680689
//TODO: ืœืื—ืจ ื™ืฆื™ืจืช ื›ืœ ืคืจื™ื˜ ื‘ืขื’ืœื” ืคื•ื ืงืฆื™ื” ืฉืžื•ืกื™ืคื” ืื™ืจื•ืข ืœืžื—ื™ืงืช ืคืจื™ื˜ื™ื
@@ -711,4 +720,66 @@ function handleDeleteCartItem(event) {
711720
if (headerParagraph) {
712721
headerParagraph.textContent = 'You have: ' + cart.length + ' items in your wishlist.';
713722
}
714-
}
723+
}
724+
725+
//TODO: ื”ืฉืžื” ืฉืœ ืื™ื‘ื ื˜ ืขืœ ื”ื›ืžื•ืช ืฉื”ืžืฉืชืžืฉ ืจื•ืฆื”
726+
function addCartQuantityEventListeners() {
727+
const quantityInputs = document.querySelectorAll('.chosen_product_input');
728+
quantityInputs.forEach(input => {
729+
input.addEventListener('input', cartQuantity);
730+
});
731+
}
732+
733+
//TODO: ืคื•ื ืงืฆื™ื” ืฉืžืฉื ื” ืืช ื”ืžื—ื™ืจ ืฉืœ ื”ืžื•ืฆืจ ื‘ืขื’ืœื”
734+
function cartQuantity(event) {
735+
const quantityInput = event.currentTarget;
736+
const quantityValue = Number(quantityInput.value);
737+
const productBox = quantityInput.closest('.chosen_product_list');
738+
739+
if (!productBox) return;
740+
741+
// ืงืจื™ืืช ื”ืžื—ื™ืจ ืžื”-data-price
742+
const pricePerItem = Number(productBox.dataset.price);
743+
const totalPricePerItemElement = productBox.querySelector('.cart_item_total_price');
744+
745+
if (!isNaN(pricePerItem) && totalPricePerItemElement) {
746+
const total = quantityValue * pricePerItem;
747+
totalPricePerItemElement.textContent = `${total.toLocaleString('he-IL', { minimumFractionDigits: 2, maximumFractionDigits: 2 })} $`;
748+
}
749+
750+
// ืขื“ื›ื•ืŸ localStorage
751+
const listItem = quantityInput.closest('li[data-id]');
752+
if (!listItem) return;
753+
const productId = listItem.getAttribute('data-id');
754+
755+
let cart = JSON.parse(localStorage.getItem('cart')) || [];
756+
const productIndex = cart.findIndex(item => item.id === productId);
757+
if (productIndex !== -1) {
758+
cart[productIndex].quantity = quantityValue;
759+
localStorage.setItem('cart', JSON.stringify(cart));
760+
}
761+
}
762+
763+
function parsePriceToNumber(priceString) {
764+
if (!priceString) return 0;
765+
766+
// ื”ืกืจืช ืชื•ื•ื™ื ืฉืื™ื ื ืกืคืจื•ืช, ื ืงื•ื“ื” ืื• ืคืกื™ืง
767+
let cleaned = priceString.replace(/[^\d.,]/g, '');
768+
769+
if (cleaned.includes('.') && cleaned.includes(','))
770+
{
771+
// ืื ื™ืฉ ื’ื ื ืงื•ื“ื” ื•ื’ื ืคืกื™ืง, ื”ืคืกื™ืง ื”ื•ื ืืœืคื™ื - ื ืกื™ืจ ืื•ืชื•
772+
cleaned = cleaned.replace(/,/g, '');
773+
}
774+
775+
else
776+
if (cleaned.includes(',') && !cleaned.includes('.')) {
777+
// ืื ื™ืฉ ืจืง ืคืกื™ืงื™ื ื•ื ืงื•ื“ื” ืœื ืงื™ื™ืžืช, ื ื—ืœื™ืฃ ืคืกื™ืงื™ื ื‘ื ืงื•ื“ื•ืช (ืœืžืงืจื” ืฉืœ ืขืฉืจื•ื ื™ ืขื ืคืกื™ืง)
778+
cleaned = cleaned.replace(/,/g, '.');
779+
}
780+
781+
const number = parseFloat(cleaned);
782+
783+
return isNaN(number) ? 0 : number;
784+
}
785+

0 commit comments

Comments
ย (0)