-
Notifications
You must be signed in to change notification settings - Fork 1
/
actions.php
143 lines (122 loc) · 4.47 KB
/
actions.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
134
135
136
137
138
139
140
141
142
143
<?php
include 'config.php';
// add products to cart
if(isset($_POST['addCart'])){
$p_id = $_POST['addCart'];
if(isset($_COOKIE['user_cart'])){
$user_cart = json_decode($_COOKIE['user_cart']);
}else{
$user_cart = [];
}
if(!in_array($p_id,$user_cart)){
array_push($user_cart,$p_id);
}
$cart_count = count($user_cart);
$u_cart = json_encode($user_cart);
if(setcookie('user_cart',$u_cart,time() + (1000),'/','','',TRUE)){
setcookie('cart_count',$cart_count,time() + (1000),'/','','',TRUE);
echo 'cookie set successfully';
}else{
echo 'false';
}
}
// remove products from cart
if(isset($_POST['removeCartItem'])){
$p_id = $_POST['removeCartItem'];
if($_COOKIE['cart_count'] == '1'){
setcookie('cart_count','',time() - (180),'/','','',TRUE);
setcookie('user_cart','',time() - (180),'/','','',TRUE);
}else{
if(isset($_COOKIE['user_cart'])){
$user_cart = json_decode($_COOKIE['user_cart']);
if(is_object($user_cart)){
$user_cart = get_object_vars($user_cart);
}
if (($key = array_search($p_id, $user_cart)) !== false) {
unset($user_cart[$key]);
}
}
$cart_count = count($user_cart);
$u_cart = json_encode($user_cart);
if(setcookie('user_cart',$u_cart,time() + (180),'/','','',TRUE)){
setcookie('cart_count',$cart_count,time() + (180),'/','','',TRUE);
echo 'cookie set successfully';
}else{
echo 'false';
}
}
}
// add products in wishlist
if(isset($_POST['addWishlist'])){
$p_id = $_POST['addWishlist'];
if(isset($_COOKIE['user_wishlist'])){
$user_wishlist = json_decode($_COOKIE['user_wishlist']);
}else{
$user_wishlist = [];
}
if(!in_array($p_id,$user_wishlist)){
array_push($user_wishlist,$p_id);
}
$wishlist_count = count($user_wishlist);
$u_wishlist = json_encode($user_wishlist);
if(setcookie('user_wishlist',$u_wishlist,time() + (180),'/','','',TRUE)){
setcookie('wishlist_count',$wishlist_count,time() + (180),'/','','',TRUE);
echo 'cookie set successfully';
}else{
echo 'false';
}
}
// remove products from wishlist
if(isset($_POST['removeWishlistItem'])){
$p_id = $_POST['removeWishlistItem'];
if($_COOKIE['wishlist_count'] == '1'){
setcookie('wishlist_count','',time() - (180),'/','','',TRUE);
setcookie('user_wishlist','',time() - (180),'/','','',TRUE);
}else{
if(isset($_COOKIE['user_wishlist'])){
$user_wishlist = json_decode($_COOKIE['user_wishlist']);
if(is_object($user_wishlist)){
$user_wishlist = get_object_vars($user_wishlist);
}
if (($key = array_search($p_id, $user_wishlist)) !== false) {
unset($user_wishlist[$key]);
}
}
$wishlist_count = count($user_wishlist);
$u_wishlist = json_encode($user_wishlist);
if(setcookie('user_wishlist',$u_wishlist,time() + (180),'/','','',TRUE)){
setcookie('wishlist_count',$wishlist_count,time() + (180),'/','','',TRUE);
echo 'cookie set successfully';
}else{
echo 'false';
}
}
}
// add products from wishlist to cart
if(isset($_POST['proceedCart'])){
$p_id = $_POST['proceedCart'];
if(isset($_COOKIE['user_wishlist'])){
$user_wishlist = json_decode($_COOKIE['user_wishlist']);
if(is_object($user_wishlist)){
$user_wishlist = get_object_vars($user_wishlist);
}
if(isset($_COOKIE['user_cart'])){
$user_cart = json_decode($_COOKIE['user_cart']);
if(is_object($user_cart)){
$user_cart = get_object_vars($user_cart);
}
array_merge($user_cart,$user_wishlist);
$user_cart = array_unique($user_cart);
}else{
$user_cart= $user_wishlist;
}
$u_cart = json_encode($user_cart);
$cart_count = count($user_cart);
if(setcookie('user_cart',$u_cart,time() + (180),'/','','',TRUE)){
setcookie('cart_count',$cart_count,time() + (180),'/','','',TRUE);
echo 'cookie set successfully';
}else{
echo 'false';
}
}
}