-
Notifications
You must be signed in to change notification settings - Fork 19
/
action.php
92 lines (74 loc) · 2.27 KB
/
action.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
<?php
if (isset($_POST['action'])) {
session_start();
require_once('db/DbConnect.php');
$db = new DbConnect();
$conn = $db->connect();
require 'classes/workshop.class.php';
require 'classes/cart.class.php';
if (isset($_POST['wId'])) {
$objWorkshop = new workshop($conn);
$objWorkshop->setId($_POST['wId']);
$workshop = $objWorkshop->getWorkshopById();
}
$objCart = new cart($conn);
switch ($_POST['action']) {
case 'add':
$objCart->setCid($_SESSION['cid']);
$objCart->setPid($workshop['id']);
$objCart->setTitle($workshop['title']);
$objCart->setQuantity(1);
$objCart->setTotalAmount($workshop['price']);
$objCart->setCreatedOn(date('Y-m-d H:i:s'));
if($objCart->addItem()) {
echo json_encode( ["status" => 1, "msg" => "Added to cart."] );
exit;
} else {
echo json_encode( ["status" => 0, "msg" => "Opps!! Something went wrong."] );
exit;
}
break;
case 'update':
$objCart->setCid($_SESSION['cid']);
$objCart->setPid($workshop['id']);
$objCart->setQuantity($_POST['quantity']);
$objCart->setTotalAmount($workshop['price']*$_POST['quantity']);
if($objCart->updateItem()) {
$data = $objCart->calculatePrices();
echo json_encode( ["status" => 1, "msg" => "Cart updated.", 'data' => $data] );
exit;
} else {
echo json_encode( ["status" => 0, "msg" => "Opps!! Something went wrong."] );
exit;
}
break;
case 'remove':
$objCart->setCid($_SESSION['cid']);
$objCart->setId($_POST['cartId']);
if($objCart->removeItem()) {
$data = $objCart->calculatePrices();
echo json_encode( ["status" => 1, "msg" => "Cart item deleted.", 'data' => $data] );
exit;
} else {
echo json_encode( ["status" => 0, "msg" => "Opps!! Something went wrong."] );
exit;
}
break;
case 'clear':
$objCart->setCid($_SESSION['cid']);
if($objCart->removeAllItems()) {
echo json_encode( ["status" => 1, "msg" => "Cart is clear."] );
exit;
} else {
echo json_encode( ["status" => 0, "msg" => "Opps!! Something went wrong."] );
exit;
}
break;
default:
# code...
break;
}
} else {
header('location: index.php');
}
?>