-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManageCart.php
230 lines (157 loc) · 5.86 KB
/
ManageCart.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
//session_register("cart");
session_start();
header('Content-Type: text/xml');
?>
<?php // server side processing
$title = $_GET["bookTitle"];
//$ISBN = $_GET["bookISBN"];
$action = $_GET["action"];
//$price = $_GET["bookPrice"];
$id = $_GET["id"];
$ISBN = $_GET["bookPrice"]; //Swapping varaibles to get correct Price
$price = $_GET["bookISBN"];
if($action == "Add") {
$xmlfile = "../../data/goods.xml";
$flag = 0;
if (file_exists($xmlfile))
{
$dom = DOMDocument::load("../../data/goods.xml");
$item = $dom->getElementsByTagName("item");
foreach($item as $node)
{
$xmlEmail = $node->getElementsByTagName("itemname");
$xmlEmail = $xmlEmail->item(0)->nodeValue;
$node1 = $node;
if($xmlEmail == $title)
{
//$y = $node->getElementsByTagName("itemprice")->item(0)->nodeValue + 1 //Increasing 1 Quantity on hold
//$node->getElementsByTagName("itemprice")->item(0)->nodeValue = $y;
$xx = $node->getElementsByTagName("itemquantity")->item(0)->nodeValue - 1;
$node->getElementsByTagName("itemquantity")->item(0)->nodeValue = $xx; //Decreasing 1 quanitity available
$xx = $node->getElementsByTagName("quantityonhold")->item(0)->nodeValue + 1; //Increasing 1 Quantity on hold
$node->getElementsByTagName("quantityonhold")->item(0)->nodeValue = $xx;
//echo $yy;
//echo $node->getElementsByTagName("itemquantity")->item(0)->nodeValue;
//echo $xmlEmail;
}
//$y = $node->getElementsByTagName("itemprice")->item(0)->nodeValue + 1 //Increasing 1 Quantity on hold
// $node->getElementsByTagName("itemprice")->item(0)->nodeValue = $y;
}
$dom->save("../../data/goods.xml");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (!(empty($_SESSION["cart"])))
//if ($_SESSION["cart"] != "") //check the cart
{
$cart = $_SESSION["cart"]; //read the cart session into local variable
if ($action == "Add") //process add function
{
if (!isset($cart[$title]))
{
$qty = 1; // increase no of books by 1
$value = array(); //NOT SURE
$value["qty"] = $qty;
$value["isbn"] = $ISBN;
$value["id"] = $id;
$value["total"] = $price;
$cart[$title] = $value;
$_SESSION["cart"] = $cart; // save the adjusted cart to session variable
echo (toXml($cart)); // send XML form of CART to client
}
else
{
$value = $cart[$title];
$value["qty"] = $value["qty"] + 1;
$value["total"] = $price * $value["qty"];
$cart[$title] = $value;
$_SESSION["cart"] = $cart;
echo (toXml($cart));
}
}
else //proccess remove function
{
$value = $cart[$title];
//$value["qty"] = $value["qty"] - 1;
//$value["total"] = $price * $value["qty"];
$value["qty"] = 0;
$value["total"] = 0;
$cart[$title] = $value;
$_SESSION["cart"] = $cart;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$dom = DOMDocument::load("../../data/goods.xml");
$item = $dom->getElementsByTagName("item"); //UPDATE GOODS.XML TO INTITAL STATE
foreach($item as $node)
{
$xmlEmail = $node->getElementsByTagName("itemname");
$xmlEmail = $xmlEmail->item(0)->nodeValue;
$node1 = $node;
if($xmlEmail == $title)
{
$xx = $node->getElementsByTagName("quantityonhold")->item(0)->nodeValue; //Getting Quantity on hold number
$node->getElementsByTagName("quantityonhold")->item(0)->nodeValue = 0; //Setting quantity on hold as 0
$node->getElementsByTagName("itemquantity")->item(0)->nodeValue = $node->getElementsByTagName("itemquantity")->item(0)->nodeValue + $xx;
//Adding quanitity on hold to item quantity
}
}
$dom->save("../../data/goods.xml");
echo (toXml($cart));
}
}
elseif ($action == "Add") //created a new cart session with the particular book
{
$value = array();
$value["qty"] = "1";
$value["isbn"] = $ISBN;
$value["total"] = $price;
$value["id"] = $id;
$cart = array();
$cart[$title] = $value;
$_SESSION["cart"] = $cart;
echo (toXml($cart));
}
function toXml($shop_cart)
{
$doc = new DomDocument('1.0');
//to create <cart></cart>
$cart = $doc->createElement('cart');
$cart = $doc->appendChild($cart);
//$Item is a key
//$ItemName is the value
//$shop_cart is a assoc array
foreach ($shop_cart as $Item => $ItemName)
{
//to create <book></book>
$book = $doc->createElement('book');
$book = $cart->appendChild($book);
//This is to create <title></title>
$title = $doc->createElement('title');
$book->appendChild($title);
//and this to append inside <title>$value</title>
$value = $doc->createTextNode($Item);
$title->appendChild($value);
//or:
//$title = $doc->createElement('title', $Item);
//$title->appendChild($value);
$isbn = $doc->createElement('isbn');
$book->appendChild($isbn);
$value3 = $doc->createTextNode($ItemName["isbn"]);
$isbn->appendChild($value3);
$quantity = $doc->createElement('quantity');
$book->appendChild($quantity);
$value2 = $doc->createTextNode($ItemName["qty"]);
$quantity->appendChild($value2);
$total = $doc->createElement('total');
$book->appendChild($total);
$value3 = $doc->createTextNode($ItemName["total"]);
$total->appendChild($value3);
$idElement = $doc->createElement('id');
$book->appendChild($idElement);
$value4 = $doc->createTextNode($ItemName["id"]);
$idElement->appendChild($value4);
}
$strXml = $doc->saveXML();
return $strXml;
}
?>