依你的需求,我將前面較簡潔的「商店分組購物車UI」與你給的 PHP 動態資料迭代模式結合起來,並加入:
- 移除商品按鈕
- 可操作的加減數量控制 (外觀與結構參考你提供的範例)
- 響應式兼容手機/平板/電腦
- 加入兩個商店的範例資料區塊
- 調整細節符合你現有的樣式架構與語意
完整示範(含靜態HTML模擬兩商店範例,依你PHP資料迴圈可覆用此結構)
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>分店分類購物車示範</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
background-color: #f7f8fa;
margin: 0; padding: 30px;
}
.cart-page {
max-width: 940px;
margin: 0 auto;
display: flex;
flex-wrap: wrap;
gap: 24px;
}
/* 左側購物車物品區 */
.cart-main {
flex: 1 1 620px;
min-width: 300px;
}
/* 商店分組 */
.store-group {
background: #fff;
border-radius: 8px;
border: 1px solid #ddd;
padding: 18px 20px 24px;
margin-bottom: 30px;
}
.store-name {
font-weight: 700;
font-size: 1.4rem;
color: #222;
border-bottom: 2px solid #ccc;
padding-bottom: 8px;
margin-bottom: 15px;
}
/* 商品列表 */
.cart-items {
display: flex;
flex-direction: column;
gap: 14px;
}
.cart-item {
position: relative;
display: flex;
gap: 16px;
border-bottom: 1px solid #eee;
padding-bottom: 16px;
}
.cart-item:last-child {
border-bottom: none;
}
/* 移除按鈕 */
.remove-btn {
position: absolute;
top: 8px;
right: 8px;
background: transparent;
border: none;
color: #bb2222;
font-weight: 700;
font-size: 22px;
cursor: pointer;
user-select: none;
}
.remove-btn:hover {
color: #e00;
}
/* 商品縮圖 */
.item-image {
flex-shrink: 0;
width: 80px;
height: 80px;
border-radius: 6px;
overflow: hidden;
background: #f0f0f0;
}
.item-image img {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
}
/* 商品詳細 */
.item-details {
flex: 1;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.item-title {
font-weight: 600;
font-size: 1.1rem;
margin: 0 0 6px;
color: #222;
line-height: 1.3;
}
.item-title a {
color: inherit;
text-decoration: none;
}
.item-title a:hover,
.item-title a:focus {
text-decoration: underline;
}
.unit-price {
color: #555;
font-size: 0.95rem;
}
/* 數量與價格區 */
.quantity-and-total {
margin-top: 8px;
display: flex;
justify-content: space-between;
align-items: center;
}
/* 數量控制器 */
.quantity-control {
display: flex;
border: 1px solid #ccc;
border-radius: 4px;
overflow: hidden;
width: 130px;
user-select: none;
}
.quantity-control button {
background: #f0f0f0;
border: none;
width: 36px;
height: 36px;
font-size: 22px;
color: #444;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: background-color 0.2s ease;
}
.quantity-control button:hover {
background: #cde5ff;
}
.quantity-control input {
width: 58px;
border: none;
text-align: center;
font-size: 1.1rem;
outline: none;
-moz-appearance: textfield;
}
/* Chrome, Safari, Edge, Opera 去除input上下箭頭 */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
/* 商品合計價 */
.item-total-price {
font-weight: 700;
font-size: 1.1rem;
color: #0085ff;
min-width: 90px;
text-align: right;
}
/* 右側結帳側欄 */
.cart-sidebar {
flex: 0 0 280px;
background: #fff;
border-radius: 8px;
padding: 28px 24px;
border: 1px solid #ddd;
height: fit-content;
}
.sidebar-title {
font-size: 1.3rem;
font-weight: 700;
color: #222;
margin-bottom: 32px;
}
.summary-row {
display: flex;
justify-content: space-between;
font-size: 1.05rem;
margin-bottom: 20px;
color: #333;
}
.summary-row.total {
font-weight: 700;
font-size: 1.3rem;
border-top: 2px solid #0085ff;
padding-top: 18px;
margin-top: 24px;
color: #0085ff;
}
/* 結帳按鈕 */
.checkout-button {
width: 100%;
background-color: #0085ff;
color: #fff;
font-weight: 700;
font-size: 1.2rem;
padding: 16px 0;
border: none;
border-radius: 6px;
cursor: pointer;
margin-top: 36px;
user-select: none;
text-align: center;
text-decoration: none;
display: inline-block;
}
.checkout-button:disabled,
.checkout-button[disabled] {
background-color: #aacfff;
cursor: default;
}
.checkout-button:not(:disabled):hover,
.checkout-button:not(:disabled):focus {
background-color: #006feb;
}
/* 響應式 */
@media (max-width: 900px) {
.cart-page {
flex-direction: column;
}
.cart-sidebar {
width: 100%;
flex: none;
}
.cart-main {
width: 100%;
}
}
@media (max-width: 480px) {
.cart-item {
flex-direction: column;
gap: 10px;
padding-bottom: 12px;
}
.quantity-and-total {
flex-direction: column;
align-items: flex-start;
gap: 8px;
margin-top: 12px;
}
.item-total-price {
text-align: left;
min-width: auto;
font-size: 1.05rem;
}
.quantity-control {
width: 100%;
}
.quantity-control button {
width: 36px;
height: 36px;
font-size: 18px;
}
.quantity-control input {
font-size: 1.1rem;
width: 100%;
}
}
</style>
</head>
<body>
<div class="cart-page">
<!-- 購物車物品主要區 -->
<section class="cart-main">
<!-- 假PHP迴圈一:商店A -->
<div class="store-group" data-store="商店A">
<h2 class="store-name">商店A</h2>
<div class="cart-items">
<div class="cart-item" data-id="101" data-store="商店A" data-price="67900">
<button class="remove-btn" aria-label="移除此商品" title="移除此商品">×</button>
<div class="item-image">
<img src="https://images.unsplash.com/photo-1517336714731-489689fd1ca8?auto=format&fit=crop&w=80&q=80" alt="MacBook Pro 16吋" />
</div>
<div class="item-details">
<a href="#" class="item-title">蘋果 MacBook Pro 16吋 (M1 Max)</a>
<p class="unit-price">單價: NT$67,900</p>
<div class="quantity-and-total">
<div class="quantity-control" aria-label="調整商品數量">
<button type="button" class="quantity-minus" aria-label="數量減少">−</button>
<input type="number" class="item-qty" value="1" min="1" step="1" aria-live="polite" />
<button type="button" class="quantity-plus" aria-label="數量增加">+</button>
</div>
<div class="item-total-price" aria-live="polite">NT$67,900</div>
</div>
</div>
</div>
<div class="cart-item" data-id="102" data-store="商店A" data-price="2100">
<button class="remove-btn" aria-label="移除此商品" title="移除此商品">×</button>
<div class="item-image">
<img src="https://images.unsplash.com/photo-1574180045827-681f8a1a9622?auto=format&fit=crop&w=80&q=80" alt="Logitech 無線滑鼠" />
</div>
<div class="item-details">
<a href="#" class="item-title">Logitech 無線滑鼠 MX Master 3</a>
<p class="unit-price">單價: NT$2,100</p>
<div class="quantity-and-total">
<div class="quantity-control" aria-label="調整商品數量">
<button type="button" class="quantity-minus" aria-label="數量減少">−</button>
<input type="number" class="item-qty" value="2" min="1" step="1" aria-live="polite" />
<button type="button" class="quantity-plus" aria-label="數量增加">+</button>
</div>
<div class="item-total-price" aria-live="polite">NT$4,200</div>
</div>
</div>
</div>
</div>
</div>
<!-- 假PHP迴圈二:商店B -->
<div class="store-group" data-store="商店B">
<h2 class="store-name">商店B</h2>
<div class="cart-items">
<div class="cart-item" data-id="201" data-store="商店B" data-price="3500">
<button class="remove-btn" aria-label="移除此商品" title="移除此商品">×</button>
<div class="item-image">
<img src="https://images.unsplash.com/photo-1526170375885-4d8ecf77b99f?auto=format&fit=crop&w=80&q=80" alt="Nike 運動鞋" />
</div>
<div class="item-details">
<a href="#" class="item-title">Nike 運動鞋</a>
<p class="unit-price">單價: NT$3,500</p>
<div class="quantity-and-total">
<div class="quantity-control" aria-label="調整商品數量">
<button type="button" class="quantity-minus" aria-label="數量減少">−</button>
<input type="number" class="item-qty" value="1" min="1" step="1" aria-live="polite" />
<button type="button" class="quantity-plus" aria-label="數量增加">+</button>
</div>
<div class="item-total-price" aria-live="polite">NT$3,500</div>
</div>
</div>
</div>
<div class="cart-item" data-id="202" data-store="商店B" data-price="600">
<button class="remove-btn" aria-label="移除此商品" title="移除此商品">×</button>
<div class="item-image">
<img src="https://images.unsplash.com/photo-1528715471579-d5771f1b2e18?auto=format&fit=crop&w=80&q=80" alt="Adidas 運動襪" />
</div>
<div class="item-details">
<a href="#" class="item-title">Adidas 運動襪 (3雙組)</a>
<p class="unit-price">單價: NT$600</p>
<div class="quantity-and-total">
<div class="quantity-control" aria-label="調整商品數量">
<button type="button" class="quantity-minus" aria-label="數量減少">−</button>
<input type="number" class="item-qty" value="1" min="1" step="1" aria-live="polite" />
<button type="button" class="quantity-plus" aria-label="數量增加">+</button>
</div>
<div class="item-total-price" aria-live="polite">NT$600</div>
</div>
</div>
</div>
</div>
</div>
</section>
<!-- 右側結帳區 -->
<aside class="cart-sidebar" aria-label="購物車總計">
<h2 class="sidebar-title">購物車總額</h2>
<div class="summary-row">
<span>產品總額</span>
<span id="product-total">NT$78,300</span>
</div>
<div class="summary-row">
<span>運費</span>
<span id="shipping-fee">NT$0</span>
</div>
<div class="summary-row total">
<span>共需支付</span>
<span id="final-total" style="font-weight:bold;">NT$78,300</span>
</div>
<button id="checkout-btn" class="checkout-button" type="button">前往付款</button>
</aside>
</div>
<script>
// 取得所有商品量輸入&價格欄,與移除按鈕
const cartPage = document.querySelector('.cart-page');
const productTotalElem = document.getElementById('product-total');
const shippingFeeElem = document.getElementById('shipping-fee');
const finalTotalElem = document.getElementById('final-total');
const checkoutBtn = document.getElementById('checkout-btn');
// 假設運費為0,若有可修改此處
const SHIPPING_FEE = 0;
function updateCartTotals() {
let total = 0;
// 遍歷所有商品合計價(字串轉數字)
const itemTotals = cartPage.querySelectorAll('.item-total-price');
itemTotals.forEach(el => {
let text = el.textContent || el.innerText;
// 清除"NT$"及逗號,轉數字計算
let price = Number(text.replace(/[NT$,]/g, '').trim());
total += price;
});
productTotalElem.textContent = `NT$${total.toLocaleString()}`;
shippingFeeElem.textContent = `NT$${SHIPPING_FEE.toLocaleString()}`;
finalTotalElem.textContent = `NT$${(total + SHIPPING_FEE).toLocaleString()}`;
// 決定是否禁用結帳按鈕
checkoutBtn.disabled = total <= 0;
}
// 更新單項合計價
function updateItemTotal(itemElem) {
const qtyInput = itemElem.querySelector('.item-qty');
const priceAttr = itemElem.getAttribute('data-price');
const totalPriceElem = itemElem.querySelector('.item-total-price');
const price = Number(priceAttr);
const qty = Number(qtyInput.value);
const itemTotal = price * qty;
totalPriceElem.textContent = `NT$${itemTotal.toLocaleString()}`;
}
// 加減按鈕事件
cartPage.querySelectorAll('.cart-item').forEach(itemElem => {
const minusBtn = itemElem.querySelector('.quantity-minus');
const plusBtn = itemElem.querySelector('.quantity-plus');
const qtyInput = itemElem.querySelector('.item-qty');
minusBtn.addEventListener('click', () => {
let val = parseInt(qtyInput.value, 10);
if (val > parseInt(qtyInput.min, 10)) {
qtyInput.value = val - 1;
updateItemTotal(itemElem);
updateCartTotals();
}
});
plusBtn.addEventListener('click', () => {
// 假設無最大值限制,可自行擴充
let val = parseInt(qtyInput.value, 10);
qtyInput.value = val + 1;
updateItemTotal(itemElem);
updateCartTotals();
});
qtyInput.addEventListener('input', () => {
let val = parseInt(qtyInput.value, 10);
if (isNaN(val) || val < parseInt(qtyInput.min, 10)) {
qtyInput.value = qtyInput.min;
}
updateItemTotal(itemElem);
updateCartTotals();
});
});
// 移除商品按鈕事件
cartPage.querySelectorAll('.remove-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
const itemElem = e.target.closest('.cart-item');
if (itemElem) {
itemElem.remove();
updateCartTotals();
}
});
});
// 初始化載入時計算一次
updateCartTotals();
</script>
</body>
</html>
說明:
- 資料結構:模擬兩商店各有數件商品,
data-store 與 data-price 為方便 JS 操作設的自訂屬性,PHP可用相同邏輯產生。
- 可操作數量控件:有加、減按鈕及直接輸入欄位,支援最小值為1(可擴充最小/最大限制欄位),並會即時計算單項和全部總價。
- 移除按鈕:右上角 × 按鈕,點擊後會從 DOM 移除該商品,並重新計算總金額。
- 總價側欄:隨著商品數量/刪除變動即時計算更新,無商品時結帳會被禁用。
- 響應設計:寬螢幕雙欄顯示,窄螢幕自動堆疊;商品項目在手機尺寸會換成上下排列以利閱讀與操作。
- 無額外外部依賴,純 HTML/CSS/JS 實作,方便整合到你的 PHP 平台。
你可透過將 PHP 變數部分用你的實際資料輸出代替,結合前述 PHP分組結構。
若要動態刷更新,可依需求增補AJAX,或控制上下架邏輯。
如果需要我可以協助把原 PHP 迴圈部分寫成PHP模板示例。需要請告訴我!