File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -58,4 +58,33 @@ productsDiv.addEventListener("click", (e) => {
5858 }
5959} )
6060
61+ const calculateProductAndCartTotal = ( productInfoDiv ) => {
62+ console . log ( productInfoDiv ) ;
63+ let productQuantity = productInfoDiv . querySelector ( "#product-quantity" ) . innerText ;
64+ let productPrice = productInfoDiv . querySelector ( "strong" ) . innerText ;
65+ let productTotalPriceDiv = productInfoDiv . querySelector ( ".product-line-price" ) ;
66+ productTotalPriceDiv . innerText = ( productQuantity * productPrice ) . toFixed ( 2 ) ;
6167
68+ calculateCartTotal ( ) ;
69+ }
70+
71+ const calculateCartTotal = ( ) => {
72+ let productTotalPriceDivs = document . querySelectorAll ( ".product-line-price" ) ;
73+ // console.log(productTotalPriceDivs);
74+ let subtotal = 0 ;
75+ productTotalPriceDivs . forEach ( eachProductTotalPriceDiv => {
76+ subtotal += parseFloat ( eachProductTotalPriceDiv . innerText )
77+ } ) ;
78+ console . log ( subtotal ) ;
79+ let taxPrice = subtotal * localStorage . getItem ( "taxRate" ) ;
80+ console . log ( taxPrice ) ;
81+ let shipping = ( subtotal > 0 ? parseFloat ( localStorage . getItem ( "shippingPrice" ) ) : 0 ) ;
82+ console . log ( shipping ) ;
83+ let cartTotal = subtotal + taxPrice + shipping ;
84+ console . log ( cartTotal ) ;
85+
86+ document . querySelector ( "#cart-subtotal p:nth-child(2)" ) . innerText = subtotal . toFixed ( 2 ) ;
87+ document . querySelector ( "#cart-tax p:nth-child(2)" ) . innerText = taxPrice . toFixed ( 2 ) ;
88+ document . querySelector ( "#cart-shipping p:nth-child(2)" ) . innerText = shipping . toFixed ( 2 ) ;
89+ document . querySelector ( "#cart-total" ) . lastElementChild . innerText = cartTotal . toFixed ( 2 ) ;
90+ }
You can’t perform that action at this time.
0 commit comments