-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarenkorb.php
133 lines (115 loc) · 5.59 KB
/
warenkorb.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
session_start();
$produkte = require('core/produkte.php');
$einstellungen = require('core/einstellungen.php');
if (!isset($_SESSION['warenkorb'])) {
$_SESSION['warenkorb'] = [];
}
if (isset($_POST['clear'])) {
$_SESSION['warenkorb'] = [];
}
foreach ($produkte as $id => $produkt) {
if (isset($_POST[$id])) {
switch ($_POST[$id]) {
case 'Entfernen':
unset($_SESSION['warenkorb'][$id]);
break;
case 'Ändern':
if (isset($_POST['count'])) {
$anzahl = intval($_POST['count']);
if ($anzahl <= 0) {
unset($_SESSION['warenkorb'][$id]);
} else {
$_SESSION['warenkorb'][$id] = $anzahl;
}
} else {
$errors = ['Keine Anzahl definiert.'];
break;
}
}
header('Location: warenkorb.php');
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= $einstellungen['firma'] ?> Warenkorb</title>
<?php include('includes/head.inc.php') ?>
</head>
<body class="font-mono">
<?php include('includes/header.inc.php') ?>
<main class="flex flex-col gap-8 px-6 py-16 mx-auto max-w-7xl">
<?php if (isset($errors) && count($errors) > 0) : ?>
<section class="w-full p-4 mx-auto bg-red-300 border-2 border-red-500 rounded-md shadow-xl">
<?php foreach ($errors as $error) : ?>
<p class="text-lg font-semibold">Error: <?= $error ?></p>
<?php endforeach; ?>
</section>
<?php endif; ?>
<section>
<h1 class="text-4xl">Warenkorb</h1>
<p>Hier siehst du, was aktuell in deinem Warenkorb liegt.</p>
<?php if (!isset($_SESSION['user'])) echo '<p>Um die Bestellung abzuschließen, musst du dich erst <a class="underline text-honeygreen" href="login.php">hier einloggen.</a></p>'; ?>
</section>
<section class="w-full p-4 mx-auto bg-white rounded-md shadow-xl">
<div class="flex flex-col px-8 py-8 mt-4 border-4">
<?php if (empty($_SESSION['warenkorb'])) : ?>
<p class="mb-4">Dein Warenkorb ist noch leer!</p>
<?= Komponente::link('Zum Shop', 'shop.php') ?>
<?php else : ?>
<?php foreach ($_SESSION['warenkorb'] as $sorte => $anzahl) : ?>
<div class="flex items-center gap-3 px-3 py-3 my-3 bg-gray-200">
<img src="images/<?= $sorte ?>.png" class="w-24 h-24" alt="">
<div class="w-full">
<h2 class="mb-3 text-xl"><?= $produkte[$sorte]['name'] ?></h2>
<span class="flex flex-col items-center justify-between w-full gap-3 sm:flex-row">
<form action="" method="POST">
Menge: <?= Komponente::input('number', 'Anzahl', 'count', $anzahl) ?>
<?= Komponente::button('Ändern', $sorte) ?>
</form>
<form action="" method="POST">
<?= Komponente::button('Entfernen', $sorte, 'red') ?>
</form>
<div class="flex items-center justify-end grow">
<p class="px-3 py-2 text-xl bg-white">CHF <?= sprintf('%.2f', $produkte[$sorte]['preis'] * $anzahl) ?></p>
</div>
</span>
</div>
</div>
<?php endforeach; ?>
<div class="flex flex-col items-center gap-3 px-3 py-3 my-3 bg-gray-200">
<div class="flex flex-col items-stretch w-full gap-2 mb-3">
<div class="border-t-2 border-black"></div>
<div class="border-t-2 border-black"></div>
</div>
<div class="flex items-center justify-between w-full">
<div class="flex gap-4">
<form action="" method="POST">
<?= Komponente::button('Warenkorb löschen', 'clear') ?>
</form>
<?= Komponente::link('Bestellen', 'abschluss.php') ?>
</div>
<div class="flex items-center justify-end grow">
<?php
$gesamt = 0;
foreach ($_SESSION['warenkorb'] as $sorte => $anzahl) {
$gesamt += $produkte[$sorte]['preis'] * $anzahl;
}
?>
<span class="flex items-center gap-2">
<p class="text-xl">Gesamt:</p>
<p class="px-3 py-2 text-xl bg-white">CHF <?= sprintf('%.2f', $gesamt) ?></p>
</span>
</div>
</div>
</div>
<?php endif; ?>
</div>
</section>
</main>
</body>
</html>