- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart.php
More file actions
Latest commit
112 lines (98 loc) · 2.96 KB
/
Copy pathcart.php
File metadata and controls
112 lines (98 loc) · 2.96 KB
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
<?php
class Cart {
public$data;
publicfunction__construct() {
ob_end_clean();
if (!isset($_SESSION))
session_start();
$this->data = require'data.php';
}
publicfunctionindexAction() {
$view = (object)array(
'topcart' => $this->getTopCart(),
'cart' => $this->getCart(),
);
include'cart_tpl.php';
}
publicfunctionaddAction() {
if (!isset($_SESSION['cart']))
$_SESSION['cart'] = array();
if (isset($_SESSION['cart']) && !isset($_SESSION['cart'][(int)$_POST['id']]))
$_SESSION['cart'][(int)$_POST['id']] = 1;
elseif (isset($_SESSION['cart']) && isset($_SESSION['cart'][(int)$_POST['id']]))
$_SESSION['cart'][(int)$_POST['id']] = (int)$_SESSION['cart'][(int)$_POST['id']] + 1;
$result = $this->getTopCart();
$result['message'] = 'Товар добавлен в корзину';
echojson_encode($result);
}
publicfunctioncountAction() {
$id = (int)$_POST['id'];
$count = (int)$_POST['count'];
if (isset($_SESSION['cart']) && isset($_SESSION['cart'][$id]) && $count>0)
$_SESSION['cart'][$id] = $count;
$this->ajaxIndex();
}
publicfunctionclearAction() {
if (isset($_SESSION['cart']))
unset($_SESSION['cart']);
$this->ajaxIndex();
}
publicfunctiondelAction() {
$id = (int)$_POST['id'];
if (isset($_SESSION['cart']) && isset($_SESSION['cart'][$id]))
unset($_SESSION['cart'][$id]);
$this->ajaxIndex();
}
publicfunctiongetAction() {
echojson_encode($this->getTopCart());
}
privatefunctionajaxIndex() {
$view = (object)array(
'topcart' => $this->getTopCart(),
'cart' => $this->getCart(),
);
include'ajax_tpl.php';
}
privatefunctiongetTopCart() {
$return = array(
'count' => 0,
'summ' => 0,
);
if (isset($_SESSION['cart']) && is_array($_SESSION['cart']) && count($_SESSION['cart']) > 0) {
foreach(array_keys($_SESSION['cart']) as$key) {
if (isset($this->data[$key])) {
$price = $this->data[$key]['price'];
$return = array(
'count' => $return['count']+$_SESSION['cart'][$key],
'summ' => $return['summ']+(($price) ? $price : 0)*$_SESSION['cart'][$key],
);
}
}
}
return$return;
}
privatefunctiongetCart() {
$return = array();
if (isset($_SESSION['cart']) && is_array($_SESSION['cart']) && count($_SESSION['cart']) > 0) {
foreach(array_keys($_SESSION['cart']) as$key) {
if (isset($this->data[$key])) {
$product = $this->data[$key];
$product['id'] = $key;
$product['count'] = $_SESSION['cart'][$key];
$product['summ'] = (int)$_SESSION['cart'][$key] * $product['price'];
$return[] = $product;
}
}
}
return$return;
}
privatefunctioncount() {
return (isset($_SESSION['cart']) && is_array($_SESSION['cart']) && count($_SESSION['cart']) > 0) ? count($_SESSION['cart']) : 0;
}
}
$cart = newCart;
$action = preg_replace('/[^a-z]/i', '', isset($_GET['action']) ? $_GET['action'] : 'index').'Action';
if (method_exists($cart, $action))
$cart->$action();
else
header("HTTP/1.1 404 Not Found");