@@ -80,7 +80,7 @@ function changeCatalogMenueFunction(event) {
8080 }
8181}
8282
83- // ืืื ืกืช ืืืฆืจืื ืืชืื ื'ืืืกืื ืืงืืืื ืืืคื ืืขืื
83+ //?: ืืื ืกืช ืืืฆืจืื ืืชืื ื'ืืืกืื ืืงืืืื ืืืคื ืืขืื
8484fetch ( '../assets/products.json' )
8585 . then ( response => response . json ( ) )
8686 . then ( products => {
@@ -337,7 +337,7 @@ function updateWishlistHearts() {
337337 } ) ;
338338}
339339
340- //?: ืืขืื ื ืืืฉื ืฉื ืืืืฆืจืื ืืืืืงืืืกืืืจืื' ืืขืช ืจืขื ืื ืื ืืขืื ืช ืืืฃ ืืืขืืคืื
340+ //?: ืืขืื ื ืืืฉื ืฉื ืืืืฆืจืื ืืืืืงืืืกืืืจืื' ืืขืช ืจืขื ืื ืื ืืขืื ืช ืืืฃ
341341document . 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: ืคืื ืงืฆืื ืืื ืืืช ืคืจืื ืขืืื ืืืฉ
608614function 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