To update your existing cart layout to align better with the provided template image (https://i.ibb.co/BKvZ914H/Test.png), I suggest the following improvements and refinements, focusing on structure and styling updates in your PHP rendering to approach that look:
Key observations from the template image:
- Store groups are clearly separated blocks with store name as a header.
- Each cart item has:
- Product image on the left,
- Product name and unit price info,
- Quantity controls,
- Total price on the right,
- A trash icon for removal on the top right of each item card.
- The quantity control has plus/minus buttons around the input.
- Pricing and quantities appear horizontally aligned and clearly spaced.
- Cart summary (right sidebar) shows product subtotal, shipping fee, and total — nicely boxed and aligned, with a prominent checkout button.
What to update and enhance in your code:
-
HTML Structure Adjustments
- Wrap cart-item contents better to allow aligned flexbox columns (image + details + qty/price + remove button).
- Better semantic grouping for quantity and total price in flex container.
- Position remove button absolutely inside
.cart-item so it floats top right.
- Add data attributes and ARIA roles if needed for better accessibility.
-
Fix/Improve CSS
- Use flexbox to align
.cart-item children horizontally with consistent spacing.
.item-details to contain product name and unit price stacked vertically.
- Right side: quantity and total price side by side within
.quantity-and-total.
- Adjust margins/paddings per the screenshot.
- Adjust font sizes and font weight for product name, and highlight prices accordingly.
- Ensure
.remove-btn is overlaid on top right as trash icon.
- Adjust responsive styles ensuring mobile stacks nicely.
-
PHP Logic Minor Fixes
- Your current data grouping is fine.
- Make sure prices are formatted consistently (e.g., number_format or currency format).
- Maintain current validation of stock and quantity limits.
Full updated snippet (PHP + relevant CSS + HTML parts only):
<?php
// GROUP CART ITEMS BY STORE
$grouped = [];
foreach($cart as $row) {
if(empty($row['qty']) || $row['qty'] == 0) continue;
$store = $row['store_name'] ?? 'Unknown Store';
if(!isset($grouped[$store])) $grouped[$store] = [];
$price = (isset($row['special_price_num']) && $row['special_price_num'] > 0)
? $row['special_price_num'] : $row['price_num'];
$price_text = (isset($row['special_price']) && $row['special_price_num'] > 0 && $row['special_price'] != '')
? $row['special_price'] : $row['price'];
$qty = (int)$row['qty'];
$grouped[$store][] = [
'id' => $row['id'],
'product_variant_id' => $row['product_variant_id'],
'image' => $row['image'],
'name' => $row['name'],
'price' => $price,
'price_text' => $price_text,
'qty' => $qty,
'slug' => $row['slug'],
'minimum_order_quantity' => $row['minimum_order_quantity'],
'quantity_step_size' => $row['quantity_step_size'],
'total_allowed_quantity' => $row['total_allowed_quantity'],
'total' => $row['total']
];
}
$finalData = [];
foreach($grouped as $storeName => $items) {
$finalData[] = [
'store' => $storeName,
'items' => $items
];
}
?>
<style>
/* --- Updated Styles to match template --- */
.store-group {
margin-bottom: 30px;
border: 1px solid #ddd;
border-radius: 6px;
padding: 20px;
background-color: #fafafa;
}
.store-name {
font-size: 1.4rem;
font-weight: 700;
margin-bottom: 20px;
color: #222;
border-bottom: 2px solid #ccc;
padding-bottom: 8px;
}
/* Cart Items */
.cart-items {
display: flex;
flex-direction: column;
gap: 20px;
}
.cart-item {
display: flex;
align-items: center;
position: relative;
padding-bottom: 10px;
border-bottom: 1px solid #ddd;
}
.cart-item:last-child {
border-bottom: none;
}
.cart-item img {
width: 80px;
height: 80px;
border-radius: 6px;
object-fit: cover;
flex-shrink: 0;
}
.item-details {
flex: 1;
margin-left: 15px;
display: flex;
flex-direction: column;
justify-content: center;
min-width: 180px;
}
.item-details .title {
font-weight: 600;
font-size: 1rem;
margin: 0 0 6px;
line-height: 1.3;
color: #111;
}
.unit-price {
font-size: 0.9rem;
color: #555;
margin: 0;
}
/* Quantity and total container next to details */
.quantity-and-total {
display: flex;
flex-direction: column;
align-items: flex-end;
min-width: 140px;
margin-left: 20px;
}
/* Quantity control button group */
.quantity-control {
display: flex;
border: 1px solid #ccc;
border-radius: 6px;
overflow: hidden;
width: 130px;
}
.quantity-control button {
background: #eee;
border: none;
width: 40px;
height: 36px;
font-size: 20px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: background-color 0.2s ease;
}
.quantity-control button:hover {
background: #ddd;
}
.quantity-control input {
border: none;
width: 50px;
text-align: center;
font-size: 1rem;
outline: none;
height: 36px;
}
/* Total price */
.item-total-price {
font-weight: 700;
color: #000;
font-size: 1.1rem;
margin-top: 8px;
}
/* Remove button - positioned top right inside cart-item */
.product-removal.remove-btn {
position: absolute;
top: 8px;
right: 8px;
cursor: pointer;
color: #a00;
font-size: 24px;
transition: color 0.3s ease;
background: transparent;
border: none;
}
.product-removal.remove-btn:hover {
color: #e00;
}
/* Cart summary sidebar (unchanged mostly) */
.cart-sidebar {
border: 1px solid #ddd;
border-radius: 8px;
padding: 32px 24px;
background-color: #fff;
}
/* Responsive */
@media (max-width: 600px) {
.cart-item {
flex-direction: column;
align-items: flex-start;
padding-bottom: 15px;
}
.item-details {
margin-left: 0;
width: 100%;
margin-bottom: 10px;
}
.quantity-and-total {
flex-direction: row;
justify-content: space-between;
width: 100%;
margin-left: 0;
}
}
</style>
<!-- Updated Cart Items Section -->
<section class="cart py-80">
<div class="container container-lg">
<?php if (isset($cart[0]['total_items']) && !empty($cart[0]['total_items']) && $cart[0]['total_items'] != "") { ?>
<div class="row">
<div id="cart-container" class="col-xl-9 col-lg-8">
<?php foreach($finalData as $storeData) {?>
<div class="store-group" data-store="<?= htmlspecialchars($storeData['store']) ?>">
<div class="store-name"><?= htmlspecialchars($storeData['store']) ?></div>
<div class="cart-items">
<?php foreach($storeData['items'] as $row) { ?>
<div class="cart-item" data-id="<?= htmlspecialchars($row['id']) ?>" data-store="<?= htmlspecialchars($storeData['store']) ?>" data-price="<?= htmlspecialchars($row['price']) ?>">
<button type="button" class="product-removal remove-btn" title="Remove From Cart" aria-label="Remove <?= htmlspecialchars($row['name']) ?>" data-id="<?= htmlspecialchars($row['product_variant_id']) ?>">
<i class="ph ph-trash"></i>
</button>
<img src="<?= htmlspecialchars($row['image']) ?>" alt="<?= htmlspecialchars($row['name']) ?>" />
<div class="item-details">
<h6 class="title">
<a href="<?= base_url('products/details/' . $row['slug']) ?>" class="link" tabindex="0">
<?= nl2br(htmlspecialchars($row['name'])) ?>
</a>
</h6>
<p class="unit-price"><?= "單價: " . htmlspecialchars($row['price_text']) ?></p>
</div>
<div class="quantity-and-total">
<div class="quantity-control">
<?php $check_current_stock_status = validate_stock([$row['id']], [$row['qty']]); ?>
<?php if (isset($check_current_stock_status['error']) && $check_current_stock_status['error'] === TRUE) { ?>
<span class="text text-danger" style="padding: 0 12px; line-height:36px;">沒有庫存</span>
<?php } else { ?>
<button type="button" class="minus quantity__minus text-neutral-600"
data-min="<?= (int)($row['minimum_order_quantity'] ?? 1) ?>"
data-step="<?= (int)($row['quantity_step_size'] ?? 1) ?>">
<i class="ph ph-minus"></i>
</button>
<input type="number"
class="in-num itemQty"
data-page="cart"
data-id="<?= htmlspecialchars($row['id']); ?>"
value="<?= (int)$row['qty'] ?>"
data-price="<?= htmlspecialchars($row['price']) ?>"
data-step="<?= (int)($row['quantity_step_size'] ?? 1) ?>"
data-min="<?= (int)($row['minimum_order_quantity'] ?? 1) ?>"
data-max="<?= (int)($row['total_allowed_quantity'] ?? 0) ?>"
/>
<button type="button" class="plus quantity__plus text-neutral-600"
data-max="<?= (int)($row['total_allowed_quantity'] ?? 0) ?>"
data-step="<?= (int)($row['quantity_step_size'] ?? 1) ?>">
<i class="ph ph-plus"></i>
</button>
<?php } ?>
</div>
<div class="item-total-price" aria-label="Total Price"><?= htmlspecialchars($row['total']) ?></div>
</div>
</div>
<?php } ?>
</div>
</div>
<?php } ?>
</div>
<div class="col-xl-3 col-lg-4">
<div class="cart-sidebar">
<h6 class="text-xl mb-4">購物車總額</h6>
<div class="bg-color-three rounded-8 p-4">
<div class="mb-3 d-flex justify-content-between">
<span class="text-gray-900 font-heading-two">產品總額</span>
<span id="product_total" data-value="<?= htmlspecialchars($cart['sub_total'] ?? 0) ?>" class="fw-semibold"><?= htmlspecialchars($cart['sub_total'] ?? '0') ?></span>
</div>
<div class="mb-3 d-flex justify-content-between">
<span class="text-gray-900 font-heading-two">運費</span>
<span id="shipping-fee" data-value="<?= htmlspecialchars($cart['delivery_charge_sf_number'] ?? 0) ?>" class="fw-semibold"><?= htmlspecialchars($cart['delivery_charge_sf'] ?? '0') ?></span>
</div>
</div>
<div class="bg-color-three rounded-8 p-4 mt-3">
<div class="d-flex justify-content-between total-price">
<span class="text-gray-900 text-xl fw-semibold">共需支付</span>
<span id="final_total" data-value="<?= htmlspecialchars($cart['overall_amount'] ?? 0) ?>" class="text-gray-900 text-xl fw-semibold"><?= htmlspecialchars($cart['overall_amount'] ?? '0') ?></span>
</div>
</div>
<?php $disabled = empty($cart['sub_total']) ? 'disabled' : ''; ?>
<a href="<?= base_url('cart/checkout') ?>" class="btn btn-main mt-4 py-3 w-100 rounded-8" <?= $disabled ?>>前往付款</a>
</div>
</div>
</div>
<?php } else { ?>
<div class="d-flex flex-column align-items-center">
<img src="<?= base_url('assets/front_end/classic/images/empty-orders.webp') ?>" alt="Empty Cart" class="w-23" style="width:20%" />
<h1 class="h2 text-center mt-3">你的購物車是空的</h1>
</div>
<?php } ?>
</div>
</section>
Summary of changes made:
- CSS cleanups for flex alignment, spacing, font sizes.
- Removed redundant wrappers inside quantity control, simplified buttons/input layout.
- Made sure remove button appears top-right absolutely.
- Items aligned horizontally, with spaces between image/details and quantity/price.
- Semantic improvements like
htmlspecialchars() to prevent XSS.
- Responsive adjustments to stack layout on narrow screens.
- Adjusted container paddings and margins to match the reference template’s whitespace and alignment.
This updated structure + styling should bring your cart presentation visually closer to the provided template image.
If you'd like, I can help you further improve JS functionality for quantity buttons or removing items, or also help format prices with currency symbols or decimals display, just ask!