2.1 版本文件
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
// 购物车:查看 / 改数量 / 删除 / 清空
|
||||
require __DIR__ . '/includes/init.php';
|
||||
$pageTitle = __('cart_page.title');
|
||||
|
||||
$cart = $_SESSION['cart'] ?? [];
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
verifyCsrf();
|
||||
if (isset($_POST['clear'])) {
|
||||
$_SESSION['cart'] = [];
|
||||
} elseif (isset($_POST['remove'])) {
|
||||
$rid = (int) $_POST['remove'];
|
||||
unset($cart[$rid]);
|
||||
$_SESSION['cart'] = $cart;
|
||||
} elseif (isset($_POST['update'])) {
|
||||
foreach (($_POST['qty'] ?? []) as $pid => $qv) {
|
||||
$pid = (int) $pid;
|
||||
$qv = max(0, (int) $qv);
|
||||
if ($qv <= 0) {
|
||||
unset($cart[$pid]);
|
||||
} else {
|
||||
$cart[$pid] = $qv;
|
||||
}
|
||||
}
|
||||
$_SESSION['cart'] = $cart;
|
||||
}
|
||||
redirect('cart.php');
|
||||
}
|
||||
|
||||
$items = [];
|
||||
if (!empty($cart)) {
|
||||
$ids = array_keys($cart);
|
||||
$ph = implode(',', array_fill(0, count($ids), '?'));
|
||||
$stmt = db()->prepare('SELECT * FROM ' . tn('products') . ' WHERE id IN (' . $ph . ') AND status = 1');
|
||||
$stmt->execute($ids);
|
||||
foreach ($stmt->fetchAll() as $p) {
|
||||
$qty = (int) $cart[$p['id']];
|
||||
if ($qty > $p['stock']) $qty = $p['stock']; // 超出库存则裁剪
|
||||
$pointsSub = (!empty($p['points_price']) ? (int)$p['points_price'] * $qty : 0);
|
||||
$items[] = ['p' => $p, 'qty' => $qty, 'points_sub' => $pointsSub];
|
||||
}
|
||||
}
|
||||
|
||||
require 'includes/header.php';
|
||||
?>
|
||||
|
||||
<section class="section">
|
||||
<h2 class="sec-title"><i class="fas fa-shopping-cart"></i> <?= __('cart_page.title') ?></h2>
|
||||
|
||||
<?php if (empty($items)): ?>
|
||||
<p class="empty"><?= str_replace('{link}', '<a href="index.php">' . __('cart_page.empty_link') . '</a>', __('cart_page.empty')) ?></p>
|
||||
<?php else: ?>
|
||||
<form method="post" action="" class="cart-form">
|
||||
<?= csrfField() ?>
|
||||
<div class="cart-list">
|
||||
<?php foreach ($items as $it): $p = $it['p']; ?>
|
||||
<div class="cart-item">
|
||||
<a href="product.php?id=<?= (int)$p['id'] ?>" class="ci-media">
|
||||
<?php if (!empty($p['image'])): ?><img src="<?= h($p['image']) ?>" alt=""><?php else: ?><i class="fas <?= h($p['icon']) ?>"></i><?php endif; ?>
|
||||
</a>
|
||||
<div class="ci-info">
|
||||
<a href="product.php?id=<?= (int)$p['id'] ?>" class="ci-name"><?= h($p['name']) ?></a>
|
||||
<?php if (!empty($p['points_price'])): ?>
|
||||
<div class="ci-price"><?= (int)$p['points_price'] ?> <?= __('cart_page.points_each') ?></div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div class="ci-qty">
|
||||
<input type="number" name="qty[<?= (int)$p['id'] ?>]" value="<?= $it['qty'] ?>" min="1" max="<?= (int)$p['stock'] ?>">
|
||||
</div>
|
||||
<div class="ci-sub"><?= $it['points_sub'] > 0 ? (int)$it['points_sub'] . ' ' . __('common.points') : __('cart_page.subtotal_free') ?></div>
|
||||
<button type="submit" name="remove" value="<?= (int)$p['id'] ?>" class="ci-del" title="<?= __('cart_page.remove_title') ?>"><i class="fas fa-trash"></i></button>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
|
||||
<div class="cart-foot">
|
||||
<div class="cart-actions">
|
||||
<button type="submit" name="update" class="btn btn-ghost"><i class="fas fa-sync"></i> <?= __('cart_page.update_qty') ?></button>
|
||||
<button type="submit" name="clear" class="btn btn-ghost" onclick="return confirm('<?= __('cart_page.clear_confirm') ?>')"><?= __('cart_page.clear_btn') ?></button>
|
||||
<a href="checkout.php" class="btn btn-primary"><i class="fas fa-credit-card"></i> <?= __('cart_page.checkout_btn') ?></a>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
</section>
|
||||
|
||||
<?php require 'includes/footer.php'; ?>
|
||||
Reference in New Issue
Block a user