Skip to content

Commit 8291baa

Browse files
committed
generate initial price for payment summary
1 parent 4f26a64 commit 8291baa

File tree

5 files changed

+72
-35
lines changed

5 files changed

+72
-35
lines changed

basic-projects/amazon-project/checkout.html

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,39 +47,8 @@
4747
<!-- generated in js -->
4848
</div>
4949

50-
<div class="payment-summary">
51-
<div class="payment-summary-title">
52-
Order Summary
53-
</div>
54-
55-
<div class="payment-summary-row">
56-
<div>Items (3):</div>
57-
<div class="payment-summary-money">$42.75</div>
58-
</div>
59-
60-
<div class="payment-summary-row">
61-
<div>Shipping &amp; handling:</div>
62-
<div class="payment-summary-money">$4.99</div>
63-
</div>
64-
65-
<div class="payment-summary-row subtotal-row">
66-
<div>Total before tax:</div>
67-
<div class="payment-summary-money">$47.74</div>
68-
</div>
69-
70-
<div class="payment-summary-row">
71-
<div>Estimated tax (10%):</div>
72-
<div class="payment-summary-money">$4.77</div>
73-
</div>
74-
75-
<div class="payment-summary-row total-row">
76-
<div>Order total:</div>
77-
<div class="payment-summary-money">$52.51</div>
78-
</div>
79-
80-
<button class="place-order-button button-primary">
81-
Place your order
82-
</button>
50+
<div class="payment-summary js-payment-summary">
51+
8352
</div>
8453
</div>
8554
</div>

basic-projects/amazon-project/data/cart.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export function calculateCartQuantity(){
5252
cart.forEach((cartItem) => {
5353
cartQuantity += cartItem.quantity;
5454
})
55+
saveToStorage();
5556
return cartQuantity;
5657
};
5758

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import { renderOrderSummary } from "./checkout/orderSummary.js";
2+
import { renderPaymentSummary } from "./checkout/payment-summary.js";
23

4+
renderPaymentSummary()
35
renderOrderSummary();

basic-projects/amazon-project/scripts/checkout/orderSummary.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,7 @@ export function renderOrderSummary(){
178178
.forEach((link) => {
179179
const {itemId} = link.dataset;
180180
link.addEventListener('click', () => {
181-
182181
saveUpdatedQuantity(itemId);
183-
184182
})
185183
});
186184

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { cart, calculateCartQuantity } from "../../data/cart.js"
2+
import { products } from "../../data/products.js";
3+
import formatCurrency from "../utils/price.js";
4+
5+
6+
7+
8+
9+
let itemsPrice = 0;
10+
11+
cart.forEach((cartItem) => {
12+
// get productId out of cartItem
13+
const {productId} = cartItem;
14+
15+
let matchingProduct;
16+
// get the full product just using id
17+
products.forEach((product) => {
18+
if (product.id === productId){
19+
matchingProduct = product;
20+
}
21+
});
22+
itemsPrice += Number(formatCurrency(matchingProduct.priceCents * cartItem.quantity))
23+
});
24+
25+
export function renderPaymentSummary(){
26+
27+
28+
document.querySelector('.js-payment-summary')
29+
.innerHTML = `
30+
31+
<div class="payment-summary-title">
32+
Order Summary
33+
</div>
34+
35+
<div class="payment-summary-row">
36+
<div>Items (${calculateCartQuantity()}):</div>
37+
<div class="payment-summary-money">$${itemsPrice}</div>
38+
</div>
39+
40+
<div class="payment-summary-row">
41+
<div>Shipping &amp; handling:</div>
42+
<div class="payment-summary-money">$4.99</div>
43+
</div>
44+
45+
<div class="payment-summary-row subtotal-row">
46+
<div>Total before tax:</div>
47+
<div class="payment-summary-money">$47.74</div>
48+
</div>
49+
50+
<div class="payment-summary-row">
51+
<div>Estimated tax (10%):</div>
52+
<div class="payment-summary-money">$4.77</div>
53+
</div>
54+
55+
<div class="payment-summary-row total-row">
56+
<div>Order total:</div>
57+
<div class="payment-summary-money">$52.51</div>
58+
</div>
59+
60+
<button class="place-order-button button-primary">
61+
Place your order
62+
</button>
63+
64+
`
65+
66+
67+
}

0 commit comments

Comments
 (0)