36782-vm/related_suggestions.php
2025-12-12 14:13:03 +00:00

240 lines
11 KiB
PHP

<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
require_once 'includes/auth.php';
require_login();
require_once 'db/config.php';
require_once 'includes/helpers.php';
// Use product_id and qty from the URL
$product_id = isset($_GET['product_id']) ? (int)$_GET['product_id'] : 0;
$added_qty = isset($_GET['qty']) ? (int)$_GET['qty'] : 0;
// If product_id is invalid, redirect to cart
if ($product_id === 0) {
header('Location: cart.php');
exit;
}
$db = db();
$user_id = $_SESSION['user_id'];
// Fetch details for the added product, including the primary image
$stmt = $db->prepare("
SELECT
p.id,
p.name,
p.unit,
p.price_net,
COALESCE(cp.price, p.price_gross) as final_price,
pi.file_path AS primary_image
FROM products p
LEFT JOIN users u ON u.id = :user_id
LEFT JOIN client_prices cp ON cp.product_id = p.id AND cp.client_id = u.client_id
LEFT JOIN product_images pi ON pi.product_id = p.id AND pi.is_primary = 1
WHERE p.id = :product_id
");
$stmt->execute(['user_id' => $user_id, 'product_id' => $product_id]);
$added_product = $stmt->fetch(PDO::FETCH_ASSOC);
// If product somehow doesn't exist, redirect away
if (!$added_product) {
header('Location: cart.php');
exit;
}
// If image is not found, use a placeholder
if (empty($added_product['primary_image'])) {
$added_product['primary_image'] = 'assets/pasted-20251212-131440-62c0087c.jpg'; // A default placeholder
}
// Fetch related products (accessories)
$related_products_stmt = $db->prepare("
SELECT
p.id,
p.name,
p.unit,
p.price_net,
COALESCE(cp.price, p.price_gross) as final_price,
pi.file_path as primary_image
FROM products p
JOIN product_relations pr ON p.id = pr.related_product_id
LEFT JOIN users u ON u.id = :user_id
LEFT JOIN client_prices cp ON cp.product_id = p.id AND cp.client_id = u.client_id
LEFT JOIN product_images pi ON p.id = pi.product_id AND pi.is_primary = 1
WHERE pr.product_id = :product_id AND p.product_role = 'akcesoria'
");
$related_products_stmt->execute(['user_id' => $user_id, 'product_id' => $product_id]);
$related_products = $related_products_stmt->fetchAll(PDO::FETCH_ASSOC);
$user_role = get_user_role();
$page_title = 'Dodano do koszyka';
?>
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= htmlspecialchars($page_title) ?> - B2B Commerce</title>
<!-- SEO Meta Tags -->
<meta name="description" content="<?= htmlspecialchars($_SERVER['PROJECT_DESCRIPTION'] ?? 'A B2B E-commerce Platform') ?>">
<!-- Open Graph / Twitter Meta Tags (managed by the platform) -->
<meta property="og:title" content="<?= htmlspecialchars($page_title) ?>">
<meta property="og:description" content="<?= htmlspecialchars($_SERVER['PROJECT_DESCRIPTION'] ?? 'A B2B E-commerce Platform') ?>">
<meta property="og:image" content="<?= htmlspecialchars($_SERVER['PROJECT_IMAGE_URL'] ?? '') ?>">
<meta name="twitter:card" content="summary_large_image">
<!-- Bootstrap 5.3 CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Bootstrap Icons -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<!-- Custom CSS -->
<link rel="stylesheet" href="assets/css/custom.css?v=<?= time() ?>">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
<div class="container-fluid">
<a class="navbar-brand" href="index.php">
<img src="assets/pasted-20251209-065617-6bf1b4e6.png" alt="Logo" style="height: 40px;">
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ms-auto mb-2 mb-lg-0 align-items-center">
<li class="nav-item">
<a class="nav-link" href="index.php">Katalog</a>
</li>
<li class="nav-item">
<a class="nav-link" href="cart.php">
<i class="bi bi-cart"></i> Koszyk
<span class="badge bg-primary rounded-pill"><?= count($_SESSION['cart'] ?? []) ?></span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="orders.php">Zamówienia</a>
</li>
<?php if ($user_role === 'admin'): ?>
<li class="nav-item">
<a class="nav-link" href="/admin/products.php">Admin</a>
</li>
<?php endif; ?>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-person-circle"></i> Witaj, <?= isset($_SESSION['username']) ? htmlspecialchars($_SESSION['username']) : '' ?>
</a>
<ul class="dropdown-menu dropdown-menu-end">
<li><a class="dropdown-item p-2" href="profile.php">Profil</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item p-2" href="logout.php">Wyloguj</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<main class="container my-5">
<div class="alert alert-success text-center">
<strong>Produkt został pomyślnie dodany do koszyka!</strong>
</div>
<?php if ($added_product): ?>
<div class="card shadow-sm mb-4">
<div class="card-header">
<h4 class="mb-0">Dodałeś do koszyka:</h4>
</div>
<div class="card-body">
<div class="row align-items-center">
<div class="col-md-2 text-center">
<img src="<?= htmlspecialchars($added_product['primary_image']); ?>" class="img-fluid rounded" style="max-height: 120px;" alt="<?= htmlspecialchars($added_product['name']); ?>">
</div>
<div class="col-md-5">
<h5 class="mb-1"><?= htmlspecialchars($added_product['name']); ?></h5>
<?php if ($added_qty > 0): ?>
<small class="text-muted">Ilość: <?= $added_qty; ?></small>
<?php endif; ?>
</div>
<div class="col-md-5">
<div class="d-flex justify-content-end align-items-center">
<div class="text-end">
<p class="mb-0 h5"><strong><?= number_format($added_product['final_price'], 2, ',', ' '); ?> zł</strong> <small>brutto</small></p>
<p class="mb-0 text-muted"><?= number_format($added_product['price_net'], 2, ',', ' '); ?> zł <small>netto</small></p>
</div>
</div>
</div>
</div>
</div>
</div>
<?php endif; ?>
<?php if (!empty($related_products)): ?>
<h3 class="mt-5 mb-4">Polecamy także produkty powiązane:</h3>
<div class="list-group">
<?php foreach ($related_products as $product): ?>
<div class="list-group-item list-group-item-action">
<div class="row align-items-center">
<div class="col-md-2 text-center">
<a href="product.php?id=<?= $product['id']; ?>">
<img src="<?= htmlspecialchars(!empty($product['primary_image']) ? $product['primary_image'] : 'assets/pasted-20251212-131440-62c0087c.jpg'); ?>" class="img-fluid rounded" style="max-height: 100px;" alt="<?= htmlspecialchars($product['name']); ?>">
</a>
</div>
<div class="col-md-4">
<a href="product.php?id=<?= $product['id']; ?>" class="text-decoration-none text-dark">
<h5 class="mb-1"><?= htmlspecialchars($product['name']); ?></h5>
</a>
<p class="mb-1">
<?php if (!empty($product['unit'])): ?>
<small class="text-muted">Jednostka: <?= htmlspecialchars($product['unit']); ?></small>
<?php endif; ?>
</p>
</div>
<div class="col-md-3">
<div class="text-end">
<p class="mb-0 h5"><strong><?= number_format($product['final_price'], 2, ',', ' '); ?> zł</strong> <small>brutto</small></p>
<p class="mb-0 text-muted"><?= number_format($product['price_net'], 2, ',', ' '); ?> zł <small>netto</small></p>
</div>
</div>
<div class="col-md-3">
<form action="cart_actions.php" method="post" class="d-flex justify-content-end">
<input type="hidden" name="action" value="add">
<input type="hidden" name="product_id" value="<?= $product['id']; ?>">
<!-- Correctly redirect back to this suggestion page -->
<input type="hidden" name="redirect_to" value="related_suggestions.php?product_id=<?= $product_id; ?>">
<div class="input-group" style="max-width: 180px;">
<input type="number" name="quantity" class="form-control" value="1" min="1">
<button type="submit" class="btn btn-primary"><i class="bi bi-cart-plus"></i></button>
</div>
</form>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<div class="mt-5 d-flex justify-content-between">
<a href="index.php" class="btn btn-outline-secondary btn-lg"> <i class="bi bi-arrow-left"></i> Kontynuuj zakupy</a>
<a href="cart.php" class="btn btn-success btn-lg">Przejdź do koszyka <i class="bi bi-arrow-right"></i></a>
</div>
</main>
<footer class="text-center py-4 mt-auto text-muted bg-light">
<div class="container">
<p class="mb-0">&copy; <?php echo date("Y"); ?> powered by LEA24. All Rights Reserved.</p>
</div>
</footer>
<!-- Bootstrap 5.3 JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>