-
Notifications
You must be signed in to change notification settings - Fork 1
/
basket.class.php
111 lines (95 loc) · 2.13 KB
/
basket.class.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
<?php
/**
* Class for handling a basket
*/
class Basket
{
private $basket = array();
private $session_name = '';
public function __construct($session_name = 'basket')
{
if(session_status() == 1) session_start();
if(isset($_SESSION[$this->session_name]))
{
$this->basket = $_SESSION[$this->session_name];
} else {
$this->session_name = $this->session_name;
$_SESSION[$this->session_name] = array();
}
}
private function save_basket()
{
if(!isset($this->basket))
$this->basket = array();
$_SESSION[$this->session_name] = $this->basket;
}
public function add_to_basket($produkt_id, $navn, $antal, $pris)
{
if( isset($this->basket[$produkt_id]) )
{
$this->basket[$produkt_id]['antal'] += $antal;
} else {
$this->basket[$produkt_id] = [
'navn' => $navn,
'antal' => $antal,
'pris' => $pris
];
}
$this->save_basket();
}
public function remove_product($produkt_id)
{
unset($this->basket[$produkt_id]);
$this->save_basket();
}
public function remove_all_products()
{
unset($this->basket);
$this->save_basket();
}
public function price_with_vat($pris,$antal)
{
return ($pris*$antal)*1.25;
}
public function price_without_vat($pris,$antal)
{
return $pris*$antal;
}
public function price_only_vat($pris,$antal)
{
return ($pris*$antal)*0.25;
}
public function all_price_with_vat()
{
$samlet_pris = 0;
foreach ($this->basket as $produkt_id => $produkt) {
$samlet_pris += $this->price_with_vat($produkt['pris'],$produkt['antal']);
}
return $samlet_pris;
}
public function all_price_without_vat()
{
$samlet_pris = 0;
foreach ($this->basket as $produkt_id => $produkt) {
$samlet_pris += $this->price_without_vat($produkt['pris'],$produkt['antal']);
}
return $samlet_pris;
}
public function all_price_only_vat()
{
$samlet_pris = 0;
foreach ($this->basket as $produkt_id => $produkt) {
$samlet_pris += $this->price_only_vat($produkt['pris'],$produkt['antal']);
}
return $samlet_pris;
}
public function vis_kurv()
{
if( isset($this->basket) and count($this->basket)>0 )
{
return $this->basket;
} else {
return false;
}
}
}