1- // 購物車計算器 - 完美修復版本
2- // 所有 Bug 已修復,功能完整運作 !
1+ // 購物車計算器 - 6個Bug修復完成版本
2+ // 對應簡化教學:所有核心問題都已解決 !
33
44let cart = [ ] ;
55let discount = 0 ;
66let freeShipping = false ;
77
8- // 修復:函數名稱與 HTML 呼叫一致
8+ // ✅ 修復Bug 6:加入輸入驗證,改善使用者體驗
99function addToCart ( productId , price ) {
1010 const qtyInput = document . getElementById ( productId + '-qty' ) ;
1111 const quantity = parseInt ( qtyInput . value ) ;
1212
13- if ( quantity <= 0 ) {
14- showNotification ( '請選擇數量' , 'warning ') ;
13+ if ( quantity <= 0 || isNaN ( quantity ) ) {
14+ alert ( '請輸入有效的數量 ') ;
1515 return ;
1616 }
1717
18- // 修復:正確處理商品重複添加的數量更新
18+ // 處理重複商品(保持簡潔)
1919 const existingItem = cart . find ( item => item . id === productId ) ;
2020 if ( existingItem ) {
2121 existingItem . quantity += quantity ;
22- showNotification ( `已更新 ${ getProductName ( productId ) } 數量` , 'success' ) ;
2322 } else {
2423 cart . push ( {
2524 id : productId ,
2625 name : getProductName ( productId ) ,
2726 price : price ,
2827 quantity : quantity
2928 } ) ;
30- showNotification ( `已添加 ${ getProductName ( productId ) } 到購物車` , 'success' ) ;
3129 }
3230
3331 qtyInput . value = 0 ;
@@ -69,13 +67,13 @@ function updateCartDisplay() {
6967}
7068
7169function updatePricing ( ) {
72- // 修復:subtotal 計算正確 - 使用乘法
70+ // ✅ 修復Bug 1:總金額計算正確 - 使用乘法
7371 let subtotal = 0 ;
7472 cart . forEach ( item => {
75- subtotal += item . price * item . quantity ; // 修復:使用乘法
73+ subtotal += item . price * item . quantity ; // ✅ 修復:使用乘法而不是加法
7674 } ) ;
7775
78- // 修復 :運費計算邏輯正確
76+ // ✅ 修復Bug 2 :運費計算邏輯正確
7977 let shipping = 0 ;
8078 if ( ! freeShipping ) {
8179 if ( subtotal < 10000 ) {
@@ -86,7 +84,7 @@ function updatePricing() {
8684 // 超過 30000 免運費(shipping = 0)
8785 }
8886
89- // 修復:優惠券折扣計算正確
87+ // 優惠券折扣計算
9088 const discountAmount = subtotal * ( discount / 100 ) ;
9189 const total = subtotal + shipping - discountAmount ;
9290
@@ -97,159 +95,76 @@ function updatePricing() {
9795 document . getElementById ( 'total' ) . textContent = `$${ total . toLocaleString ( ) } ` ;
9896}
9997
98+ // 移除商品功能(保持簡潔)
10099function removeFromCart ( index ) {
101- // 修復:移除商品時有確認提示
102- const item = cart [ index ] ;
103- if ( confirm ( `確定要移除 ${ item . name } 嗎?` ) ) {
104- cart . splice ( index , 1 ) ;
105- showNotification ( `已移除 ${ item . name } ` , 'info' ) ;
106- updateCartDisplay ( ) ;
107- }
100+ cart . splice ( index , 1 ) ;
101+ updateCartDisplay ( ) ;
108102}
109103
110- // 修復 :清空購物車函數名稱正確
104+ // ✅ 修復Bug 3 :清空購物車函數名稱正確
111105function clearCart ( ) {
112106 if ( cart . length === 0 ) {
113- showNotification ( '購物車已經是空的' , 'info ') ;
107+ alert ( '購物車已經是空的' ) ;
114108 return ;
115109 }
116110
117- if ( confirm ( '確定要清空購物車嗎?' ) ) {
118- cart = [ ] ;
119- discount = 0 ;
120- freeShipping = false ;
121- document . getElementById ( 'coupon-status' ) . innerHTML = '' ;
122- showNotification ( '購物車已清空' , 'info' ) ;
123- updateCartDisplay ( ) ;
124- }
111+ cart = [ ] ;
112+ discount = 0 ;
113+ freeShipping = false ;
114+ updateCartDisplay ( ) ;
125115}
126116
117+ // ✅ 修復Bug 4:優惠券功能完整實現
127118function applyCoupon ( ) {
128- const couponCode = document . getElementById ( 'coupon-code' ) . value . trim ( ) . toUpperCase ( ) ;
129-
130- if ( ! couponCode ) {
131- showNotification ( '請輸入優惠券代碼' , 'warning' ) ;
132- return ;
133- }
119+ const couponCode = document . getElementById ( 'coupon-code' ) . value . toUpperCase ( ) ;
134120
135- // 修復:優惠券邏輯完整
136- let couponMessage = '' ;
137121 switch ( couponCode ) {
138122 case 'SAVE10' :
139123 discount = 10 ;
140124 freeShipping = false ;
141- couponMessage = '已套用 10% 折扣!' ;
125+ alert ( '已套用 10% 折扣!' ) ;
142126 break ;
143127 case 'SAVE20' :
144128 discount = 20 ;
145129 freeShipping = false ;
146- couponMessage = '已套用 20% 折扣!' ;
130+ alert ( '已套用 20% 折扣!' ) ;
147131 break ;
148132 case 'FREESHIP' :
149- // 修復:免運費功能完整實現
133+ // ✅ 修復:免運費功能完整實現
150134 discount = 0 ;
151135 freeShipping = true ;
152- couponMessage = '已套用免運費!' ;
136+ alert ( '已套用免運費!' ) ;
153137 break ;
154138 default :
155- showNotification ( '無效的優惠券代碼' , 'error ') ;
139+ alert ( '無效的優惠券代碼' ) ;
156140 return ;
157141 }
158142
159143 document . getElementById ( 'coupon-code' ) . value = '' ;
160- document . getElementById ( 'coupon-status' ) . innerHTML = `<span class="coupon-active">${ couponMessage } </span>` ;
161- showNotification ( couponMessage , 'success' ) ;
162-
163- // 修復:正確更新優惠券狀態
164144 updatePricing ( ) ;
165145}
166146
147+ // ✅ 修復Bug 5:結帳後自動清空購物車
167148function checkout ( ) {
168149 if ( cart . length === 0 ) {
169- showNotification ( '購物車是空的!' , 'warning ') ;
150+ alert ( '購物車是空的!' ) ;
170151 return ;
171152 }
172153
173154 const total = document . getElementById ( 'total' ) . textContent ;
174- showNotification ( `結帳成功!總金額:${ total } ` , 'success' ) ;
175-
176- // 修復:結帳後清空購物車
177- setTimeout ( ( ) => {
178- cart = [ ] ;
179- discount = 0 ;
180- freeShipping = false ;
181- document . getElementById ( 'coupon-status' ) . innerHTML = '' ;
182- updateCartDisplay ( ) ;
183- showNotification ( '感謝您的購買!' , 'info' ) ;
184- } , 1500 ) ;
185- }
186-
187- // 修復:當使用者修改商品數量時,自動更新購物車
188- function updateQuantityRealtime ( productId ) {
189- const qtyInput = document . getElementById ( productId + '-qty' ) ;
190- const quantity = parseInt ( qtyInput . value ) || 0 ;
191-
192- // 找到購物車中對應的商品並更新數量
193- const existingItem = cart . find ( item => item . id === productId ) ;
194- if ( existingItem && quantity > 0 ) {
195- existingItem . quantity = quantity ;
196- updateCartDisplay ( ) ;
197- } else if ( existingItem && quantity === 0 ) {
198- // 如果數量設為 0,移除該商品
199- const index = cart . findIndex ( item => item . id === productId ) ;
200- cart . splice ( index , 1 ) ;
201- updateCartDisplay ( ) ;
202- }
203- }
204-
205- // 新增:通知系統
206- function showNotification ( message , type = 'info' ) {
207- // 移除現有通知
208- const existingNotification = document . querySelector ( '.notification' ) ;
209- if ( existingNotification ) {
210- existingNotification . remove ( ) ;
211- }
212-
213- // 創建新通知
214- const notification = document . createElement ( 'div' ) ;
215- notification . className = `notification ${ type } ` ;
216- notification . textContent = message ;
217-
218- // 添加到頁面
219- document . body . appendChild ( notification ) ;
155+ alert ( `結帳成功!總金額:${ total } ` ) ;
220156
221- // 顯示動畫
222- setTimeout ( ( ) => notification . classList . add ( 'show' ) , 100 ) ;
223-
224- // 自動隱藏
225- setTimeout ( ( ) => {
226- notification . classList . remove ( 'show' ) ;
227- setTimeout ( ( ) => notification . remove ( ) , 300 ) ;
228- } , 3000 ) ;
229- }
230-
231- // 新增:頁面載入時初始化
232- document . addEventListener ( 'DOMContentLoaded' , function ( ) {
157+ // ✅ 修復:結帳後清空購物車
158+ cart = [ ] ;
159+ discount = 0 ;
160+ freeShipping = false ;
233161 updateCartDisplay ( ) ;
234- showNotification ( '購物車系統已就緒!' , 'success' ) ;
235- } ) ;
162+ }
236163
237- // 新增:鍵盤快捷鍵支援
238- document . addEventListener ( 'keydown' , function ( e ) {
239- if ( e . ctrlKey || e . metaKey ) {
240- switch ( e . key ) {
241- case 'Enter' :
242- if ( document . activeElement . id === 'coupon-code' ) {
243- e . preventDefault ( ) ;
244- applyCoupon ( ) ;
245- }
246- break ;
247- case 'Backspace' :
248- if ( e . target . tagName !== 'INPUT' ) {
249- e . preventDefault ( ) ;
250- clearCart ( ) ;
251- }
252- break ;
253- }
254- }
255- } ) ;
164+ // ✅ 簡潔版本:所有6個核心Bug都已修復
165+ // 1. 總金額計算正確(乘法)
166+ // 2. 運費邏輯正確(不再永遠150)
167+ // 3. 清空購物車按鈕正常運作
168+ // 4. FREESHIP 優惠券實際有效
169+ // 5. 結帳後自動清空購物車
170+ // 6. 輸入驗證改善
0 commit comments